【问题标题】:Export events to Multiple Google Calendars from Google Spreadsheet从 Google 电子表格将事件导出到多个 Google 日历
【发布时间】:2015-03-03 19:34:07
【问题描述】:

我需要脚本来将我的电子表格中的事件导出到它们各自的日历(J 列)中,而不需要重复。我有以下脚本,但无法将事件导出到我的各种日历中。

var scriptProperties = PropertiesService.getScriptProperties();// Set multiple script properties in one call.
scriptProperties.setProperties({
    'CF Library': 'cflibrary@efriends44221.org',
    'Arts/Crafts': 'efriends44221.org_a63hoveiu9jqisb826bllsjkp8@group.calendar.google.com',
    'Computers & Technology': 'efriends44221.org_mvov2vcook8amqltimt67i5lb0@group.calendar.google.com',
    'Library Closed': 'efriends44221.org_e4lvfr74rchoanhaqkmh8evo4c@group.calendar.google.com',
    'Story Time': 'efriends44221.org_6hc52m954fumu33o8tiob03mhs@group.calendar.google.com',
    'Board Meeting': 'efriends44221.org_g9dkgvpoenbpakshoaj7i84bj8@group.calendar.google.com',
    'Continuing Education': 'efriends44221.org_aur9f7fgj401biph9370l7vdp8@group.calendar.google.com',
    'Movies': 'efriends44221.org_c7v1c9hs041ogprqasaguc6cq4@group.calendar.google.com',
    'Summer Reading Program': 'efriends44221.org_r7nfru4r624a41ifqu2n540qss@group.calendar.google.com',
    'Book Sale': 'efriends44221.org_fr19jra74rs5ia4i4ortd44sgg@group.calendar.google.com',
    'Exhibit': 'efriends44221.org_151qtt5afnr7hs6fae4g05vrjg@group.calendar.google.com',
    'Music': 'efriends44221.org_uo2j384ldonbasja8drpl8520k@group.calendar.google.com',
    'Tax Preparation': 'efriends44221.org_lhdtbjunpjqtn52k9o5gkgol5k@group.calendar.google.com',
    'Business & Career': 'efriends44221.org_gboa7mo6i9uvothv4s1q929ne8@group.calendar.google.com',
    'Health & Wellness': 'efriends44221.org_gh97a27i0bqc93hndpr08435uc@group.calendar.google.com',
    'Non-Profit & Community': 'efriends44221.org_mfm9u656llmtubnvvm8h2ehjak@group.calendar.google.com',
    'Workshop/Class': 'efriends44221.org_gbarc152o2h55c67fc8cpshqfo@group.calendar.google.com',
    'Club/Discussion Group': 'efriends44221.org_c2a7vkhh3hlvlh9ijkdlcqin1s@group.calendar.google.com',
    'History': 'efriends44221.org_rnto9utgss0h2ltb83hp203634@group.calendar.google.com',
    'Special Event': 'efriends44221.org_7u2tqcktujiautq2qes591p8tk@group.calendar.google.com'});

function ExportEvents() {
  var sheet = ss.getSheetByName("Events");
  var headerRows = 1;
  var dataRange = sheet.getDataRange();
  var data = dataRange.getValues();
  var calendarName = sheet.getRange('J:J').getValue();
  var calendarId = ScriptProperties.getProperty(calendarName);
  var cal = CalendarApp.getCalendarById(calendarId);
  var timeZone = cal.getTimeZone();
  ss.setSpreadsheetTimeZone(timeZone);
  for (i in data) {
    if (i < headerRows) continue;
    var row = data[i];
    var date = new Date(row[0]);
    var title = row[1];           
    var tstart = setTimeToDate(date,row[2]);
    var tstop = setTimeToDate(date,row[3]);
    Logger.log('date = '+date+'tstart = '+tstart+'  tstop = '+tstop);
    var location = row[4];
    var description = row[5];
    var type = row[6];
    var times = row[7];
    var enddate = row[8];
    var calname = row[9];
    var id = row[10];
    var status = row[11];
    try {
      var event = cal.getEventSeriesById(id);
      event.setTitle('---');
    }catch(e){
      if (calname = 'CF Library') {
        var newEvent = cal.createEvent(title, tstart, tstop,      {description:description,location:location});// create a "normal" event
      }else if (calname = 'Arts/Crafts') {
      var newEvent = cal.createEvent(title, tstart, tstop,{description:description,location:location});
      }else if (calname = 'Computers & Technology') {
        var newEvent = cal.createEvent(title, tstart, tstop, {description:description,location:location});
      }else if (calname = 'Library Closed') {
      var newEvent = cal.createEvent(title, tstart, tstop, {description:description,location:location});
      }else if (calname = 'Story Time') {
      var newEvent = cal.createEvent(title, tstart, tstop, {description:description,location:location});
      }
  row[10] = newEvent.getId();  // Update the data array with event ID
  Logger.log('Event Created');// while debugging
  var event = cal.getEventSeriesById(row[10]);// make it an event Series
}
event.setTitle(title);
event.setDescription(desc);
event.setLocation(loc);
if(type=='PM'){ // Per Month
  var recurrence = CalendarApp.newRecurrence().addMonthlyRule().times(times)
  event.setRecurrence(recurrence, tstart, tstop);// we need to keep start and stop otherwise it becomes an AllDayEvent if only start is used
}else if(type=='PW'){ // Per Week
  var recurrence = CalendarApp.newRecurrence().addWeeklyRule().times(times)
  event.setRecurrence(recurrence, tstart, tstop);
}else if(type=='PD'){ //Per Day
  var recurrence = CalendarApp.newRecurrence().addDailyRule().times(times)
  event.setRecurrence(recurrence, tstart, tstop);
}else if(type=='PY'){ //Per Year
  var recurrence = CalendarApp.newRecurrence().addYearlyRule().times(times)
  event.setRecurrence(recurrence, tstart, tstop);
}else if(type=='MCD'){//MCD is Monthly on a certain day.
  var recurrence = CalendarApp.newRecurrence().addMonthlyRule().onlyOnMonthDay(times);// Creates a rule that recurs every month on a certain day of the month.
  event.setRecurrence(recurrence, tstart, tstop);
}else if(type=='TTH'){//TTH is 2 days per week - Tues, Thurs.
  var recurrence = CalendarApp.newRecurrence().addWeeklyRule().onlyOnWeekdays([CalendarApp.Weekday.TUESDAY, CalendarApp.Weekday.THURSDAY]).until(new Date(enddate));
  event.setRecurrence(recurrence, tstart, tstop);
}else if(type=='MWF'){//MWF is 3 days per week - Mon, Weds, Fri.
  var recurrence = CalendarApp.newRecurrence().addWeeklyRule().onlyOnWeekdays([CalendarApp.Weekday.MONDAY, CalendarApp.Weekday.WEDNESDAY, CalendarApp.Weekday.FRIDAY]).until(new Date(enddate));
  event.setRecurrence(recurrence, tstart, tstop);
}
  data[i] = row ;
  }
dataRange.setValues(data);

 for (i in data) {
    if (i < headerRows) continue;
      var sheet = SpreadsheetApp.getActiveSheet();
      var startRow = 1;// Is the start row & also the header row.
      var dataR = sheet.getDataRange();
      var data = dataR.getValues();
      var rowNum = startRow;
      var destRow = Number(startRow)+Number(i);
        Logger.log(startRow+' '+i+' '+(startRow+i))
        Logger.log(startRow+' '+i+' '+(Number(startRow)+Number(i)))
       sheet.getRange(Number(++destRow)-1, 12).setValue("Event Exported");
   SpreadsheetApp.flush();
  }
}

【问题讨论】:

  • 有错误吗?如果有错误,它在哪一行?如果没有错误,您是否使用过调试器、单步调试代码并检查每一行和变量是否符合您的期望以及返回的内容? Breakpoints and Debugger
  • 是的,它以第 8 行的 Typeerror (getCalendarById(calendarId)。第 32 行(var newEvent = cal.createEvent...)。
  • 我找不到您的代码有任何问题。您是否不止一次设置了ScriptProperties?我想知道您的代码中的其他地方是否存在冲突。你有插件吗?图书馆?有什么可能与此代码冲突吗?在函数名称中搜索具有相同名称的另一个函数名称。我不知道。发生了一些奇怪的事情。
  • 它出现的唯一地方是我定义 var scriptProperties 的地方,就在我设置属性的地方,然后我在函数 ExportEvents() 中引用它的地方。
  • @Administrator 问题似乎您在ExportEvents() 中使用ScriptProperties 和大写S,但您已将scriptProperties 定义为小s。 GAS 区分大小写。

标签: google-apps-script google-sheets google-calendar-api


【解决方案1】:
var destRow = Number(headerRows)+Number(i);
    Logger.log(headerRows+' '+i+' '+(headerRows+i))
    Logger.log(headerRows+' '+i+' '+(Number(headerRows)+Number(i)))
var calendarName = sheet.getRange(Number(destRow), 10).getValue();

我使用 Loggers 添加了 var destRow,并在我添加的 getRange() 的 var calendarName 中添加了 (Number(destRow), 10)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-25
    • 1970-01-01
    相关资源
    最近更新 更多