javascript Google Apps Script:如何获取工作表中的列数?(另一种方式)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20306794/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 18:11:45  来源:igfitidea点击:

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

javascriptgoogle-apps-script

提问by user2970973

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:

我想在 for 循环中使用工作表中的列数。我可以使用这样的函数,在循环找到第一个空列时停止:

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?

但我想要一个使用工作表中列数的替代函数。我怎样才能做到这一点?

回答by Serge insas

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

请查看文档getLastColumn()并获得最大计数,包括空计数,getMaxColumns()