首页 > 解决方案 > 在 Google 表格中使用多个条件过滤值

问题描述

我正在使用下面的公式从第 1 页过滤 H 列中的 OK 值并粘贴到第 2 页

function filter() {
const sss = SpreadsheetApp.getActiveSpreadsheet();
const ss = sss.getSheetByName('Page1'); //replace with source Sheet tab name
const rawData = ss.getRange("A2:I150").getValues().map(([a,,c,d,e,f,g,h]) => [a,c,d,e,f,g,h]); // code [a,c,d,e,f,g]
const frawData=rawData.filter(row=>row[6]!="OK");// 5 means column G. [a,c,d,e,f,g] => [0,1,2,3,4,5]
const tss = SpreadsheetApp.getActiveSpreadsheet(); 
const ts = tss.getSheetByName('Page2'); //replace with destination Sheet tab name
ts.getRange(2, 1, frawData.length, frawData[0].length).setValues(frawData);
}

我有另一个场景

在此处输入图像描述

我还需要使用值为 1 的 I 列进行过滤所以,我应该得到第 4 行值,因为它包含 H 列 OK 和 I 列 1

标签: javascriptgoogle-apps-scriptgoogle-sheets

解决方案


不知道你为什么使用!="OK"并说该行应该包含“OK”。

所以可能你需要而不是这个:

const frawData=rawData.filter(row=>row[6]!="OK");

尝试这个:

const frawData=rawData.filter(row => row[6] == "OK" && row[7] == 1);

推荐阅读