问题
I am trying to create an input box with a drop down list, where that list is based on a 2D array pulled from a Spreadsheet.
My research so far has told me that if i store the HtmlService.createHtmlOutputFromFile in a variable that I can then "set properties" of that variable that will then get passed to the html. (i saw this used specifically with HtmlService.createTemplateFromFile)
//google script code
function selectMonth(){
var monthTab = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("sheet1");
var LR = monthTab.getRange("B1").getDataRegion().getLastRow()
var sNamesArray = monthTab.getRange(1,2,LR,2).getValues()
var monthBox = HtmlService.createHtmlOutputFromFile('Month Box')
.setSandboxMode(HtmlService.SandboxMode.IFRAME)
.setWidth(250)
.setHeight(50);
//i believe the error to be occurring on this line
monthBox.mList = sNamesArray
SpreadsheetApp.getUi().showModalDialog(monthBox, 'Student Name List');
};
<!--html code-->
<select id="tabMonth">
<option disabled selected>Select Month</option>
<?for(var i=0;i<list.length;i++){ ?>
<option value=<?mList[i][1]?>><?mList[i][0]?></option>
<?}?>
</select>
but every time i try and run the code i get the error: "Object does not allow properties to be added or changed."
based upon what i can tell the error is occurring on the line indicated above
回答1:
Issue:
- Attempting to modify the
HtmlOutputobject instead of modifyingHtmlTemplateobject.
Solution:
- Only the html template can be modified with variables. Modify the
templateand evaluate it to returnHtmlOutput
Snippet:
var monthBox = HtmlService.createHtmlTemplateFromFile('Month Box');//Type: HtmlTemplate
monthBox.mList = sNamesArray;
monthBox = monthBox.evalate() //Type: HtmlOutput after evaluation
.setSandboxMode(HtmlService.SandboxMode.IFRAME)
.setWidth(250)
.setHeight(50);
References:
- HtmlTemplate
- HtmlOutput
来源:https://stackoverflow.com/questions/57238218/i-am-trying-to-pass-a-variable-from-my-google-script-through-to-htmloutputfromfi