How to use a filter inside an array formula?

不羁的心 提交于 2019-12-06 09:21:31

For your desired output (including the names), try:

=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)})

Following your thought process (ultimately you want to see if a device is currently checked in or out, right ?), try:

=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")})

Example sheet with last formula in F2

Thank you @JPV Here is the final formula that I used, I changed things around so that all I had to do was enter in the number of the column in and number of the column out:

=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), )))

I am programably creating the script, and here is the code.

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");

}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!