Reputation: 17
New to programming and I have been hitting my head against the wall when trying to add multiple calendar ID's to the script below. I need it to iterate through the rows and if the event hasn't been added to the calendar yet, add it to mine and to others' calendars, as the sheet gets updated.
function addToCal() {
var ss = SpreadsheetApp.getActiveSpreadsheet(),
sheet = ss.getSheetByName("ProductionSchedule"),
range = sheet.getActiveRange(),
row = range.getRow(),
data = sheet.getRange(row, 1, 1, 26).getValues(),
date = data[0][6];
if (date =='') return;
var today = new Date(),
startDate = new Date(today.setMonth(today.getMonth()-1)),
endDate = new Date(today.setMonth(today.getMonth()+6)),
calId = 'calendarID',
cal = CalendarApp.getCalendarById(calId),
events = cal.getEvents(startDate, endDate),
titles = [];
for (var i = 0, len = events.length; i < len; i++)
titles.push(events[i].getTitle());
var item = ('Produção de' + ' ' + data[0][3]),
qtd = ('Qtd (und): ' + data[0][5]),
local = ('Local de Produção: ' + data[0][4]);
var index = titles.indexOf(item),
ok = 1;
if (index > -1)
{
var eventDate = events[index].getAllDayStartDate();
if (eventDate.getTime() == date.getTime()) var ok = 0;
else events[index].deleteEvent();
}
if (ok)
cal.createAllDayEvent(item, date, {description: qtd, location: local})
.removeAllReminders();
}
Currently, it sets the events to my calendar containing Item Description as the event title, Product name, qty and Production Location in the description field. I would need the same information to be added to others' calendars.
Besides, this can't mark me as Busy and the event doesn't need to be an All Day event.
Any help is appreciated. Cheers,
Upvotes: 0
Views: 1884
Reputation: 6902
In order to repeat the calendar interactions for multiple calendars, you need to create an array of calendar IDs and loop through them.
One approach would be:
var CALENDER_IDS = [
"calendarID1",
"calendarID2",
"calendarID3",
]
func addToCal() {
... spreadsheet data extraction and date calculations ...
for (var i = 0, clen = CALENDER_IDS.length; i < clen; i++) {
var cal = CalendarApp.getCalendarById(CALENDER_IDS[i]);
... calendar interactions ...
}
}
If you use the above code, make sure to update the counter variable in the title loop (i.e. they both can't be i
). Common practice is to make inner loops use j
, k
, etc.
Upvotes: 0