Creating webpages with Excel Web App mashups
Pages in this article
Unfortunately the functionality discussed in this article is no longer available
Getting input from web form controls into your Excel file
Wouldn't it be nice to have a web form which users can enter data into and have your Excel Web App updated with the content of the web form. That is what I describe here.
Put up a form with some controls
To get data from a form textbox into your Excel file, you need to place a form on your web page. This piece of html does that:
<form name="ChangeInput">
Phase difference (degrees)
<input name="Phase" size="18" style="width: 112px" />
Frequency ratio
<input name="Freq" size="18" style="width: 112px" />
<input onclick="JavaScript:SetValuesInSheet();" type="button"
value="Update Chart" />
</form>
As you can see, the button's click event is set to call a Javascript function called SetValuesInSheet.
Writing values in a cell
The SetValuesInSheet function is relatively short:
function SetValuesInSheet()
{
// Get a reference to where we need to write
ewaCht.getActiveWorkbook().getRangeA1Async('b1:b2',
getRangeA1Callback, null);
}
As you can see, we first need to ask the Ewa to give us a reference to range B1:B2 in the active sheet. This is done asynchronously, so a call-back function is needed: getRangeA1Callback. Listed below:
function getRangeA1Callback(asyncResult)
{
// getRangeA1Async returns a range
object through the AsyncResult object.
var range =
asyncResult.getReturnValue();
var values = new Array(2);
values[0] = new Array(1);
values[1] = new Array(1);
var control =
document.ChangeInput.Phase;
values[0][0] = control.value;
control = document.ChangeInput.Freq;
values[1][0] = control.value;
// Now send the array to the range.
range.setValuesAsync(values,Dummy,null);
}
If you want to push data into cells, you always need to use an array, like the code above shows.
Because the setValuesAsync is -again- asynchronous, it requires a callback, for which I included an empty function called Dummy (not shown here). NB: Dummy can be replaced with "null", but I left it in because the code works as it is.
So does this all work? Have a look at the next page!
Frequently asked Questions
How can I get input from web form controls into my Excel file?
What HTML code is needed to create a form with controls for Excel Web App?
How does the SetValuesInSheet function write values into Excel cells?
Why do I need to use an array to push data into Excel cells?
What is the purpose of the callback function in setValuesAsync?
How is the button click event linked to updating the Excel Web App?
What does the getRangeA1Async method do in the JavaScript code?
How are values retrieved from form controls and assigned to the Excel range?
Is it possible to update an Excel Web App with data entered in a web form?
Where can I see a working example of updating Excel Web App from a web form?



Comments