[SOLVED] Call a function from another and returning results

Issue

This Content is from Stack Overflow. Question asked by Johan

I’m trying to call a function from another one (main function) but i can’t see the returning values. When I try to run the function alone without parameters all goes well.

function main(){

  let sheetID = "***IDHERE***";
  let sheet = SpreadsheetApp.openById(sheetID);
  let sheetName = sheet.getSheetByName("SAMPLESHEET");
  let sheetData = sheetName.getDataRange().getDisplayValues().filter(data => data[0] != "");

  Logger.log(buscarRegistros(sheetData, 19));

}

function buscarRegistros(sheet, data){
  let sheetData = sheet;
  let input = data;
  let registros = [];

  for(i=0; i< sheetData.length; i++){
      if( sheetData[0] === input || sheetData[2] === input ){
        registros.push(i);
    }
  }
  return registros;
}



Solution

Modification points:

  • In your script, sheetData is a 2-dimensional array. And, all retrieved values are the string type. But, in your buscarRegistros, you are comparing an array and number at if( sheetData[0] === input || sheetData[2] === input ){. I thought that this might be the reason for your issue.

When these points are reflected in your script, how about the following modification?

From:

if( sheetData[0] === input || sheetData[2] === input ){

To:

if (sheetData[i][0] == input || sheetData[i][2] == input) {

Note:

  • In your script, I thought that the following modification might be able to be also used.

      let sheetID = "***IDHERE***";
      let sheet = SpreadsheetApp.openById(sheetID);
      let sheetName = sheet.getSheetByName("SAMPLESHEET");
      let search = 19;
      let result = sheetName.getDataRange().getDisplayValues().filter(data => data[0] != "").flatMap((r, i) => (r[0] == search || r[2] == search) ? [i] : []);
    

References:


This Question was asked in StackOverflow by Johan and Answered by Tanaike It is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?