So I was searching for a way to compare date formats of 2 different cells and validate them.
A SO user showed me some code. We changed it in order not to use the confusing (for me) regex syntax so the user came up with something like this:
const getDateFormats = range =>
SpreadsheetApp.getActiveSpreadsheet()
.getActiveSheet()
.getRange(range)
.getNumberFormats()
.flat();
const main = () => {
const formatsInRange = getDateFormats('A1:A3')
console.log(formatsInRange);
formatsInRange.forEach((format, i) =>
'dddd", "d" "mmmm" "yyyy' === format ||
'dd"/"mm"/"yyyy' === format
? console.log(`Value ${i + 1} is OK`)
: console.log(`Value ${i + 1} is not OK`)
)
};
So the code above will work for 2 and 3 cells: getDateFormats('A1:A2') and getDateFormats('A1:A3').
But if I try with 4 cells or multiple cells all the way down the column with getDateFormats('A1:A4') or getDateFormats('A1:A') the console displays this error:
I would like to understand why this happens and what needs to be changed in order for the console to display multiple results like it does for 2 or 3 cells.
My file: https://docs.google.com/spreadsheets/d/1ExXtmQ8nyuV1o_UtabVJ-TifIbORItFMWjtN6ZlruWc/edit?usp=sharing
The answer of the question is on the comments, leaving this answer as community wiki:
The code provided was working as expected and returned the proper result.
The asking user commented that:
Oh you're right. It's working. You see yesterday I think the consoles is ok is not ok etc wasn't showing. Something changed or I was just tired. Sorry.