这是一个自定义函数,可以替换您正在使用的公式。例如:
=listStudents(C50,B50,F50)
如果以这种方式使用,您仍然会不断地重新计算,但它应该比正则表达式测试快得多。或者,可以从菜单项调用相同的函数,并用于填充工作表中的给定目标范围,从而完全避免自动重新计算。
代码:
/**
* Custom spreadsheet function to produce a list of names of
* students that match the given criteria.
*/
function listStudents( givenName, surname, employer ) {
var matches = []; // matching students will be placed in this array
var HEADERS = 1; // # rows of header info at top of sheet
var FULLNAME = 1; // Column containing full names (B)
var EMPLOYER = 2; // employers (C)
// Array filter function - returns true if conditions match
function test4match( row ) {
return ( row[FULLNAME].indexOf(givenName) !== -1 &&
row[FULLNAME].indexOf(surname) !== -1 &&
row[EMPLOYER].indexOf(employer) !== -1)
}
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Students');
var range = sheet.getDataRange();
var data = range.getValues().slice(HEADERS); // All data from sheet, without headers
var filteredData = data.filter(test4match); // Get matching rows
for (var i=0; i<filteredData.length; i++) {
matches.push(filteredData[i][FULLNAME]); // Then produce list of names
}
return [matches]; // Return a 2-d array, one row
}