How can I store a range of cells to an array?

后端 未结 4 1368
孤独总比滥情好
孤独总比滥情好 2021-01-06 16:55

If I have a list of data in cells A1:A150 (but the amount can vary), is there a way to push that into an array without looking at each cell individually to determine if it i

4条回答
  •  既然无缘
    2021-01-06 17:22

    Try this:

        var sheet = SpreadsheetApp.openById(SHEET_ID).getSheetByName(SHEET_NAME);
        var lastRow = sheet.getLastRow();
    
        var data = sheet.getRange(1, 1, lastRow, 1).getValues(); //getRange(starting Row, starting column, number of rows, number of columns)
    
        for(var i=0;i<(lastRow-1);i++)
        {
          Logger.log(data[0][i]);
        }
    

    the variable data stores all the cells of column A.

    Cell A1 is stored in data[0][0], cell A2 is stored in data[0][1], cell A3 is stored in data[0][2] and so on.

    The getRange(starting Row, starting column, number of rows, number of columns) is a batch operation so it is much faster when you have a large dataset.

提交回复
热议问题