Google apps script 日历同步-安装onEventUpdated触发器

Google apps script 日历同步-安装onEventUpdated触发器,google-apps-script,google-calendar-api,Google Apps Script,Google Calendar Api,背线:我有一个不明白的问题;如果在脚本编辑器中手动运行此代码,则可以运行此代码: ScriptApp.newTrigger('functionname').forUserCalendar('usercalendar').onEventUpdated().create() 但是,当我从清单安装应用程序并尝试从用户角度运行相同的代码时,我收到一个错误“异常:很抱歉,发生了服务器错误。请稍等,然后重试。[行:15,函数:installTrigger,文件:code]” 上下文:我正在与一个客户端合作

背线:我有一个不明白的问题;如果在脚本编辑器中手动运行此代码,则可以运行此代码:

ScriptApp.newTrigger('functionname').forUserCalendar('usercalendar').onEventUpdated().create()
但是,当我从清单安装应用程序并尝试从用户角度运行相同的代码时,我收到一个错误“异常:很抱歉,发生了服务器错误。请稍等,然后重试。[行:15,函数:installTrigger,文件:code]”

上下文:我正在与一个客户端合作,为谷歌日历设置一个会议扩展

这其中的一部分涉及到保持他们的服务器大致上与用户的任何日历更改同步——如果我在上午10点有一个会议,但在最后一分钟我将会议更改为上午11点,服务器需要知道

文档中实际上有一个关于此的部分:。其思想是该插件安装一个触发器,该触发器在事件更新时运行。你需要做一些过滤,以确保你只发送相关数据,但这是广泛的机制。我简直无法复制创建日历一次触发的功能

为了演示这个问题,我将下面的代码简化为基本内容。这段代码在谷歌日历中提供了一个带有按钮的侧边栏。当您单击该按钮时,它应该在您在代码中指定的日历上安装一个onEventUpdated触发器

Code.gs:

//Attempting to install an onEventUpdated - it appears I can't use 'forUserCalendar' when installing as a user, but can when I manually select 'installTrigger()' function in the code editor?

//Functionality/scopes based on those demonstrated here:
//https://developers.google.com/gsuite/add-ons/calendar/conferencing/sync-calendar-changes
//https://developers.google.com/gsuite/add-ons/calendar/conferencing/conferencing-sample?hl=fr#add-on-manifest

function onHomePageOpen(e) {
    let newButton = CardService.newTextButton().setText('Try install sync').setOnClickAction(CardService.newAction().setFunctionName('installTrigger'))
    let card = CardService.newCardBuilder()
        .setHeader(CardService.newCardHeader()
            .setTitle("<h1>Test install sync</h1>"))
        .addSection(CardService.newCardSection().addWidget(newButton))
        .build()
    return card
}

function installTrigger(e) {
    var triggerBuilder = ScriptApp.newTrigger('syncupdate');
    var triggerBuilder2 = triggerBuilder.forUserCalendar('<<YOUR EMAIL IN HERE>>').onEventUpdated().create()
    console.log('triggerBuilder2', triggerBuilder2)
}

function syncupdate() {
    console.log('triggered on update of an event!');
}
这似乎是一个错误! 我自己对此进行了测试,结果显示,无论提供的日历ID是否为硬编码字符串
”user@example.com“
,从
会话.getActiveUser().getEmail()
检索,或使用关键字
“primary”
,此错误仍然存在

我相信你知道,但对于未来的读者来说,已经有一份关于谷歌问题追踪器的报告详述了同样的行为:

谷歌似乎确实知道这个问题(这个问题在撰写本答案时已经被分配)。有趣的是,这似乎在过去有过报道,但在2020-08-03年被标记为固定

如果你还没有,我建议你去☆ 在之前链接的两个bug的左上角的问题编号旁边,因为这让谷歌知道有更多的人遇到了这个问题,因此它更有可能更快地被发现


我知道这通常是个坏消息,但我希望这对你有帮助

谢谢你的确认。谷歌问题追踪报告是我的!
{
  "timeZone": "Pacific/Auckland",
  "dependencies": {},
  "oauthScopes": [
    "https://www.googleapis.com/auth/calendar",
    "https://www.googleapis.com/auth/calendar.readonly",
    "https://www.googleapis.com/auth/calendar.events",
    "https://www.googleapis.com/auth/calendar.events.readonly",
    "https://www.google.com/calendar/feeds",
    "https://www.googleapis.com/auth/script.locale",
    "https://www.googleapis.com/auth/calendar.addons.current.event.read",
    "https://www.googleapis.com/auth/calendar.addons.execute",
    "https://www.googleapis.com/auth/calendar.addons.current.event.write",
    "https://www.googleapis.com/auth/script.scriptapp"
  ],
  "exceptionLogging": "STACKDRIVER",
  "runtimeVersion": "V8",
  "addOns": {
    "common": {
      "name": "Test Install Sync",
      "logoUrl": "https://www.gstatic.com/companion/icon_assets/tasks2_2x.png",
      "layoutProperties": {
        "primaryColor": "#123763",
        "secondaryColor": "#1a73e8"
      },
      "useLocaleFromApp": true
    },
    "calendar": {
      "homepageTrigger": {
        "runFunction": "onHomePageOpen"
      },
      "currentEventAccess": "READ_WRITE"
    }
  }
}