You need to maintain a mapping of client to calendar ID somewhere so that each time you process an event row you can do:
var clientEventCal = CalendarApp.getCalendarById(clientCalendarId);
to make sure you're adding the event to the right calendar.
Note: If you share the Apps Script code you're using to create events to a single calendar using your spreadsheet structure, I can provide specific instructions on the exact changes to make!
For the sake of outlining a complete solution, let's assume you create a new sheet named "Client Calendars" containing Client or Project in column A and that client's Google Calendar ID in column B.
After you create this new client-to-client calendar ID table, you have a few options of how to use it:
- Option A: Read the table directly in your code at runtime and look up the right Calendar ID to use in Apps Script code.
- Option B: Add a new column to your main data sheet that uses each row's Client column value to populate the calendar ID directly onto your source data row using
VLOOKUP.
(B) is probably easier, as it will require less Apps Script code modification.
A:
function createAllDayEvent(calendarId, eventName, date, location, description) {
var eventCal = CalendarApp.getCalendarById(calendarId);
var event = eventCal.createAllDayEvent(eventName, date, date, {location: location, description: description});
return event;
}
function createClientEvents(event) {
var sheet = event ? event.source.getSheets()[0] : SpreadsheetApp.getActiveSheet();
var startRow = 4;
var dataRange = sheet.getRange('A' + startRow.toString() + ':G');
var data = dataRange.getDisplayValues();
var clientCalendarSheet = ss.getSheetByName('Client Calendars');
var clientCalendarData = clientCalendarSheet.getDataRange().getValues();
// Date (Entry) | Client / Project | Event | Location | Capacity | Seated Cap | Link
var dateStr, client, eventName, location, capacity, seatedCap, link;
for (var i = startRow - 1; i < data.length; i++) {
[dateStr, client, eventName, location, capacity, seatedCap, link] = data[i];
if (!eventName) continue;
var calendarId = null;
for (var j = 0; j < clientCalendarData.length; j++) {
if (clientCalendarData[i][0] == client) {
calendarId = clientCalendarData[i][1];
break;
}
}
var date = new Date(dateStr);
var description = "Capacity: " + capacity + ", Seated Cap: " + seatedCap + ", Link: " + link;
createAllDayEvent(calendarId, eventName, date, location, description);
}
}
B:
This solution assumes data sheet column M contains client's Calendar ID (you could populate M using =IF($B4="",, VLOOKUP($B4, 'Client Calendars'!$A:$B, 2, FALSE)) assuming you've created the "Client Calendars" sheet as outlined above).
function createAllDayEvent(calendarId, eventName, date, location, description) {
var eventCal = CalendarApp.getCalendarById(calendarId);
var event = eventCal.createAllDayEvent(eventName, date, date, {location: location, description: description});
return event;
}
function createClientEvents(event) {
var sheet = event ? event.source.getSheets()[0] : SpreadsheetApp.getActiveSheet();
var startRow = 4;
var dataRange = sheet.getRange('A' + startRow.toString() + ':G');
var data = dataRange.getDisplayValues();
var calendarIdColumn = sheet.getRange('M' + startRow.toString() + ':M').getValues();
// Date (Entry) | Client / Project | Event | Location | Capacity | Seated Cap | Link
var dateStr, client, eventName, location, capacity, seatedCap, link;
for (var i = startRow - 1; i < data.length; i++) {
[dateStr, client, eventName, location, capacity, seatedCap, link] = data[i];
if (!eventName) continue;
var calendarId = calendarIdColumn[i][0];
var date = new Date(dateStr);
var description = "Capacity: " + capacity + ", Seated Cap: " + seatedCap + ", Link: " + link;
createAllDayEvent(calendarId, eventName, date, location, description);
}
}