最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Events appearing in wrong Google Calendar - Stack Overflow

matteradmin5PV0评论

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

Post a comment

comment list (0)

  1. No comments so far