开发者问题收集

未找到脚本函数

2021-02-24
316

我正在尝试向 google 电子表格编辑器的界面添加一个自定义菜单,以激活 2 个脚本。

“scheduleShifts”运行良好,但“clearCalendar”提示未找到脚本函数:clearCalendar 错误。

我尝试进行一些修改,但仍然出现相同的错误。

提前感谢您的所有帮助。

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
}
}
1个回答

由于缺少 ,您的 clearCalendar 函数似乎位于 onOpen 中, ,这是预料之中的吗?

修复:

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();
      }
    });
  }
}

注意:

  • 我还删除了上面重复的 scheduleShifts
  • 添加了上面的 clearCalendar 代码

输出:

output

NightEye
2021-02-24