Imagine we have the following Sheet:
A | B | C | D | E | F | |
---|---|---|---|---|---|---|
1 | John | Juan | Pepe | |||
2 | Y | N | Y |
Then we want to iterate the row 1:
var sheet = SpreadsheetApp.getActiveSheet();
var peopleNamesRangeWithEmptyCells = sheet.getRange('A1:F1'); // ← Here are the name of the people like John, Juan...
var peopleNamesRange = [];
peopleNamesRangeWithEmptyCells.forEach(function (cell) {
if (cell.value() != "") {
doSomethingOnThatColumn(cell.value(), cell.getColumn());
}
});
But it seems I cannot iterate a range with foreach as I get the error: TypeError: peopleNamesRangeWithEmptyCells.forEach is not a function
I know the common way to go for it is using getValues()
:
var peopleNamesRangeWithEmptyCells = sheet.getRange('A1:F1').getValues(); // ← Here are the name of the people like John, Juan...
But then I would loose the original row and column, so I could not do something like calling a function with the column as param:
doSomethingOnThatColumn(cell.value(), cell.getColumn());
It seems a basic question, but I'm struggling: How to iterate a range and not only its values?