您有 2 个选项。第一种是使用 Google 表格中的标准 query() 函数来获取值。这里的缺点是它只是值的参考。所以你不能对它们重新排序,等等。要使用它,将它放在单元格 A1 中,它将拉出标题并从列 A 和 C 中检索值:
=QUERY(A:C, "select A, C where B = 'blue'", 1)
对于 Google Apps 脚本答案:
这将遍历您的列表工作表,并且对于 B 列为 蓝色 的每一行,它会将 A 列和 C 列中的值保存到新工作表的 A 列和 B 列:
function doIt(){
var activeSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet4");
var lastRow = activeSheet.getLastRow();
var lastCol = activeSheet.getLastColumn();
var targetValues = [];
var sourceSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("List");
var lastSourceRow = sourceSheet.getLastRow();
var lastSourceCol = sourceSheet.getLastColumn();
var sourceRange = sourceSheet.getRange(1, 1, lastSourceRow, lastSourceCol);
var sourceData = sourceRange.getValues();
var activeRow = 0;
//Loop through every retrieved row from the Source
for (row in sourceData) {
//IF Column B in this row has 'blue', then work on it.
if (sourceData[row][1] === 'blue') {
//Save it ta a temporary variable
var tempvalue = [sourceData[row][0], sourceData[row][2]];
//then push that into the variables which holds all the new values to be returned
targetValues.push(tempvalue);
}
}
//Save the new range to the appropriate sheet starting at the last empty row
activeSheet.getRange(lastRow + 1, 1 , targetValues.length, 2).setValues(targetValues);
}
当然,您可以通过替换 2 行来将要测试的值传递给函数。首先,定义函数:
function doIt(testingvar){
传递一个名为 testingvar 的变量,以及用传递的变量替换硬编码测试的测试行:
if (sourceData[row][1] === testingvar) {