Google Apps Script: how to get the number of columns in a sheet? (alternative way)
google-apps-script, javascript
Solution
Please have a look at the documentation: getLastColumn() and to get the maximum count including empty ones, getMaxColumns().
Problem
I want to use the number of columns in a sheet in a for loop. I could use a function like this, to stop when the loop finds the first empty column: ``` function getRowAsArray(sheet, row) { var dataRange = sheet.getRange(row, 1, 1, 99); var data = dataRange.getValues(); var columns = []; for (i in data) { var row = data[i]; Logger.log("Got row", row); for(var l=0; l<99; l++) { var col = row[l]; // First empty column interrupts if(!col) { break; } columns.push(col); } } return columns; } ``` But I'd like an alternative function which use the number of columns in the sheet. How can I do that?