I have this Google sheet that contains 3 columns, data with three columns Here is the data: Data Source I wrote a script to filter for when column A and column C meets certain criteria The script then produces the result in sheet 'PharmacyV1'. The result was generated as expected, as there are 14 rows in this sheet.
The next part of the script, I basically filtered for the exact opposite of the first criteria, essentially grabbing data that wasn't filtered to sheet 'Pharmacy V1' and rewrite it to the original sheet 'V1'.
My Original V1 sheet contains 1,631 rows of data (headers excluded)
My PharmacyV1 sheet filtered out 14 rows of data
But the V1 result ended up with 1,575 rows of data, but I should expect 1631-14 = 1617 rows.
Can someone please point out how or why there is such a discrepancy?
function S02_FilterPharmacy_Copy2NewSheet() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('V1');
var firstRow = sheet.getRange(1,1,1,sheet.getLastColumn()).getValues();
var C1 = firstRow[0].indexOf('TxnUser');
var C2 = firstRow[0].indexOf('Location');
// Filter for IV therapy and pharmacy users using filtered Range:
var rangeVals = sheet.getRange(1,1,sheet.getLastRow()-1,sheet.getLastColumn()).getValues();
var filteredRange = rangeVals.filter(function(val){
return val[C2] === "IV THERAPY" && (val[C1] === "DEOK645"|| val[C1] === "JETH850" ||
val[C1] === "JOON409"|| val[C1] === "THAM1452");
});
var targetSheet = ss.insertSheet('PharmacyV1');
targetSheet.getRange(2,1,filteredRange.length,filteredRange[0].length).setValues(filteredRange);
var sheet = ss.getSheetByName('V1');
targetSheet.getRange(1,1,1,sheet.getLastColumn()).setValues(firstRow);
//Filter and replace original V1
var rangeVals = sheet.getRange(1,1,sheet.getLastRow()-1,sheet.getLastColumn()).getValues();
var filteredRange = rangeVals.filter(function(val){
return val[C2] != "IV THERAPY" && (val[C1] != "JOON409"|| val[C1] != "JETH850" ||
val[C1] != "JOON409"|| val[C1] != "THAM1452");
});
var RANGE = sheet.getDataRange(); RANGE.clearContent();
var newRange = sheet.getRange(1,1,filteredRange.length, filteredRange[0].length);
newRange.setValues(filteredRange);
}