'Google Apps Script each sheet different calendar
I have multiple sheets with different dates and different calendar ids. The intention is to have the script go over each sheet and import it in the calendar with the corresponding calander id. I have it working for one sheet/calendar, but i cant figure out how to expand the code to go over each sheet. This is what i have that works for one sheet/calendar:
function scheduleshifts() {
var spreadsheet = SpreadsheetApp.getActiveSheet();
var calendarId = spreadsheet.getRange("B2").getValue();
var eventCal = CalendarApp.getCalendarById(calendarId);
var BBB = spreadsheet.getRange("BO1").getValue();
var CCC = "BL2:"
var signups = spreadsheet.getRange(CCC+BBB).getValues();
for (x=0; x<signups.length; x++){
var shift = signups[x];
var startTime = shift[0];
var endTime = shift[1];
var title = "Werken";
eventCal.createEvent(title, startTime, endTime);
}
}
function onOpen(){
var ui = SpreadsheetApp.getUi();
ui.createMenu('Sync to Calendar').addItem("Schedule shifts now", 'scheduleshifts').addToUi();
}
Solution 1:[1]
Creating Events from multiple sheets and muliple calendar ids
function scheduleshifts() {
const ss = SpreadsheetApp.getActive();//included sheets. If you are always using all sheets then remove this line and the filter section.
const inclshts = ["Sheet1","Sheet2","Sheet3"];
const shts = ss.getSheets().filter(s => ~inclshts.indexOf(s.getName()) ).forEach(sh => {
let eventCal = CalendarApp.getCalendarById(sh.getRange("B2").getDisplayValue());//id is in each sheet
let signups = sh.getRange(`BL2:${sh.getRange("BO1").getValues()}`);
Logger.log(`BL2:${sh.getRange("BO1").getValues()}`);
Logger.log(JSON.stringify(sh.getRange("BO1").getValues()));
Logger.log(JSON.stringify(signups));
signups.forEach((r,i)=>{
eventCal.createEvent("Werken",new Date(r[1]),new Date(r[2]);
});
});
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |