简体   繁体   中英

Retrieving a specific cell value from Google app script sheet using JavaScript

I'm trying to retrieve a specific value from a google app script sheet. I have a sheet where it stores information. I want to loop through that sheet based on row and retrieve all values that match and meet the conditions given.

For example

if (row[4].toString().toLowerCase() == anotherRow[4]){

    //then display all rows which match that specific value

    // e.g:  
    row[4]. Display everything that matches anotherRow only;

}

this is what I can't get my head around displaying all rows that meet that criteria only, currently I'm able to display all row[4], row[4] is the column

This script that will get a range of data and store it as an array of arrays.

function getMultipleValues(){
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName('Sheet1');
  // getRange exp. - 
  //sheet.getRange(first row, first column, last row, last column)

  // Get a grid of data 4 rows and two columns
  var range = sheet.getRange(1,1,4,2);
  var dataGrid = range.getValues()
  Logger.log(dataGrid)
  //[["Fruit","quantity"],["pears",1],["apples",1],["grapes",3]]


  //Get a column of data
  var range = sheet.getRange(1,1,4,1);
  var dataColumn = range.getValues()
  Logger.log(dataColumn)
  //[["Fruit"],["pears],["apples"],["grapes"]]

  //If the column length is unknown. Get everything.
  //Be sure to delete rows that are not needed in the sheet when using this.  
  var range = sheet.getRange("A1:A");
  var allOfColumnA = range.getValues()
  Logger.log(allOfColumnA)
  //[["Fruit"],["pears],["apples"],["grapes"],[],[],[],[]]

}

Building on Supertopaz' answer, it looks like the filter method of an array will work to remove rows that don't match your criteria. For example, given a set of data like this:

Source data for function

The following function will match the data to the criteria you specify, and write the matches to a second sheet.

 function writeMatches() { var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet1 = ss.getSheetByName('Sheet1'); var sheet2 = ss.getSheetByName('Sheet2'); var criteria = sheet1.getRange(1,2,1,1).getValue(); // returns a single value if the range is a single cell var arData = sheet1.getRange(4,1,5,2).getValues(); // returns a 2d array var arOutput = []; var rangeOutput; arOutput = arData.filter(function(row){ return row[1].toLowerCase() === criteria /* The callback function iterates through the array. When this expression evaluates to true, the element is included in the output array. The callback function can also supply an optional index argument, if you want to compare symmetric arrays. */ }); rangeOutput = sheet2.getRange(1,1,arOutput.length,arOutput[0].length); rangeOutput.setValues(arOutput); } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM