我正在使用react google apps脚本来实现电子表格的google apps script UI。
我的服务器实现中有代码作为
const getActiveUserEmail = () => {
return Session.getActiveUser().getEmail();
};
它通过从客户端引用
import { getActiveUserEmail } from '../../../server';
const email = getActiveUserEmail();
在我部署到电子表格并运行它之后,当代码被命中时,我会得到未定义的"会话"。如果该代码被命中,我还会得到"PropertiesService undefined"。
我能够毫无问题地使用HtmlService、SpreadsheetApp和UrlFetch api。
我的appscript.json文件包含
{
"timeZone": "America/New_York",
"dependencies": {},
"exceptionLogging": "STACKDRIVER",
"oauthScopes": [
"https://www.googleapis.com/auth/script.container.ui",
"https://www.googleapis.com/auth/spreadsheets",
"https://www.googleapis.com/auth/script.external_request",
"https://www.googleapis.com/auth/userinfo.email"
],
"runtimeVersion": "V8"
}
为什么这些是未定义的,我如何才能强迫它们被定义?
更新:Enushi提供了一个工作代码示例。
很可能。。。
const email = Session.getActiveUser().getEmail();
const props = PropertiesService.getScriptProperties().getProperties();
在客户端代码中(以某种方式将它们传递给HtmlService.htmlOutput
(,而不是在服务器端代码中。
上面的方法可以与Html.htmlTemplate
一起使用,但应该包含在要作为服务器端代码执行的scriptlet中。
相关
- 服务器端对象在传递给客户端代码时丢失
- 引用:TypeError:无法从未定义中读取属性[此处为属性名称]
参考
- http://developers.google.com/apps-script/guides/html
- http://developers.google.com/apps-script/guides/html/templates