Events appearing in wrong Google Calendar - Stack Overflow

I'm trying to mass create events into a Google Calendar using the Google Calendar API. I've b

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745636223a4637407.html

相关推荐

  • Events appearing in wrong Google Calendar - Stack Overflow

    I'm trying to mass create events into a Google Calendar using the Google Calendar API. I've b

    3小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信