Home > database >  Call a function from another and returning results
Call a function from another and returning results

Time:09-20

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;
}

CodePudding user response:

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:

CodePudding user response:

Try this:

function main(){
  let ss = SpreadsheetApp.openById("sheetID");
  let sh = ss.getSheetByName("SAMPLESHEET");
  let vs = sh.getDataRange().getDisplayValues().filter(data => data[0] != "");
  Logger.log(buscarRegistros(vs, 19));
}
function buscarRegistros(v,n){
  let a =v.map(r => {if(r[0] == n || r[2] == n) { return i;};})
  return a;
}
  • Related