I have a small rota application in Google Sheets.
There is a tab that lists the types of jobs/roles. This list can change, e.g. last week a new role was added to the list.
I have an HTML form for adding new employees to the rota application.
The form uses the list of roles in the spreadsheet to dynamically create the input fields on the form on my addPerson.html:
<? var ss = SpreadsheetApp.getActive(); //get the currently open & active workbook
var roles =ss.getSheetByName("Roles"); // get the Roles tab
var roleValues = roles.getRange(1,1,roles.getLastRow(),1).getValues();?>
<table>
<? for (i = 0; i < roleValues.length; i++) { ?>
<tr><td><?= roleValues[i]?>:</td><td> £</td><td> <input type = "number" name=<?= String(roleValues[i]).replace(/\s/g,'') ?> id=<?= String(roleValues[i]).replace(/\s/g,'') ?>/></td></tr>
<? } ?>
</table>
That's all working fine.
Where I am having trouble is in accessing the data from the form for the fields that are created in the code above. I can access the other data without a problem.
Here is how I am trying to get the fields in question in my code.gs:
for(i=0; i<lastRole; i++){
nextRow++;
var role = String(roleValues[i]).replace(/\s/g,'');
Logger.log(role);
if(form.role !== null){
Logger.log(form.role);
rates.getRange(nextRow, 1).setValue(name);
rates.getRange(nextRow, 2).setValue(form.role);
rates.getRange(nextRow, 3).setValue(rate);
rates.getRange(nextRow, 4).setValue(team);
}
}
The initial definition of role works fine, but using that as form.role doesn't work, as you can see in the logs.
[18-03-16 04:04:43:634 PDT] Bar
[18-03-16 04:04:43:635 PDT] undefined
[18-03-16 04:04:43:643 PDT] BarB
[18-03-16 04:04:43:643 PDT] undefined
[18-03-16 04:04:43:651 PDT] BarSup
[18-03-16 04:04:43:652 PDT] undefined
[18-03-16 04:04:43:660 PDT] DM
[18-03-16 04:04:43:661 PDT] undefined
[18-03-16 04:04:43:669 PDT] DT
[18-03-16 04:04:43:670 PDT] undefined
[18-03-16 04:04:43:678 PDT] FoHC
[18-03-16 04:04:43:679 PDT] undefined
[18-03-16 04:04:43:688 PDT] Maint
[18-03-16 04:04:43:688 PDT] undefined
[18-03-16 04:04:43:697 PDT] TM
[18-03-16 04:04:43:698 PDT] undefined
[18-03-16 04:04:43:707 PDT] VT
[18-03-16 04:04:43:707 PDT] undefined
How do I work through the list of HTML form input fields to get the data into a spreadsheet?
Logger.log(form)Logger.log(form):[18-03-16 17:07:49:266 GMT] {BarB=8, BarSup=, DT=, Maint=, Bar=, baseRate=8, FoHC=, name=Test, DM=, TM=, team=Box, VT=}<?=tag is used to populate the HTML form with the output of the code within the tag. You can see from the log information above that each of the elements has the required name. What I am seeking to achieve is that the content in the fieldBarBis added to a spreadsheet. However, I don't want to callBarBexplicitly - I want to create that call using variables, i.e. pick upBarBfrom the list of roles on the spreadsheet and then use that to find the corresponding value from the form and put it into another sheet.form.roledoesn't exist. However,form[role]does.