I have a dataset in my google sheets where one of the columns contains various dates. My objective is to add an empty row under every date that falls on a Sunday (regardless of the year). I am only concerned with identifying the day of the week.
I have come up with a pseudo-code outline for this task:
- Iterate through the column containing dates
- Modify the date format to display only the full abbreviation of the day (e.g., Sunday)
- If the formatted day is "Sunday", then insert a new row below that specific date.
Below is the current script I am using:
function addRows() {
const ss = SpreadsheetApp.getActiveSpreadsheet()
const sheet = ss.getActiveSheet();
const range = sheet.getDataRange();
const data = range.getValues();
for (let i = 1; i < data.length; i++) {
const sheetDate = data[i][0];
const day = Utilities.formatDate(sheetDate, ss.getSpreadsheetTimeZone(), "EEEE");
if (day === "Sunday") {
sheet.insertRowsAfter(i, 1); // However, I suspect the issue lies within this line as it should not be i but rather the index of the row containing the value "Sunday"
}
}
}
I seem to be encountering some confusion regarding the first argument of the insertRowsAfter()
function. What value should this argument actually represent?