get value in one column in spreadsheet using google apps script

前端 未结 3 938
渐次进展
渐次进展 2020-12-08 01:03

I want to get a string value -to compare it later on with an if condition- from only one column in spreadsheet using Google apps script. I searched the internet and I found

相关标签:
3条回答
  • 2020-12-08 01:24

    if you use simply :

    var values = SpreadsheetApp.getActiveSheet().getDataRange().getValues()

    You will get a 2 Dimension array of all the data in the sheet indexed by rows and columns.

    So to get the value in column A, row1 you use values[0][0] , values[1][0] for columnA, row 2, values[0][2] for column C row1, etc...

    If you need to iterate in a for loop (in a single column) :

    for(n=0;n<values.length;++n){
    var cell = values[n][x] ; // x is the index of the column starting from 0
    }
    

    If you need to iterate in a for loop (in a single row) :

    for(n=0;n<values[0].length;++n){
    var cell = values[x][n] ; // x is the index of the row starting from 0
    }
    
    0 讨论(0)
  • 2020-12-08 01:26

    Here is the script I use to get the values in a dynamic column:

    var SS = SpreadsheetApp.getActiveSheet()
    var Avals = SS.getRange("A1:A").getValues();
    var numberOfValues = Avals.filter(String).length;
    var RangeVals = SS.getRange(1,1,numberOfValues).getValues();
    

    I've never had to change which starting row based based on a dynamic changing starting point though. Would be interested in seeing how that would be done.

    Here is a similar post. Another example here.

    0 讨论(0)
  • 2020-12-08 01:38

    much easier way to loop through the rows and get a column value.. hope that helps

    var values = SpreadsheetApp.getActiveSheet().getDataRange().getValues();
    
    values.forEach( function(row) {
      row[4] // column index as 4
    });
    
    0 讨论(0)
提交回复
热议问题