get values ​from one or more rows matching an index value in google apps script

Viewed 448

I am trying the following:

Look for this value of the sheet "Pedidos" in the rows of the sheet "Pedidos articulos". When you find one or more rows matches that contain these search value, then get the values ​​for the entire row.

I need to get the data ​​of the matching rows, however, I don't need to paste them anywhere on the sheet, since this function is to display this data in a modaldialog html template through Scriptlets.

What's my problem:

My problem is that I have managed to find the matches of the value (numPedido) in the "Pedidos articulos" sheet, however, it always returns the index of the matching rows. I can't get the data for those matching rows.

My code:

function obtenerProductos3(){

  //Hoja Pedidos

  var hojaPedidos = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Pedidos');
  var fila = hojaPedidos.getCurrentCell().getRow();
  var numeroPedidoPE = hojaPedidos.getRange(fila,3,1,1).getDisplayValue();
  Logger.log(numeroPedidoPE)

  //Hoja Pedidos artículos

  var hojaPedidosArticulos = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Pedidos Artículos'); 
  var numPedidoAR = hojaPedidosArticulos.getRange('C7:C').getValues();
  var articulos = hojaPedidosArticulos.getRange(7,3, hojaPedidosArticulos.getLastRow()-6, hojaPedidosArticulos.getLastColumn()-1).getValues();

  //var articulos = hojaPedidosArticulos.getRange('C7:O').getDisplayValues();

  Logger.log(numPedidoAR)
  Logger.log(articulos)
  
  var newData =  articulos.map(function(r){ return r[0]; });
  Logger.log(newData)

  var indexes = newData.map((element, i) => element === numeroPedidoPE ? i : "")
                      .filter(element => element !== "");

  Logger.log(indexes)
  
  return (hojaPedidosArticulos.getRange(indexes, 2, indexes, hojaPedidosArticulos.getLastColumn()).getDisplayValues());


}

All help is greatly appreciated.

1 Answers

indexes is an array. Please iterate it.

  const values = hojaPedidosArticulos.getRange(7, 2, hojaPedidosArticulos.getLastRow() - 6, hojaPedidosArticulos.getLastColumn() - 1).getDisplayValues();
  const results = values.filter((e, i) => indexes.includes(i));
  return results;
Related