Fixing an office script to filter a list populated by xlookup formula
I'm writing an Office Script to click a button and further filter a list (which is already filtered via xlookup). I want the script to show only the employees who's training is out of date.
I receive the "AutoFilter apply: The argument is invalid or missing or has an incorrect format" output message when I run the following script. It does not produce a "Problem" output.
Is anyone able to spot where I'm messing this up?
function main(workbook: ExcelScript.Workbook) {
let selectedCell = workbook.getActiveCell(); //may not need this line
let selectedSheet = workbook.getActiveWorksheet();
let range = selectedSheet.getRange("A13:C160"); //column headers in row 13: Employee Email, CPR Status, Next Enrolled Date
let autoFilter = selectedSheet.getAutoFilter();
//want to clear any prior filter before the new filter gets applied
if (autoFilter) {selectedSheet.getAutoFilter().clearCriteria();}
if (!autoFilter) {autoFilter = selectedSheet.getAutoFilter();}
let today = new Date("I2"); //don't think I need this line
let expireDate = new Date("J2");
//let formattedDate = expireDate.toLocaleDateString("en-US"); I don't know if this is necessary or not
//I want the filter to return employees who either have no complete date in the "CPR Status" column AND the people whose training date is expire.
const filterCriteria: ExcelScript.FilterCriteria = {
filterOn: ExcelScript.FilterOn.custom,
criterion1: '<==$expireDate', //the script runs with "" and '' - I don't know what's better
criterion2: "",
operator: ExcelScript.FilterOperator.or
}
autoFilter.apply(range, 1, filterCriteria)
}
The old VBA code that worked in the desktop version of excel looked like this:
Private Sub FilterCPR_Click()
Dim sDate As Date '(don't actually need this)
Dim eDate As Date
sDate = Range("J2").Value
eDate = Range("I2").Value '(don't actually need this either)
ActiveSheet.Range("B13:C160").AutoFilter 1, "<=" & sDate, xlOr, ""
End Sub
Thank you for any help!!