您好我正在使用Apps脚本更改域日历资源中的日历事件。它工作正常,但我必须将每个日历添加到变量(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);
}
}
答案 0 :(得分:2)
另一种方法是创建一个日历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]);
}
}