Appendix: Performing an Action on Each Row of an Uploaded Spreadsheet
After using an EXCELUPLOAD element to upload data from a spreadsheet into session memory, you can use the ForEachSessionListItem function to perform an action on each row of uploaded data.
For each row, before the action is called the function will copy the row's values to model variables, where the name of the variable matches the corresponding keyword defined by the uploader. The action can then interact with the row's values by referencing those variables. The function will return True if it completes successfully, and False if not.
The ForEachSessionListItem function's syntax is as follows:
ForEachSessionListItem( ActionName, ItemName );
ActionName is a string containing the name of the action you want to call for each row of data.
ItemName is a string containing the name of the session item that holds the uploaded spreadsheet's data. This name is determined by the uploader's syntax - if no name was specified, the uploader will use an item named DataSourceUpload by default.
To demonstrate how this function might be used, a student administrator named Jane Jones has created a spreadsheet uploader using the following element:
<%# EXCELUPLOAD(
TotalHeaderRows="1"
MaxDisplayedRows="10"
SessionName="StudentSheetUpload"
ColumnA="UserID|Integer"
ColumnB="FirstName|String"
ColumnC="LastName|String") %>
As a result of using this, no more than ten rows of spreadsheet data will be uploaded to session memory, with each row containing values from three columns. All of this data will be stored in a session item named StudentSheetUpload.
Jane wants to write each row of data in StudentSheetUpload into a data source named StudentRecords. The data source includes three columns with the aliases UserID, FirstName and LastName, corresponding with the keywords assigned to the three columns of uploaded data.
To write the data to StudentRecords, Jane creates an action named WriteStudentToDB and gives it the following expression:
WriteData("UserID,FirstName,LastName","","StudentRecords");
Each time this action is called, the values of the three alias keywords are used to create a new row in StudentRecords. To use this action on each row of the StudentSheetUpload session item, Jane writes the following expression:
ForEachSessionListItem( "WriteStudentToDB", "StudentSheetUpload" );
This expression will call the WriteStudentToDB action once per row of data. Before the action is called for a particular row, ForEachSessionListItem will assign the row's three values to the corresponding keywords - which, as mentioned above, are the same as the data source's aliases. Then, when the action itself is called, it creates a new row in StudentRecords using the three current values of its aliases. After the final row has been created, ForEachSessionListItem will complete.