I am trying to pass a variable from my Google Script through to HtmlOutputFromFile

风格不统一 提交于 2020-07-02 03:00:29

问题


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 HtmlOutput object instead of modifying HtmlTemplate object.

Solution:

  • Only the html template can be modified with variables. Modify the template and evaluate it to return HtmlOutput

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!