我正在尝试将Google脚本中的变量传递给HtmlOutputFromFile [英] I am trying to pass a variable from my Google Script through to HtmlOutputFromFile
问题描述
我正在尝试创建一个带有下拉列表的输入框,该列表基于从电子表格中提取的2D数组.
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.
到目前为止,我的研究告诉我,如果我将HtmlService.createHtmlOutputFromFile存储在一个变量中,则可以设置该变量的属性",然后将其传递给html. (我看到它专门用于HtmlService.createTemplateFromFile)
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
推荐答案
问题:
- 尝试修改
HtmlOutput
对象,而不是修改HtmlTemplate
对象. - Attempting to modify the
HtmlOutput
object instead of modifyingHtmlTemplate
object. - 仅html模板可以使用变量进行修改.修改
template
并对其求值以返回HtmlOutput
- Only the html template can be modified with variables. Modify the
template
and evaluate it to returnHtmlOutput
Issue:
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);
参考文献:
- HtmlTemplate
- HtmlOutput
- HtmlTemplate
- HtmlOutput
References:
这篇关于我正在尝试将Google脚本中的变量传递给HtmlOutputFromFile的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!