如何在数组公式中使用过滤器?
How to use a filter inside an array formula?
我有一个退出表单,我想仅使用公式找出哪些退出。
我的思考过程是找到每个标签的最后一个实例的行号,如果 "out" 行号高于 "in" 行号,则设备当前已检出。
Timestamp Student Name Check out Chromebook Check in Chromebook
2/26/2015 10:33:48 Bjorn Spare1-01
2/26/2015 10:33:59 Fred Spare1-02
2/26/2015 10:34:16 Bjorn Spare1-01
2/26/2015 10:34:39 Conor Spare1-03
2/26/2015 11:57:31 Conor Spare1-01
2/26/2015 11:57:49 Fred Spare1-02
2/26/2015 11:57:59 Bjorn Spare1-02
我能够找到所有已检查过的备件:=sort(UNIQUE(C2:C))
我已经能够找到每个使用的最后一个实例的行号:=max(filter(row(C:C),C:C=F2))
(我的排序唯一函数在 F 列中)。
我想让我的最后一行公式与数组公式一起使用,这样我就不必依赖手动向下拖动公式。
例如,我希望这样可以工作:=arrayformula(max(filter(row(C:C),C:C=F:F)))
注意 arrayformula 的添加以及从 F:2 到 F:F 的更改。
我不太清楚为什么,但我得到的结果是 1000。
编辑:我的预期输出是 "Last checkout row"。
Out Unique Last checkout row
Spare1-01 6
Spare1-02 8
Spare1-03 5
如果我能弄清楚将它添加到数组公式中,我还想找到一种方法来进行排序(独特的功能,以便它成为一个单一的公式,我在列号中输入它并吐出输出每个唯一术语的最后一行编号)。
这是我正在使用的文档的 link:https://docs.google.com/spreadsheets/d/1jC0RPxUZSt7BCHRQI5vBXvoANbQlu9CjDNr2VV-MOOI/edit?usp=sharing
编辑(使用最终公式)(我非常感谢@JVP 的帮助):
=ArrayFormula( if(vlookup(unique(filter(C2:C, len(C2:C))), sort({C2:C, A2:B, row(A2:A)},4,0), 4, 0) > iferror(vlookup(unique(filter(C2:C, len(C2:C))), sort({D2:D, A2:B, row(A2:A)},4,0),4,0),0), vlookup(unique(filter(C2:C, len(C2:C))), sort({C2:C, A2:C, row(A2:A)},5,0), {2,4,3}, 0), ))
对于您想要的输出(包括名称),请尝试:
=ArrayFormula({unique(filter(C2:C, len(C2:C))), vlookup(unique(filter(C2:C, len(C2:C))), sort({C2:C, A2:B, row(A2:A)},4,0), 4, 0)})
根据您的思考过程(最终您想查看设备当前是签入还是签出,对吗?),尝试:
=ArrayFormula({unique(filter(C2:C, len(C2:C))), if(vlookup(unique(filter(C2:C, len(C2:C))), sort({C2:C, A2:B, row(A2:A)},4,0), 4, 0) > iferror(vlookup(unique(filter(C2:C, len(C2:C))), sort({D2:D, A2:B, row(A2:A)},4,0),4,0),0), "checked out", "checked in")})
谢谢@JPV 这是我使用的最终公式,我改变了周围的东西,所以我所要做的就是输入列数和列数:
=sort(ArrayFormula( if(vlookup(unique(filter(INDIRECT("'Form Responses 1'!"&"R2C4:C4", FALSE), len(INDIRECT("'Form Responses 1'!"&"R2C4:C4", FALSE)))), sort({INDIRECT("'Form Responses 1'!"&"R2C4:C4", FALSE),row('Form Responses 1'!A2:A)},2,0), 2, 0) > iferror(vlookup(unique(filter(INDIRECT("'Form Responses 1'!"&"R2C4:C4", FALSE), len(INDIRECT("'Form Responses 1'!"&"R2C4:C4", FALSE)))), sort({INDIRECT("'Form Responses 1'!"&"R2C5:C5", FALSE),row('Form Responses 1'!A2:A)},2,0), 2, 0),0), vlookup(unique(filter(INDIRECT("'Form Responses 1'!"&"R2C4:C4", FALSE), len(INDIRECT("'Form Responses 1'!"&"R2C4:C4", FALSE)))), sort({INDIRECT("'Form Responses 1'!"&"R2C4:C4", FALSE), row('Form Responses 1'!A2:A), INDIRECT("'Form Responses 1'!"&"R2C1:C"&COUNTA('Form Responses 1'!1:1)&"", FALSE)},2,0), {3,4,6}, 0), )))
我正在以编程方式创建脚本,这是代码。
function createReportSheet(setName) {
if (!setName){setName = "Items"};
var destId = FormApp.getActiveForm().getDestinationId();
var as = SpreadsheetApp.openById(destId);
try{
var sheet = as.insertSheet("Report Out: "+setName);
} catch(err){
var sheet = as.getSheetByName("Report Out: "+setName);
sheet.clear();
}
//build formula
var formSheetName = "'Form Responses 1'!";
var colOut = 4;
var colIn = 5;
var colToShow = "3,4,6"; // A=3, B=4, C=5, ..
var frTimeStamp = formSheetName+"A2:A";
var lastCol = "&COUNTA("+formSheetName+"1:1)&";
var frSort = 'INDIRECT("'+formSheetName+'"&"R2C'+1+':C"'+lastCol+'"", FALSE)';
var frIn = 'INDIRECT("'+formSheetName+'"&"R2C'+colIn+':C'+colIn+'", FALSE)';
var frOut = 'INDIRECT("'+formSheetName+'"&"R2C'+colOut+':C'+colOut+'", FALSE)';
//break into parts
var f_unique = "unique(filter("+frOut+", len("+frOut+")))";
var f_sortIn = "sort({"+frOut+",row("+frTimeStamp+")},"+2+",0)";
var f_vlookupIn = "vlookup("+f_unique+", "+f_sortIn+", "+2+", 0)";
var f_sortOut = "sort({"+frIn+",row("+frTimeStamp+")},"+2+",0)";
var f_vlookupOut = "vlookup("+f_unique+", "+f_sortOut+", "+2+", 0)";
var f_sortDisp = "sort({"+frOut+", row("+frTimeStamp+"), "+frSort+"},"+2+",0)";
var f_vlookupDisp = "vlookup("+f_unique+", "+f_sortDisp+", {"+colToShow+"}, 0)";
var formula1 = "ArrayFormula( if("+f_vlookupIn+" > iferror("+f_vlookupOut+",0), "+f_vlookupDisp+", ))";
var formula = "=sort("+formula1+")";
// put formula into sheet
sheet.getRange(2, 1).setFormula(formula);
sheet.getRange("a:a").setNumberFormat("mm/dd/yy h:mm:s");
}
我有一个退出表单,我想仅使用公式找出哪些退出。
我的思考过程是找到每个标签的最后一个实例的行号,如果 "out" 行号高于 "in" 行号,则设备当前已检出。
Timestamp Student Name Check out Chromebook Check in Chromebook
2/26/2015 10:33:48 Bjorn Spare1-01
2/26/2015 10:33:59 Fred Spare1-02
2/26/2015 10:34:16 Bjorn Spare1-01
2/26/2015 10:34:39 Conor Spare1-03
2/26/2015 11:57:31 Conor Spare1-01
2/26/2015 11:57:49 Fred Spare1-02
2/26/2015 11:57:59 Bjorn Spare1-02
我能够找到所有已检查过的备件:=sort(UNIQUE(C2:C))
我已经能够找到每个使用的最后一个实例的行号:=max(filter(row(C:C),C:C=F2))
(我的排序唯一函数在 F 列中)。
我想让我的最后一行公式与数组公式一起使用,这样我就不必依赖手动向下拖动公式。
例如,我希望这样可以工作:=arrayformula(max(filter(row(C:C),C:C=F:F)))
注意 arrayformula 的添加以及从 F:2 到 F:F 的更改。
我不太清楚为什么,但我得到的结果是 1000。
编辑:我的预期输出是 "Last checkout row"。
Out Unique Last checkout row
Spare1-01 6
Spare1-02 8
Spare1-03 5
如果我能弄清楚将它添加到数组公式中,我还想找到一种方法来进行排序(独特的功能,以便它成为一个单一的公式,我在列号中输入它并吐出输出每个唯一术语的最后一行编号)。
这是我正在使用的文档的 link:https://docs.google.com/spreadsheets/d/1jC0RPxUZSt7BCHRQI5vBXvoANbQlu9CjDNr2VV-MOOI/edit?usp=sharing
编辑(使用最终公式)(我非常感谢@JVP 的帮助):
=ArrayFormula( if(vlookup(unique(filter(C2:C, len(C2:C))), sort({C2:C, A2:B, row(A2:A)},4,0), 4, 0) > iferror(vlookup(unique(filter(C2:C, len(C2:C))), sort({D2:D, A2:B, row(A2:A)},4,0),4,0),0), vlookup(unique(filter(C2:C, len(C2:C))), sort({C2:C, A2:C, row(A2:A)},5,0), {2,4,3}, 0), ))
对于您想要的输出(包括名称),请尝试:
=ArrayFormula({unique(filter(C2:C, len(C2:C))), vlookup(unique(filter(C2:C, len(C2:C))), sort({C2:C, A2:B, row(A2:A)},4,0), 4, 0)})
根据您的思考过程(最终您想查看设备当前是签入还是签出,对吗?),尝试:
=ArrayFormula({unique(filter(C2:C, len(C2:C))), if(vlookup(unique(filter(C2:C, len(C2:C))), sort({C2:C, A2:B, row(A2:A)},4,0), 4, 0) > iferror(vlookup(unique(filter(C2:C, len(C2:C))), sort({D2:D, A2:B, row(A2:A)},4,0),4,0),0), "checked out", "checked in")})
谢谢@JPV 这是我使用的最终公式,我改变了周围的东西,所以我所要做的就是输入列数和列数:
=sort(ArrayFormula( if(vlookup(unique(filter(INDIRECT("'Form Responses 1'!"&"R2C4:C4", FALSE), len(INDIRECT("'Form Responses 1'!"&"R2C4:C4", FALSE)))), sort({INDIRECT("'Form Responses 1'!"&"R2C4:C4", FALSE),row('Form Responses 1'!A2:A)},2,0), 2, 0) > iferror(vlookup(unique(filter(INDIRECT("'Form Responses 1'!"&"R2C4:C4", FALSE), len(INDIRECT("'Form Responses 1'!"&"R2C4:C4", FALSE)))), sort({INDIRECT("'Form Responses 1'!"&"R2C5:C5", FALSE),row('Form Responses 1'!A2:A)},2,0), 2, 0),0), vlookup(unique(filter(INDIRECT("'Form Responses 1'!"&"R2C4:C4", FALSE), len(INDIRECT("'Form Responses 1'!"&"R2C4:C4", FALSE)))), sort({INDIRECT("'Form Responses 1'!"&"R2C4:C4", FALSE), row('Form Responses 1'!A2:A), INDIRECT("'Form Responses 1'!"&"R2C1:C"&COUNTA('Form Responses 1'!1:1)&"", FALSE)},2,0), {3,4,6}, 0), )))
我正在以编程方式创建脚本,这是代码。
function createReportSheet(setName) {
if (!setName){setName = "Items"};
var destId = FormApp.getActiveForm().getDestinationId();
var as = SpreadsheetApp.openById(destId);
try{
var sheet = as.insertSheet("Report Out: "+setName);
} catch(err){
var sheet = as.getSheetByName("Report Out: "+setName);
sheet.clear();
}
//build formula
var formSheetName = "'Form Responses 1'!";
var colOut = 4;
var colIn = 5;
var colToShow = "3,4,6"; // A=3, B=4, C=5, ..
var frTimeStamp = formSheetName+"A2:A";
var lastCol = "&COUNTA("+formSheetName+"1:1)&";
var frSort = 'INDIRECT("'+formSheetName+'"&"R2C'+1+':C"'+lastCol+'"", FALSE)';
var frIn = 'INDIRECT("'+formSheetName+'"&"R2C'+colIn+':C'+colIn+'", FALSE)';
var frOut = 'INDIRECT("'+formSheetName+'"&"R2C'+colOut+':C'+colOut+'", FALSE)';
//break into parts
var f_unique = "unique(filter("+frOut+", len("+frOut+")))";
var f_sortIn = "sort({"+frOut+",row("+frTimeStamp+")},"+2+",0)";
var f_vlookupIn = "vlookup("+f_unique+", "+f_sortIn+", "+2+", 0)";
var f_sortOut = "sort({"+frIn+",row("+frTimeStamp+")},"+2+",0)";
var f_vlookupOut = "vlookup("+f_unique+", "+f_sortOut+", "+2+", 0)";
var f_sortDisp = "sort({"+frOut+", row("+frTimeStamp+"), "+frSort+"},"+2+",0)";
var f_vlookupDisp = "vlookup("+f_unique+", "+f_sortDisp+", {"+colToShow+"}, 0)";
var formula1 = "ArrayFormula( if("+f_vlookupIn+" > iferror("+f_vlookupOut+",0), "+f_vlookupDisp+", ))";
var formula = "=sort("+formula1+")";
// put formula into sheet
sheet.getRange(2, 1).setFormula(formula);
sheet.getRange("a:a").setNumberFormat("mm/dd/yy h:mm:s");
}