如何修改下面的函数以从多个日历中获取事件
How to modify function below to get events from multiple calendars
我正在使用以下函数将事件从 Google 日历提取到 Google Sheet,有没有办法修改它以从多个日历中提取事件。
function getEvents () {
var ss = SpreadsheetApp.openById("spreadsheetID").getSheetByName("Cal");
var cal = CalendarApp.getCalendarById("username@email.ca");
var events = cal.getEvents(new Date("4/1/2022 12:00 AM"), new Date("4/30/2022 11:59 PM"));
var lr = ss.getLastRow();
ss.getRange(2, 1, lr-1, 5).clearContent();
for(var i = 0;i<events.length;i++){
var title = events[i].getTitle();
var sd = events[i].getStartTime();
var ed = events[i].getEndTime();
var loc = events[i].getLocation();
var des = events[i].getDescription();
ss.getRange(i+2, 1).setValue(sd);
ss.getRange(i+2, 1).setNumberFormat("mm/dd/yyyy h:mm AM/PM")
ss.getRange(i+2, 2).setValue(ed);
ss.getRange(i+2, 3).setNumberFormat("mm/dd/yyyy h:mm AM/PM")
ss.getRange(i+2, 3).setValue(title);
ss.getRange(i+2, 4).setValue(loc);
ss.getRange(i+2, 5).setValue(des);
}
}
试试这个:
function getEvents() {
const ss = SpreadsheetApp.getActiveSheet();
var lr = ss.getLastRow();
if (lr > 0) {
ss.getRange(2, 1, lr - 1, 5).clearContent();
}
const ids = ['q289qp9augtnshkn70dkibcc54@group.calendar.google.com', 'jimesteban@jimesteban.com'];//cal ids
ids.forEach(id => {
let cal = CalendarApp.getCalendarById(id);
let events = cal.getEvents(new Date("4/23/2022 8:00 AM"), new Date("4/23/2022 5:00 PM"));
for (var i = 0; i < events.length; i++) {
var title = events[i].getTitle();
var sd = events[i].getStartTime();
var ed = events[i].getEndTime();
var loc = events[i].getLocation();
var des = events[i].getDescription();
ss.appendRow([sd,ed,title,loc,des])
}
})
}
我正在使用以下函数将事件从 Google 日历提取到 Google Sheet,有没有办法修改它以从多个日历中提取事件。
function getEvents () {
var ss = SpreadsheetApp.openById("spreadsheetID").getSheetByName("Cal");
var cal = CalendarApp.getCalendarById("username@email.ca");
var events = cal.getEvents(new Date("4/1/2022 12:00 AM"), new Date("4/30/2022 11:59 PM"));
var lr = ss.getLastRow();
ss.getRange(2, 1, lr-1, 5).clearContent();
for(var i = 0;i<events.length;i++){
var title = events[i].getTitle();
var sd = events[i].getStartTime();
var ed = events[i].getEndTime();
var loc = events[i].getLocation();
var des = events[i].getDescription();
ss.getRange(i+2, 1).setValue(sd);
ss.getRange(i+2, 1).setNumberFormat("mm/dd/yyyy h:mm AM/PM")
ss.getRange(i+2, 2).setValue(ed);
ss.getRange(i+2, 3).setNumberFormat("mm/dd/yyyy h:mm AM/PM")
ss.getRange(i+2, 3).setValue(title);
ss.getRange(i+2, 4).setValue(loc);
ss.getRange(i+2, 5).setValue(des);
}
}
试试这个:
function getEvents() {
const ss = SpreadsheetApp.getActiveSheet();
var lr = ss.getLastRow();
if (lr > 0) {
ss.getRange(2, 1, lr - 1, 5).clearContent();
}
const ids = ['q289qp9augtnshkn70dkibcc54@group.calendar.google.com', 'jimesteban@jimesteban.com'];//cal ids
ids.forEach(id => {
let cal = CalendarApp.getCalendarById(id);
let events = cal.getEvents(new Date("4/23/2022 8:00 AM"), new Date("4/23/2022 5:00 PM"));
for (var i = 0; i < events.length; i++) {
var title = events[i].getTitle();
var sd = events[i].getStartTime();
var ed = events[i].getEndTime();
var loc = events[i].getLocation();
var des = events[i].getDescription();
ss.appendRow([sd,ed,title,loc,des])
}
})
}