kaminikage
kaminikage

Reputation: 23

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

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

Upvotes: 2

Views: 1081

Answers (1)

TheMaster
TheMaster

Reputation: 50445

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.createTemplateFromFile('Month Box');//Type: HtmlTemplate
monthBox.mList = sNamesArray;

monthBox = monthBox.evaluate()    //Type: HtmlOutput after evaluation
    .setSandboxMode(HtmlService.SandboxMode.IFRAME)
    .setWidth(250)
    .setHeight(50);

monthbox = monthbox.getContent();

References:

Upvotes: 2

Related Questions