程序的作用:我编写了一个简单的Google Apps脚本,该脚本可从当前登录的Gmail帐户中的标签中检索未读的电子邮件,并返回这些电子邮件的数量并显示在网站。
问题::当我从开发该计算机的计算机上创建一个Web应用程序并从网站上运行该应用程序时,此程序将运行。但是,如果我复制该URL并将其分发到以其他Google帐户身份登录的另一台计算机上,它将无法正常工作。
代码:
/*Sample.gs*/
function doGet(e){
return HtmlService.createHtmlOutputFromFile('Run.html');
}
function GetUnreadCount(sourceLabel){
return GmailApp.getUserLabelByName(sourceLabel).getUnreadCount();
}
function Main()
{
/*For testing,return unread email count from "CustomerA" label of Gmail*/
return GetUnreadCount("CustomerA");
}
。
/*Run.html*/
<!DOCTYPE html>
<html>
<head>
<base target="_top">
<script>
function CallGetUnreadCount() {
document.getElementById('Messages').innerHTML = 'Counting...';
google.script.run
.withSuccessHandler(onSuccess)
.withFailureHandler(onFailure)
.Main();
}
function onSuccess(UnreadCount)
{
document.getElementById('Messages').innerHTML = "There are " + UnreadCount + " unread emails.";
}
function onFailure(error)
{
document.getElementById('Messages').innerHTML = "Err: " + error.message;
}
</script>
</head>
<body>
<div>
<h1 id="Messages">Count # of unread emails</h1>
</div>
<div>
<button type="button" onclick='CallGetUnreadCount();' id="CountButton">Count Unread Emails</button>
</div>
</body>
</html>
如果我在编写此脚本的计算机上运行该程序,则该程序可以正常运行。但是,如果我将其部署为Web应用程序并分发URL(以/exec
结尾),然后在以其他Gmail帐户登录的另一台计算机上运行,即使未读邮件也始终返回0。带有CustomerA
标签的电子邮件。
令我感到困惑的是,如果我在另一台计算机上创建一个新项目并将上述代码复制粘贴到.gs
文件和html
文件中并作为Web部署,则该程序可以在另一台计算机上运行应用程式。
如果每台计算机都自行部署Web应用程序,为什么同一程序可以工作,但是如果我分发URL,则为什么该程序不能工作?