我正在尝试使用可变数量(0 到 3)的本身动态的过滤条件来过滤数组。例如,一个过滤条件基于用户从 Google 表格上的下拉列表中选择的名称。如果过滤条件不是动态的(例如名称是硬编码的),则它可以工作。请参阅下面的行。
const filterFn1 = "x => x[0] === 'John Doe'";
Run Code Online (Sandbox Code Playgroud)
我尝试过这个,但 Google Apps 脚本似乎无法识别new Function
const filterFn1 = new Function("x => x[0] === '" + name + "'");
Run Code Online (Sandbox Code Playgroud)
完整代码:
function myFilter() {
const ss = SpreadsheetApp.getActiveSpreadsheet();
const shtDash = ss.getSheetByName('Dashboard');
const shtDB = ss.getSheetByName('Database');
const lastDBRow = shtDB.getLastRow();
const arrData = shtDB.getRange(2,1,lastDBRow-1,4).getValues();
let arr = [];
const name = shtDash.getRange('B1').getValue();
if (name !== '') {
const filterFn1 = "x => x[0] === '" + name …Run Code Online (Sandbox Code Playgroud)