Google Calendar App Script to pull Data into a Spreadsheet periodically

≯℡__Kan透↙ 提交于 2020-01-22 16:30:26

问题


I've been using this script to pull Data from Google Calendar into a spreadsheet. I have a couple questions for improvements:

  1. Is it possible to have this pull data from multiple calendars to the same spreadsheet without them overwriting each other?
  2. Is it possible to have it add a new column of data to the spreadsheet that marks which calendar each row came from?
  3. Also, how would you go about setting this up to run automatically every 24 hours?

THANKS!

function caltest3(){
  var ss = SpreadsheetApp.openById("SPREADSHEET ID");
  SpreadsheetApp.setActiveSpreadsheet(ss);
  var cal=CalendarApp.getCalendarById("CALENDAR ID");
  var sheet = SpreadsheetApp.getActiveSheet();

  var events = cal.getEvents(new Date("January 1, 2013"), new Date("January 13, 2013"));
for (var i=0;i<events.length;i++) {
  //http://www.google.com/google-d-s/scripts/class_calendarevent.html
      var details=[[events[i].getTitle(), events[i].getStartTime(),            
                    events[i].getEndTime(), events[i].getDescription(),
                    events[i].getLocation()]];
  var row=i+1;
  var range=sheet.getRange(row+1,1,1,5);
  range.setValues(details);
    }
  }
}

回答1:


Short answers then an example:

  1. You cannot do this with a single call to the CalendarApp, but if you loop through a list of calendarIds and add the resultant events for that calendar to an array, you can then sort this array and put this to the spreadsheet
  2. Simply add the calendar name (or id) as another item in your details array, just remember to increase your range column count by 1
  3. Add a script trigger to whatever period you want, the documentation describes this best.

Whether you are building an array of calendar events from multiple calendars or just one, adding them to an array in a loop and then writing them to the spreadsheet outside any loop is better practice than writing row by row as every time you call the getRange() & setValues methods they are separate API calls and these calls are most expensive in time for Apps Script. Call each only once with a range to suit your data and your script will run an order of magnitude quicker.

Script below illustrates answers 1 & 2. Your needs may vary if you are setting a timer to this as you may be wanting to affect the period of time you are querying?

function caltest3(){

  var ss = SpreadsheetApp.openById( 'spreadsheetId' ),
      sheet = ss.getSheetByName( 'sheetName' ),
      cals = ['id1', 'id2', 'id3'], c, cal, calName,
      start = new Date( 'whenever' ), end = new Date( 'whenever' ),
      events, i, details,
      eventslog = [], e,
      rows = [], range;

  for (c = 0; c < cals.length; c += 1) {

    cal = CalendarApp.getCalendarById(cals[c]);
    calName = cal.getTitle();
    events = cal.getEvents(start, end);

    // add the events of the current calendar to the array of all events
    eventslog = eventslog.concat(
      events.map(function(event) {
        return {
          time: new Date(event.getStartTime()).getTime(), // sort by this
          details: [
            event.getTitle(),
            event.getStartTime(),
            event.getEndTime(),
            event.getDescription(),
            event.getLocation(),
            calName // change calendar info position in array to suit
          ]
        };
      })
    );
  }

  // sort array of event so date order can be either way by reversing a & b
  eventslog.sort(function(a, b) { return a.time - b.time; });

  rows = eventslog.map(function(entry) { return entry.details; });

  range = sheet.getRange(2, 1, rows.length, 6);
  range.setValues(rows);
}

code spaced out for clarity



来源:https://stackoverflow.com/questions/14415752/google-calendar-app-script-to-pull-data-into-a-spreadsheet-periodically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!