Google Apps Script如何获取Google表格中匹配指定值的整行数据
修复方案
核心错误原因
SpreadsheetApp提供的getRange()方法仅支持传入数字类型的起始行号、起始列号、行数、列数,无法直接接收索引数组作为参数,你原本的写法不符合方法调用规范- 你已经提前将「Pedidos Artículos」工作表的目标区域数据全部读取到了
articulos数组中,无需再调用接口二次读取工作表数据,直接通过数组下标过滤匹配结果即可,执行效率也更高
修正后完整代码
function obtenerProductos3(){ // 读取Pedidos工作表当前选中行的订单号 const hojaPedidos = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Pedidos'); const fila = hojaPedidos.getCurrentCell().getRow(); const numeroPedidoPE = hojaPedidos.getRange(fila,3,1,1).getDisplayValue(); // 读取Pedidos Artículos工作表的目标区域数据 const hojaPedidosArticulos = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Pedidos Artículos'); // 这里如果需要获取B列的内容,把起始列参数3改为2即可 const articulos = hojaPedidosArticulos.getRange(7,3, hojaPedidosArticulos.getLastRow()-6, hojaPedidosArticulos.getLastColumn()-1).getDisplayValues(); // 提取所有订单号列的内容做匹配 const pedidoList = articulos.map(r => r[0]); // 拿到所有匹配行在articulos数组中的下标 const indexes = pedidoList.map((element, i) => element === numeroPedidoPE ? i : "") .filter(element => element !== ""); // 直接从已读取的数组中取出所有匹配行内容,返回二维数组可直接给HTML模板使用 return indexes.map(i => articulos[i]); }
调用说明
修正后返回的就是所有匹配订单号的行数据组成的二维数组,可直接传递给模态对话框的HTML模板通过Scriptlets渲染,不需要额外处理。
内容的提问来源于stack exchange,提问作者Nerea
相关产品推荐
相关产品推荐

