0

I am very new to Script editing. I have a Spreadsheet from a source online that I replicated in order to add events for students.

Screenshot Example

I want to be able to add the Description and then change the event color depending on the class. The script that I am using is copied below. I don't know how to add in the description and color ID for the event.

function scheduleShifts() {
var spreadsheet = SpreadsheetApp.getActiveSheet();
    var calendarID = spreadsheet.getRange("C4").getValue();
    var eventCal = CalendarApp.getCalendarById("calendarId");
var signups = spreadsheet.getRange("A5:C7").getValues();
for (x=0; x<signups.length;x++)
{
    var shift = signups[x];
    var startTime = shift[0];
    var endTime = shift[1];
    var volunteer= shift[2];
    eventCal.createEvent(volunteer, startTime, endTime);
}
}
Rubén
  • 34,714
  • 9
  • 70
  • 166
pres10
  • 19
  • 7
  • Welcome to [so]. Please add a brief description of your search efforts as is suggested in [ask]. – Rubén Apr 30 '21 at 16:55
  • 1
    Related: https://stackoverflow.com/q/15788897/1595451, https://stackoverflow.com/q/34213957/1595451 – Rubén Apr 30 '21 at 16:56

1 Answers1

0

As mentioned by @Ruben you can use Calendar.createEvent(title, startTime, endTime, options) to create events with additional options like description, location, guests and sendInvites. It will return a CalendarEvent object where you can set your event color using CalendarEvent.setColor(color). You can check other methods available in CalendarEvent object for more information.

Sample Code:

function scheduleShifts() {
  var spreadsheet = SpreadsheetApp.getActiveSheet();
  var calendarID = spreadsheet.getRange("C4").getValue();
  var eventCal = CalendarApp.getCalendarById(calendarID);
  var signups = spreadsheet.getRange("A5:E7").getValues(); //modified, include column D and E

  
  for (x=0; x<signups.length;x++)
  {
    var shift = signups[x];
    var startTime = shift[0];
    var endTime = shift[1];
    var volunteer= shift[2];
    var desc= shift[3];
    var color = shift[4];
    var event = eventCal.createEvent(volunteer, startTime, endTime,{description:desc});

    //Check if color exist
    if(color){
      //Set event color
      event.setColor(CalendarApp.EventColor[color]);
    }
  }
}

Output:

enter image description here

Ron M
  • 5,791
  • 1
  • 4
  • 16