简体   繁体   中英

Export events to Multiple Google Calendars from Google Spreadsheet

I need the script to export events from my spreadsheet to their respective calendars (column J), without duplicates. I have the below script, but haven't been able to get it to export events to my various calendars.

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

I added the var destRow, with the Loggers, and in the var calendarName for the getRange() I added (Number(destRow), 10).

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