admin管理员组

文章数量:1434949

I'm trying to mass create events into a Google Calendar using the Google Calendar API. I've been able to have to events appear from my google sheet into my main personal calendar, but not in the specified calendar. I want to be able to share just these events instead of sharing my entire personal calendar later . Here's my code from google apps script.

function create_Events(){

  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName("Sheet1");
  var last_row = sheet.getLastRow();
  var data = sheet.getRange("B1:D" + last_row).getValues();
  var cal = CalendarApp.getCalendarById("[email protected]");
  //Logger.log(data);

  for(var i = 0;i< data.length;i++){
    //index 0 =
    var event = CalendarApp.getDefaultCalendar().createEvent(data[i][0],
    new Date(data[i][1]),
    new Date(data[i][2]),
    {location: data[i][3]});
 Logger.log('Event ID: ' + event.getId());

    
  }

}

I copied the Calendar ID from the calendar settings, expecting the events to populate in that calendar. Instead they populated into the calendar tied to my gmail account

I'm trying to mass create events into a Google Calendar using the Google Calendar API. I've been able to have to events appear from my google sheet into my main personal calendar, but not in the specified calendar. I want to be able to share just these events instead of sharing my entire personal calendar later . Here's my code from google apps script.

function create_Events(){

  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName("Sheet1");
  var last_row = sheet.getLastRow();
  var data = sheet.getRange("B1:D" + last_row).getValues();
  var cal = CalendarApp.getCalendarById("[email protected]");
  //Logger.log(data);

  for(var i = 0;i< data.length;i++){
    //index 0 =
    var event = CalendarApp.getDefaultCalendar().createEvent(data[i][0],
    new Date(data[i][1]),
    new Date(data[i][2]),
    {location: data[i][3]});
 Logger.log('Event ID: ' + event.getId());

    
  }

}

I copied the Calendar ID from the calendar settings, expecting the events to populate in that calendar. Instead they populated into the calendar tied to my gmail account

Share Improve this question asked Nov 17, 2024 at 12:08 user28338128user28338128 11 silver badge1 bronze badge
Add a comment  | 

1 Answer 1

Reset to default 1

Create Google event in Calendar

You can try this Code:

function create_Events() {

  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName("Sheet1");
  var last_row = sheet.getLastRow();
  var data = sheet.getRange("B1:D" + last_row).getValues();
  var cal = CalendarApp.getCalendarById("Your calendar ID");


  for (var i = 0; i < data.length; i++) {
    var event = CalendarApp.getCalendarById(cal).createEvent(data[i][0],
      new Date(data[i][1]),
      new Date(data[i][2]),
      { location: data[i][3] });
    Logger.log('Event ID: ' + event.getId());


  }

}

I modified this section of your code:

var event = CalendarApp.getDefaultCalendar().createEvent(data[i][0],
  new Date(data[i][1]),
  new Date(data[i][2]),
  { location: data[i][3] });

When running the code, the default calendar is accessed. You need to open the calendar by its ID where you plan to work.

Reference:

Calendar - GetCalendarbyID

本文标签: Events appearing in wrong Google CalendarStack Overflow