Home > database >  Filtering is too slow
Filtering is too slow

Time:09-06

I need help making this code more efficient it takes ages to run , now I have like 3500 lines later will be much more... I tried with looping but I thing I just doing something wrong.. Any help will be appreciated... Code that work but runs slow as 1995 xp windows below:

function myReport() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var loanSht = ss.getSheetByName("CURENT POOL DATA SHEET");
  var report = ss.getSheetByName("REPORTS");
  var dRng = loanSht.getRange(2, 1, loanSht.getLastRow(), 14).getDisplayValues();


  if (report.getRange("B3").getValue() !== "") {
   dRng = dRng.filter(function(e){ return e[1] === report.getRange("B3").getValue()});
  }  
  if (report.getRange("C3").getValue() !== "") {
    dRng = dRng.filter(function(e){ return e[2] === report.getRange("C3").getValue()});   
  } 
  if (report.getRange("D3").getValue() !== "") {
    dRng = dRng.filter(function(e){ return e[3] === report.getRange("D3").getValue()}); 
  } 
  if (report.getRange("E3").getValue() !== "") {
    dRng = dRng.filter(function(e){ return e[4] === report.getRange("E3").getValue()});
  }
  if (report.getRange("J3").getValue() !== "") {
    dRng = dRng.filter(function(e){ return e[9] === report.getRange("J3").getDisplayValue()});
  }
  if (report.getRange("L3").getValue() !== "") {
    dRng = dRng.filter(function(e){ return e[11] === report.getRange("L3").getValue()});
  }
  
  report.getRange(4 ,1 ,dRng.length, 14).setValues(dRng);
}  

strong text

CodePudding user response:

If you make the following assignment:

const [b,c,d,e,,,,,j,,l] = sheet.getRange("B3:L3").getValues()[0];

then you can make the following comparisons. Thus saving the time of several getValue() calls

e[1] === b
e[2] === c
e[3] === d
e[4] === e
e[9] === j
e[11] === l

I hope you get the idea. I did not wish to copy the entire thing.

  • Related