Home > Blockchain >  CalendarApp.createEvent fails due to permission issue
CalendarApp.createEvent fails due to permission issue

Time:12-15

I have a google spread sheet, and a script. Whenever something is changes in the sheet, I want a meeting to be created in my calendar. So I write the following script:



function onEdit() {
  let meetingTimeMilis = 1000 * 60 * 15;
  let startTime = new Date();
  let endTime = new Date(startTime.getTime()   meetingTimeMilis);

  let eventCal = CalendarApp.getCalendarById(Session.getActiveUser().getEmail());
  eventCal.createEvent('event', startTime, endTime);
}

The first time this happens, it asks me to allow this, so I grand all the premonitions, but still I get this error:

Exception: The script does not have permission to perform that action. Required permissions: (https://www.googleapis.com/auth/calendar || https://www.googleapis.com/auth/calendar.readonly || https://www.google.com/calendar/feeds)
    at onEdit(notifyme:84:5)

How can I grant this permission too, of bypass this?

CodePudding user response:

In theory something as simple as this could work but I think you're going to have to do a little more thinking about the organization of the sheet. As it is now I just copied your code. But it probably work work as it is now but I would need to know more about your sheet organization. Or you could learn more about how to create an onEdit function which would be the best way to go.

function onMyEdit(e) {
  const sh = e.range.getSheet()
  if (sh.getName() == 'Your Sheet Name') {
    let meetingTimeMilis = 1000 * 60 * 15;
    let startTime = new Date();
    let endTime = new Date(startTime.getTime()   meetingTimeMilis);
    let eventCal = CalendarApp.getCalendarById(Session.getActiveUser().getEmail());
    eventCal.createEvent('event', startTime, endTime);
  }
}

function createtrigger() {
  if(ScriptApp.getProjectTriggers().filter(t => t.getHandlerFunction() == "onMyEdit").length == 0) {
    ScriptApp.newTrigger('onMyEdit').forSpreadsheet(SpreadsheetApp.getActive()).onEdit().create();
  }
}

CodePudding user response:

installable trigger solved this issue

  • Related