Apps-Script email permissions approved in script editor, but not in sheet?

为君一笑 提交于 2019-12-12 03:58:19

问题


I am using Google Enterprise (in case that makes a difference).

I have made a spreadsheet with a very short script, just two functions, one that counts the # of unread gmail mails which I called unread_emails(), and an onOpen() script that copies the current time to a spreadsheet cell (so I know that it's running) and then calls the first function and copies its result to the spreadsheet.

When I first ran this from the Script Editor, it asked me for permissions, and I clicked okay. Since then, the script always runs perfectly if I launch it from the Script Editor, and no longer asks for permissions. BUT -- if I just open the spreadsheet, the onOpen() function will be halted part-way through, by a permissions error on getInboxUnreadCount() -- even though I've already given permission, and it's clearly running on my account (and no, it doesn't ask me to simply re-affirm my permissions, either). If I re-open the Script Editor, everything runs fine.

How can I get Google Sheets to recognize the permissions that are already assigned here? Any insights would be greatly appreciated.

Here is the simple script. The GmailApp at line 3 is the apparent source of troubles.

function unread_mails() {
  Logger.log("starting unanswered func");
  return GmailApp.getInboxUnreadCount();
}

function onOpen() {
  Logger.log("starting onOpen func");
  var ss = SpreadsheetApp.getActive();
  var sheet = ss.getSheets()[0];
  var d = new Date();
  sheet.getRange("B4").setValue(0); // clear
  sheet.getRange('B2').setValue(d.toLocaleTimeString());
  sheet.getRange("B4").setValue(unread_mails());
}

回答1:


from simple trigger restrictions:

They cannot access services that require authorization. For example, a simple trigger cannot send an email because the Gmail service requires authorization

it works from the editor, b/c running onOpen() manually isn't the same as having the simple trigger fire off automatically



来源:https://stackoverflow.com/questions/34645966/apps-script-email-permissions-approved-in-script-editor-but-not-in-sheet

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!