Pergunta

Hi im using Apps Script to change calendar-events in the domains calendar resources. It works fine but i have to add each calendar to a variable (mycal, mycal2, mycal3,).

Could this be done better?

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);
  }

}
Foi útil?

Solução

An alternative is to create an Array of calendar IDs and iterate through it:

(The benefits of this is that you can later create a sheet with the IDs and read them in, avoiding having to hardcode them in, and possibly hand it off to someone who doesnt know how to code but can update a sheet)

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);
      }
   }
}

An even better alternative is to create another function to handle the "heavy lifting":

(this makes the code more readable and easier to reuse / modify later)

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]);
   }
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top