Creating webpages with Excel Web App mashups
Pages in this article
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!
Comments