简体   繁体   中英

How do I fix this script in Google Apps Script to create a calendar event with invites?

I'm linking a script to a button called "Send to Calendar" that will be on each sheet in my worksheet. I want the script to:

1) Create a new event on a google calendar, basing its name and dates on the information on the sheet where the button is located (different data per sheet, all located at the same cell addresses).
2) Set a reminder for all attendees 6 weeks in advance of the start date.
3) Invite/Add guests (possibly by sending the event as an email to a non-gmail address? I'm flexible on how this looks, so long as I can automatically invite the guests.)
4) Set the colorId of all these invites to "3" (mauve), or anything other than the default event color. (This is nice, but optional.)

I've been cobbling the script together from Google's examples and some answers I've found on this site, but none quite do what I'd like. I also enabled Google Advanced Services ( https://developers.google.com/apps-script/guides/services/advanced ) because I read that the problem might be in the Google API permissions, but it's been a few hours since I enabled it and things are still not working. I'm not sure whether that made a difference.

This is close, I think, but it returns a 404 error and I don't know why.

function createEvent() {
  var vSS = SpreadsheetApp.getActiveSpreadsheet();
  var vS = vSS.getActiveSheet(); 
  var vEventName = vS.getRange("M5").getValue();
  var start = vS.getRange("M6").getValue();
  var end = vS.getRange("H3").getValue();
var calendar = CalendarApp.getCalendarById(
 'example@gmail.com');
var event = calendar.createEvent; {vEventName;
 new Date(start);
 new Date(end);                             
 attendees: [
      {email: 'example@example.com'},
      {email: 'example@example.com'}
    ];
    colorId: 3
  };
  event = Calendar.Events.insert(event, calendar);
  Logger.log('Event ID: ' + event.id);
}

This creates an event on my calendar successfully, but doesn't add guests, create the reminder, or change the color. (Found that here: Google Apps Script - Creating Calendar Events )

function createEvent() {
var vSS = SpreadsheetApp.getActiveSpreadsheet();
var vS = vSS.getActiveSheet();  
var vStartDate = vS.getRange("M5").getValue();
var vEndDate = vS.getRange("M6").getValue();  
var vEventName = vS.getRange("H3").getValue();
var calendar = CalendarApp.getCalendarById(
 'example@gmail.com');
var event =  calendar.createEvent(vEventName,
 new Date(vStartDate),
 new Date(vEndDate));
Logger.log('Event ID: ' + event.getId());
}

Any help is appreciated, especially if you're able to add some "//" explanations about how parts of the code work. I'm new to scripts and trying to teach myself. Thanks for looking!

Consider the following code:

function createEvent() {
  var vSS = SpreadsheetApp.getActiveSpreadsheet();
  var vS = vSS.getActiveSheet(); 
  var vEventName = vS.getRange("M5").getValue();
  // M6 and H3 cells should contain valid dates
  var start = vS.getRange("M6").getValue();
  var end = vS.getRange("H3").getValue();
  var calendar = CalendarApp.getCalendarById('example@gmail.com');
  // Creating event with options (guests list)
  var event = calendar.createEvent(vEventName, start, end, {
    // a comma-separated list of email addresses that should be added as guests
    guests: 'guest1@gmail.com,guest2@mail.ru',
    sendInvites: true
  });
  // Set desired color to the created event
  event.setColor(CalendarApp.EventColor.MAUVE);

  Logger.log('Event ID: ' + event.id);
}

We have used creating event with options with a subsequent event coloring. It works for me (e-mails have been changed).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM