HTML侧:
Google Apps Script side:
我正在编写一个脚本,从URL参数和电子邮件数量中获取用户,如下所示输入图片描述
然而">UserName"是否可以停止该代码,如下所示,
摘自index.html
<script>
function onSuccess2(jUserName)
{
var div = document.getElementById('output2');
div.innerHTML = 'You User Name Is ' + jUserName
}
google.script.run.withSuccessHandler(onSuccess2)
.getUserName();
</script>
<body>
<div id="output"></div>
<div id="output2"></div>
</body>
code.gs
const userProperties = PropertiesService.getUserProperties();
function doGet(e) {
Logger.log(JSON.stringify(e.parameter));
UserName=JSON.stringify(e.parameter['UserName']);
userProperties.setProperty('UserName', UserName);
Logger.log("UserName= "+UserName);
return HtmlService.createHtmlOutputFromFile('Index');
}
function getUnreadEmails() {
return GmailApp.getInboxUnreadCount();
}
function getUserName() {
Logger.log("Getting User Name");
return userProperties.getProperty("UserName");
}
修改点:
- 在你的脚本中,没有使用
getUnreadEmails()
。 - 在您的情况下,我认为当使用HTML模板时,脚本可能很简单。而且,当使用
google.script.run
时,它是在加载HTML之后运行的。这样,当使用HTML模板时,该成本可能会降低。
当这些要点反映在您的脚本中时,如何进行以下修改?
HTML侧:Index.html
<body>
<div id="output">Count: <?= count ?></div>
<div id="output2">You User Name Is <?= userName ?></div>
</body>
Google Apps Script side:Code.gs
function doGet(e) {
const html = HtmlService.createTemplateFromFile('Index');
html.count = GmailApp.getInboxUnreadCount();
html.userName = e.parameter['UserName'];
return html.evaluate();
}
当使用
https://script.google.com/macros/s/###/exec?UserName=sample
这样的URL访问此Web Apps时,得到以下结果:Count: 123 You User Name Is sample
参考:
- HTML Service: Templated HTML