Javascript 从HtmlService向Google应用程序脚本函数发送变量
我有一个名为Javascript 从HtmlService向Google应用程序脚本函数发送变量,javascript,google-apps-script,Javascript,Google Apps Script,我有一个名为showModalReport()的Google应用程序脚本函数,我将一个名为reportData的对象传递给它,该对象属于Javascript对象类型(它具有属性和值对)。此函数创建并提供HtmlTemplate: 函数showModalReport(reportData){ var template=HtmlService.createTemplateFromFile('modalReport'); template.reportData=reportData;//这使repor
showModalReport()
的Google应用程序脚本函数,我将一个名为reportData
的对象传递给它,该对象属于Javascript对象类型(它具有属性和值对)。此函数创建并提供HtmlTemplate:
函数showModalReport(reportData){
var template=HtmlService.createTemplateFromFile('modalReport');
template.reportData=reportData;//这使reportData在HTML模式对话框中可用
template=template.evaluate().setSandboxMode(HtmlService.SandboxMode.IFRAME);
SpreadsheetApp.getUi().showModalDialog(模板“测试报告”);
}
下面是我的modalReport.html文件:
报告名称:
函数onClick函数(reportData){
google.script.run.withSuccessHandler(function(){google.script.host.close();}).emailReport(reportData);
}
对话框可以正常工作并正确显示报告名称,因此我知道reportData
变量可用。接下来,我想让它在用户单击按钮时调用emailReport()
函数,该函数通过电子邮件将报告数据发送给某人,然后关闭模式对话框
以下是emailReport()
:
函数emailReport(reportData){
Logger.log('reportData:'+reportData);
//电子邮件代码
}
但是emailReport()
函数需要将reportData
对象作为参数传递,这就是我一直坚持的部分。如何将reportData
传递到emailReport()
我已经试过了:
onclick=“onClickFunctions(reportData)”//Javascript控制台输出错误:“未捕获引用错误:未定义reportData”
onclick=“onClickFunctions()”//reportData被发送到函数中,但为null
onclick=“onClickFunctions(this.reportData)”//reportData被发送到函数中,但为null
知道如何将该变量发送到函数吗
此外,我知道这里还有许多其他问题与我的问题类似。我阅读并尝试了他们中许多人的代码,也尝试了谷歌HTMLService文档中推荐的内容,但我仍然被卡住了 您可以将JSON.stringify reportData并传递:
template.reportData = reportData;
template.reportDataJSON = JSON.stringify(reportData);
然后:
非常感谢。另外,对于任何可能遇到这种情况的人来说:Cameron的JSON解析建议是一个有效的解决方案。使用
的强制打印scriptlet语法的第一个建议产生了错误Uncaught SyntaxError:Unexpected identifier
onclick="onClickFunctions('<?= reportDataJSON ?>');
function emailReport(reportData) {
var reportData = JSON.parse(reportData);
}