问题
I've been using this script to pull Data from Google Calendar into a spreadsheet. I have a couple questions for improvements:
- Is it possible to have this pull data from multiple calendars to the same spreadsheet without them overwriting each other?
- Is it possible to have it add a new column of data to the spreadsheet that marks which calendar each row came from?
- 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:
- 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 - Simply add the calendar name (or id) as another item in your
details
array, just remember to increase your range column count by1
- 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