6

a really quick question... I want to read rows in sheets one by one, (preferably 'live' so rather than putting it in an array or something). Besides using a loop, how do I read the contents of a whole row? Is it even possible?

stretch
  • 113
  • 1
  • 3
  • 6
  • Possible duplicate of [Retrieve rows from spreadsheet data using google app script](http://stackoverflow.com/questions/10518084/retrieve-rows-from-spreadsheet-data-using-google-app-script) – Sujay Phadke Mar 20 '17 at 12:24
  • You might consider setting up a separate sheet that will filter the row you are interested in instead of using Google Apps scripts. – ScampMichael Mar 20 '17 at 16:33

3 Answers3

7

You can read about it here

function readData() {
  var sht = SpreadsheetApp.getActiveSheet();
  var rng = sht.getRange(rownumber, 1, 1, numberofcolums)
  var rangeArray = rng.getValues();
//now all your data for that row is in a two Dimensional array [[1,2,3,4,'My Data','etc']]  

}
Cooper
  • 59,616
  • 6
  • 23
  • 54
2
var
  spreadsheetID =
    "1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms",
  rowNum = 1,
  rowVals =
    SpreadsheetApp.openById(spreadsheetID)
    .getRange(rowNum +":"+ rowNum)
    .getValues()
;

The other answers to date don't get a row, they get a range of specfic columns within a row. The code in this answer gets the entire unbounded row.

Matthew
  • 757
  • 11
  • 19
1

Not sure exactly what your end goal is, but I assume you mean something like this:

      var values = sheet.getRange(startRow, startCol, numRows, numCols).getValues(); // returns 2d array

      values.forEach(function(row){

             Logger.log(row); // prints the contents of the row

          });

You could also use the getCell() method of the Range class to iterate over cells in a range. The return type for getCell() is also Range. See this thread for details on the implementation Google Spreadsheets: Iterate over range, append string to each

Community
  • 1
  • 1
Anton Dementiev
  • 5,451
  • 4
  • 20
  • 32