Invalid Argument: Userinterface error with createTemplateFromFile
Asked Answered
U

1

5

I have used createTemplateFromFile many time in the past. It lets me use an include method so that CSS and JavaScript can be broken out into a different file. But today I can't seem to make it work. Below is my test code bound to a spreadsheet. Any ideas why its not working? I tried another existing spreadsheet with a custom dialog using the technique and it works.

Code.gs

function onOpen() {
  var ui = SpreadsheetApp.getUi();
  var menu = ui.createMenu("Test");
  menu.addItem("Test", "test");
  menu.addToUi();
}

function test() {
  try {
    var html = HtmlService.createTemplateFromFile("HTML_Test");
    Logger.log(html);
    // was html.evaluate();
    html = html.evaluate(); // correction
    SpreadsheetApp.getUi().showSidebar(html);
  }
  catch(err) {
    Logger.log(err);
  }
}

HTML_Test:

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
  </head>
  <body>
    <select>
      <option value="Doc1">Document 1</option>
      <option value="Doc2">Document 2</option>
      <option value="Doc3">Document 3</option>
    </select>
  </body>
</html>

Log

[19-01-24 09:54:51:240 PST] {}
[19-01-24 09:54:51:246 PST] Exception: Invalid argument: userInterface
Ulotrichous answered 24/1, 2019 at 19:44 Comment(2)
You need to create HtmlOutput from template. The evaluate doesn't mutate the htmlTemplate, but returns a HtmlOutput. var htmlOutput=html.evaluate(); ..showSidebar(htmlOutput);Cream
Possible duplicate of Google Apps Script - HTML service "createTemplateFromFile" not usable from within App (spreadsheet, etc.)?Cream
U
8

I've been tweeking this every which way to figure out why it wouldn't work. You get blind to a simple solution. html = html.evaluate() works.

Ulotrichous answered 24/1, 2019 at 20:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.