Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Apps Script: how to get the number of columns in a sheet? (alternative way)

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?

like image 825
user2970973 Avatar asked Nov 30 '13 23:11

user2970973


1 Answers

Please have a look at the documentation: getLastColumn() and to get the maximum count including empty ones, getMaxColumns().

like image 143
Serge insas Avatar answered Nov 01 '22 10:11

Serge insas