简体   繁体   中英

Returning data from single Google Sheets row from .gs file to .js file

I am trying to create a web app in Google scripts that returns project data based on a search query. I have the search query functioning correctly, however, the returned array filteredRows has a length of 1 when I query it, therefore when I try to use the values based on index, they are still all together. There are 12 columns in the spreadsheet. The function is returning the correct information back to me js file though.

I have tried JSON.stringify and I couldn't get it to work, but it is not something I have used before. I do suspect the answer lies there though.

The spreadsheet may have hundreds of lines, but I only want to return one at a time for manipulation.

Here is the code for the search function. var headers and var cols are there from trying to use the JSON method, but otherwise not doing anything right now.

function searchProject(tendNumber) {

   var id = 'mySpreadSheetId';
   var ss = SpreadsheetApp.openById(id);
   PropertiesService.getScriptProperties().setProperty("tend", tendNumber);
   var ws = ss.getSheetByName('Data');
   var data = ws.getRange(1, 1, ws.getLastRow(), 12).getValues();
   var headers = data[0];
   var cols = headers.length;
   Logger.log(headers);
   var filteredRows = data.filter(function(row){
   var ids = PropertiesService.getScriptProperties().getProperty("tend");
    if (row[4] == ids){
      return row;
   }

})

 return (filteredRows);
}

Any guidance as to which direction to go would be greatly appreciated. There does seem to be a lot of info available, but I need a pointer to fully understand it.

I have edited this in case it helps others. The answer was actually simple.

As stated all was working but I couldn't access the indexes in the array. I wasn't aware that it actually returns an Array of Arrays, therefore if I access array[0] it will return all of the first inner array.

To access each individual element of the returned row or rows it is array[0][0].

var filteredRows = data.filter(function(row){ var ids = PropertiesService.getScriptProperties().getProperty("tend"); if (row[4] == ids){ return row; }

You don't really want to return the row you just want to return a truthy

var ids = PropertiesService.getScriptProperties().getProperty("tend"); data.filter(function(row){return row[4] == ids;}

Your don't really want to declare ids inside the loop either since it's going to be the same for the whole function as it's a function parameter.

Array.filter

The filter() method creates a new array with all elements that pass the test implemented by the provided function. ie that return true

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