I have imported a series of events from google calendar. Each event has a title, start_date, end_date, id, etc.
function getEvents(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("SHEET_NAME");
let MONTHS_IN_ADVANCE = 12;
let minDate = new Date(44927);
let maxDate = new Date();
maxDate.setMonth(maxDate.getMonth() + MONTHS_IN_ADVANCE)
var cal = CalendarApp.getCalendarById("CALENDAR_ID");
var events = cal.getEvents(minDate,maxDate);
for(var i = 0;i<events.length;i++){
var title = events[i].getTitle();
var start_time = events[i].getStartTime();
var end_time = events[i].getEndTime();
var id = events[i].getId();
For the sake of creating an example, let’s say that events = [[a,b,c,d,e,f,g]]
If I send this to google sheets, I can get a table that looks like this:
| title | start_time | end_time | id | etc… | |
|---|---|---|---|---|---|
| Cell 1 | a | start_time | end_time | id | etc… |
| Cell 2 | b | start_time | end_time | id | etc… |
| Cell 3 | c | start_time | end_time | id | etc… |
| Cell 4 | d | start_time | end_time | id | etc… |
| Cell 5 | e | start_time | end_time | id | etc… |
| Cell 6 | f | start_time | end_time | id | etc… |
| Cell 7 | g | start_time | end_time | id | etc… |
sheet.getRange(i+2,1).setValue(title);
sheet.getRange(i+2,3).setValue(start_time);
sheet.getRange(i+2,4).setValue(end_time);
sheet.getRange(i+2,5).setValue(id);
Here’s the actual question…
The thing is, I know that event ‘b’ is 6 days long, event ‘f’ is 2 days long, ‘g’ is 30 days long, and the others are 1 day long or less.
What I actually want, is a table that looks like this:
| title | day | start_time | end_time | id | etc… | |
|---|---|---|---|---|---|---|
| Cell 1 | a | 1 | start_time | end_time | id | etc… |
| Cell 2 | b | 1 | start_time | end_time | id | etc… |
| Cell 3 | b | 2 | start_time | end_time | id | etc… |
| Cell 4 | b | 3 | start_time | end_time | id | etc… |
| Cell 5 | b | 4 | start_time | end_time | id | etc… |
| Cell 6 | b | 5 | start_time | end_time | id | etc… |
| Cell 7 | b | 6 | start_time | end_time | id | etc… |
| Cell 8 | c | 1 | start_time | end_time | id | etc… |
| Cell 9 | d | 1 | start_time | end_time | id | etc… |
| Cell 11 | e | 1 | start_time | end_time | id | etc… |
| Cell 12 | f | 1 | start_time | end_time | id | etc… |
| Cell 13 | f | 2 | start_time | end_time | id | etc… |
| Cell 14 | g | 1 | start_time | end_time | id | etc… |
| Cell 15 | g | 2 | start_time | end_time | id | etc… |
| Cell … | … | …30 | start_time | end_time | id | etc… |
(note: the column “day” isn’t necessary, just here to try to make my request clearer.)
To get the array of dates between start and finish for ‘b’, ‘f’, and ‘g’, I use getDatesBetween:
var betweenDateArray = getDatesBetween(start_time,end_time);
function getDatesBetween(startT, endT) {
var dateArray = new Array();
var midDate = startT;
while (startT <= endT) {
dateArray.push(new Date (midDate));
midDate.setDate(midDate.getDate() + 1);
}
return dateArray;
}
…which returns an object with the correct dates, but I don’t know how to transpose this data into additional rows with the correct (original [i]) email/title/id labels.
Currently betweenDateArray sits within the for loop, my thinking being that I need to run another loop for each iteration of [i] – but as a Javascript newbie I’m unsure.
As the output is going to be in sheets anyway, I’d be equally happy solving this with a formula as with the script, but I’m stuck on both. In sheets I’ve tried TRANSPOSE and FLATTEN after applying SEQUENCE to find the dates between start_date and end_date, but the additional rows created by doing so try (and fail) to overwrite the rows that follow. The incoming data from the calendar updates regularly, so I can’t say that event ‘b’ will always be in row 2, because another event may be added or ‘a’ might get deleted – therefore I suspect any formula in sheets would have to be an arrayformula, hence the attempt to use a script instead…help please!

