Home > OS >  Access rights missing for CalendarApp - createEvent?
Access rights missing for CalendarApp - createEvent?

Time:08-02

This is the full code, which works fine as long as the mail account in the "Form" sheet is the same as the account I am logged in with (using Google workspace) - Mail is of the form [[email protected]]. This code is called via a spreadsheet. So the question is: How do I logged in as account "A" get to writ to the calendar of account "B"? If that isn't possible is there an alternative way to have any account execute this code to an "open" calendar? What would be needed then?

I am getting the error message: "TypeError: Cannot read property 'createEvent' of null"

function connectToCalendar()
{
  var sheetForm = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Form");
  var calendarId = sheetForm.getRange("B1").getValue();
  return CalendarApp.getCalendarById(calendarId);
}

function addEvents() {
  // Magic happens here, connecting this Google Sheet
  // with a Google Calendar.


  //Must be called before the connectToCalendar function
  var spreadSheet = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = spreadSheet.getSheets()[2];
  Logger.log(sheet.getName());

  var eventCalendar = connectToCalendar();
  
  var events = sheet.getRange(2,2,sheet.getLastRow()-1,4).getValues();

  for(x=0; x < events.length; x  )
  {
    var event = events[x];

    var statTime = event[0];
    var endTime = event[1];
    var title = event[2];

    eventCalendar.createEvent(title, statTime, endTime);

    if(title == ""){
      break;
    }
  }
}

CodePudding user response:

  1. Have you tried using a super administrator account ? Because super admins have access to all the calendars and they should be able to modify them
  2. if you want to do it using a regular user, you will need to use a service account with domain-wide delegation, that way you can impersonate your users with the service account and have access to your users' calendars. On this link you can check the library that you will need to use for it https://github.com/googleworkspace/apps-script-oauth2

CodePudding user response:

In this scenario the error returned is:

TypeError: Cannot read property 'createEvent' of null

This is because the calendar is inaccessible to that user. To make it accessible you should share the calendar with that particular user.

This answer is based on Rubén's comment, but let me know below if you need further help.

  • Related