Hi-im使用Apps Script更改域日历资源中的日历事件。它工作正常,但我必须将每个日历添加到一个变量(mycal,mycal2,mycal3,)中。
这能做得更好吗?
function getCalendars() {
var mycal = 'comp.com_2d3634303139333138363136@resource.calendar.google.com';
var cal = CalendarApp.getCalendarById(mycal);
var events = cal.getEvents(new Date('June 1, 2010 00:00:00 CST'), new Date('June 9, 2016 23:59:59 CST'), {visibility: 'default' || 'public'});
for (var i=0;i<events.length;i++) {
events[i].setVisibility(CalendarApp.Visibility.PRIVATE);
}
var mycal2 = 'comp.com_3838393135313632343833@resource.calendar.google.com';
var cal2 = CalendarApp.getCalendarById(mycal2);
var events = cal2.getEvents(new Date('June 1, 2010 00:00:00 CST'), new Date('June 9, 2016 23:59:59 CST'), {visibility: 'default' || 'public'});
for (var i=0;i<events.length;i++) {
events[i].setVisibility(CalendarApp.Visibility.PRIVATE);
}
var mycal3 = 'comp.com_3838393135313632343833@resource.calendar.google.com';
var cal3 = CalendarApp.getCalendarById(mycal3);
var events = cal2.getEvents(new Date('June 1, 2010 00:00:00 CST'), new Date('June 9, 2016 23:59:59 CST'), {visibility: 'default' || 'public'});
for (var i=0;i<events.length;i++) {
events[i].setVisibility(CalendarApp.Visibility.PRIVATE);
}
}
另一种选择是创建一个日历ID数组并对其进行迭代:
(这样做的好处是,您可以稍后创建一个带有ID的工作表并读取它们,避免了对它们进行硬编码,并可能将其交给不知道如何编码但可以更新工作表的人)
function getCalendars() {
var mycal = new Array(); // Create an Array for the calendar IDs
mycal = ['comp.com_2d3634303139333138363136@resource.calendar.google.com',
'comp.com_3838393135313632343833@resource.calendar.google.com',
'comp.com_3838393135313632343833@resource.calendar.google.com'];
for (var i=0; i < mycal.length; i++) {
var cal = CalendarApp.getCalendarById(mycal[i]);
var events = cal.getEvents(new Date('June 1, 2010 00:00:00 CST'), new Date('June 9, 2016 23:59:59 CST'), {visibility: 'default' || 'public'});
for (var i=0;i<events.length;i++) {
events[i].setVisibility(CalendarApp.Visibility.PRIVATE);
}
}
}
一个更好的选择是创建另一个功能来处理"繁重的工作":
(这使代码更可读,以后更容易重用/修改)
function makePrivate(calId) {
var cal = CalendarApp.getCalendarById(calId);
var events = cal.getEvents(new Date('June 1, 2010 00:00:00 CST'), new Date('June 9, 2016 23:59:59 CST'), {visibility: 'default' || 'public'});
for (var i=0;i<events.length;i++) {
events[i].setVisibility(CalendarApp.Visibility.PRIVATE);
}
}
function getCalendars() {
var mycal = new Array();
mycal = ['comp.com_2d3634303139333138363136@resource.calendar.google.com',
'comp.com_3838393135313632343833@resource.calendar.google.com',
'comp.com_3838393135313632343833@resource.calendar.google.com'];
for (var i=0; i < mycal.length; i++) {
makePrivate(mycal[i]);
}
}