创建日历后立即设置日历触发器

问题描述 投票:0回答:1

所以我尝试在最近创建的日历上设置 onEventUpdated 触发器。该函数为该日历创建一个工作表、一个日历和一个触发器,但由于最近创建了日历,因此无法创建触发器,并且我收到错误“异常:没有 acceso al calendario”(很抱歉我的母语是西班牙语,但这可以翻译为“例外:您无权访问日历”)。

我尝试用 Utilities.sleep 解决这个问题,但它必须等待 15 秒才能正常运行,我认为时间太多了。您对此还有其他解决方案吗?我来贴一下我的代码

const ui = SpreadsheetApp.getUi();
const SS = SpreadsheetApp.getActiveSpreadsheet();

function addPerson() {
  const personPrompt = ui.prompt('TITLE','Por favor ingrese el nombre de la persona a añadir.', ui.ButtonSet.OK_CANCEL);
  const personName = personPrompt.getResponseText();
  const calendar = CalendarApp.createCalendar(personName);
  const calendarID = calendar.getId();
  const sheet = SS.insertSheet(personName, SS.getSheets().length);
  Utilities.sleep(15000); //Here is the workaround that I found
  ScriptApp.newTrigger('masterFunction')
    .forUserCalendar(calendarID)
    .onEventUpdated()
    .create();
}

我删除了一些与问题无关的行,希望你能帮忙。谢谢

google-apps-script runtime google-calendar-api eventtrigger
1个回答
0
投票

这对我有用:

创建这个:

function myfunk() {
  Logger.log("I am here.")
}

先运行:

function createACalendar() {
  const ui = SpreadsheetApp.getUi();
  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const calendar = CalendarApp.createCalendar("Calendar1");
  const calendarID = calendar.getId();
  PropertiesService.getScriptProperties().setProperty("calid",calendarID);
}

运行下一步:

function createTheTrigger() {
  let calendarID = PropertiesService.getScriptProperties().getProperty("calid");
  ScriptApp.newTrigger('myfunk')
    .forUserCalendar(calendarID)
    .onEventUpdated()
    .create();
}

然后这个:

function myfunction() {
  let calendarID = PropertiesService.getScriptProperties().getProperty("calid");
  let cal = CalendarApp.getCalendarById(calendarID);
  let st = new Date();
  let ed = new Date();
  ed.setHours(23);
  cal.createEvent("Event1",st,ed);
}
© www.soinside.com 2019 - 2024. All rights reserved.