Google Apps脚本-HTML服务"createTemplateFromFile"无法在App(电子表格等)中使用? [英] Google Apps Script - HTML service "createTemplateFromFile" not usable from within App (spreadsheet, etc.)?
问题描述
HtmlService的文档没有说明这不起作用,但是似乎模板方法仅限于Web应用程序Apps Script项目.
The docs for HtmlService don't state that this shouldn't work, but it seems that the template approach is limited to web app Apps Script projects.
我正在尝试使用HTMLService在SpreadSheet中创建一个UI,该UI可以采用一些初始上下文,然后与工作表进行交互.但是,在工作表的Apps脚本项目中尝试使用"createTemplateFromFile"时,将引发运行时错误.
I am attempting to use the HTMLService to create a UI within a SpreadSheet that can take some initial context, and then interact with the sheet. However, a runtime error is thrown when attempting to use "createTemplateFromFile" from within an Apps Script project in the sheet.
"changeDialog"是Apps Script项目中的一个简单文件,带有占位符标记:
"changeDialog" referenced below is a simple file in the Apps Script project with placeholder markup:
<h2> fieldName </h2>
以下代码在被调用时引发异常无效参数:用户界面":
The following code throws an exception "Invalid argument: userinterface" when invoked:
function showSidebar(){
var html = HtmlService.createTemplateFromFile('changeDialog');
html.evaluate();
SpreadsheetApp.getUi()
.showSidebar(html);
}
更新:使用了无效的实际代码进行了更新.并且...由[Mogsdad]回答.所有模板文档都显示了网页变体,其中"doGet()"方法返回".evaluate()"的结果.我认为html对象承载着评估"方法的内部状态.我发现您要么将结果设置为另一个var,要么只是在.showSideBar(...)方法中进行在线调用:
UPDATE: Updated with the actual code that wasn't working. And...answered by [Mogsdad]. All of the template docs show the web page variant, with the "doGet()" method returning the results of ".evaluate()". I thought that the html object carried the internal state of the "evaluate" method. I turns out that you either set that result to another var, or just do the call in line in the .showSideBar(...) method:
SpreadsheetApp.getUi()
.showSidebar(html.evaluate());
或...
var evaluatedHtml = html.evaluate();
SpreadsheetApp.getUi()
.showSidebar(evaluatedHtml);
推荐答案
您需要evaluate()
模板来创建HtmlOutput
对象.
You need to evaluate()
a template to create an HtmlOutput
object.
function showSidebar(){
var html = HtmlService.createTemplateFromFile('changeDialog');
var a = "test";
SpreadsheetApp.getUi()
.showSidebar(html.evaluate());
//////////
}
由于您的changeDialog.html
文件不包含模板标签,因此您可以直接从中创建HTML文件:
Since your changeDialog.html
file contains no template tags, you could instead create an HTML file from it directly:
function showSidebar(){
var html = HtmlService.createOutputFromFile('changeDialog');
//////
var a = "test";
SpreadsheetApp.getUi()
.showSidebar(html);
}
这篇关于Google Apps脚本-HTML服务"createTemplateFromFile"无法在App(电子表格等)中使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!