Zieyra
Zieyra

Reputation: 5

Script function not found

I am trying to add a custom menu to the google spreadsheet editor's interface to activate 2 scripts.

"scheduleShifts" is running fine, but "clearCalendar" prompted Script function not found: clearCalendar error.

I have tried making some modifications but the same error still appears.

Thank you in advance for all your help.

function scheduleShifts() {
    var spreadsheet = SpreadsheetApp.getActiveSheet();
    var calendarID = spreadsheet.getRange("C1").getValue();
    var eventCal = CalendarApp.getCalendarById("calendarID");
    var signups = spreadsheet.getRange("A2:C3").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);
    }
}
function onOpen() {
    var ui = SpreadsheetApp.getUi();
    ui.createMenu('Sync to Calendar')
      .addItem('Schedule shifts', 'scheduleShifts')
      .addSeparator()
      .addItem('Delete shifts from Calendar','clearCalendar')
      .addToUi();
function scheduleShifts() {
    // Magic happens here, connecting this Google Sheet
    // with a Google Calendar.
}
function clearCalendar() {
    // This one removes all of the shifts from the event
}
}

Upvotes: 0

Views: 328

Answers (1)

NightEye
NightEye

Reputation: 11184

It seems that your clearCalendar function was inside your onOpen due to lack of }, was this expected?

Fix:

function onOpen() {
    var ui = SpreadsheetApp.getUi();
    ui.createMenu('Sync to Calendar')
      .addItem('Schedule shifts', 'scheduleShifts')
      .addSeparator()
      .addItem('Delete shifts from Calendar','clearCalendar')
      .addToUi();
}
function scheduleShifts() {
    var spreadsheet = SpreadsheetApp.getActiveSheet();
    var calendarID = spreadsheet.getRange("C1").getValue();
    var eventCal = CalendarApp.getCalendarById("calendarID");
    var signups = spreadsheet.getRange("A2:C3").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);
    }
}
function clearCalendar() {
  // This one removes all of the shifts from the event
  var spreadsheet = SpreadsheetApp.getActiveSheet();
  var calendarID = spreadsheet.getRange("C1").getValue();
  var eventCal = CalendarApp.getCalendarById(calendarID);
  var signups = spreadsheet.getRange("A2:C3").getValues();
  for (x=0; x<signups.length;x++)
  {
    var shift = signups[x];
    var startTime = shift[0];
    var endTime = shift[1];
    var volunteer= shift[2];
    // get the list of events matching your details from the sheet
    var events = eventCal.getEvents(startTime, endTime, {search: volunteer});
    // loop all matches
    events.forEach(function (event){
      // extra check, title needs to be equal to volunteer
      if (event.getTitle() == volunteer) {
        // delete if all conditions are met
        event.deleteEvent();
      }
    });
  }
}

Note:

  • I also removed the duplicate scheduleShifts above
  • Added clearCalendar code above

Output:

output

Upvotes: 1

Related Questions