How to hide rows that contain date in column A that is older than one week ago - excel online (Not VBA)

24 views Asked by At

I am working on an excel online sheet which has a large number of rows. column A contains a date. I need to have a script that hides all the rows in which the value in column A (date) is less that 7 days ago.

This is the script I have:

function main(workbook: ExcelScript.Workbook) {
  const mainDataSheet = workbook.getWorksheet("Main data");
  var mainData = mainDataSheet.getUsedRange();
  var mainDataLastRow = mainDataSheet.getUsedRange(true).getLastRow().getRowIndex();
  let mainDataRangeValues = mainData.getValues(); // // Save the values locally to avoid repeatedly asking the workbook.
  let today = new Date();
  let lastWeek = new Date(today.getTime() - 7 * 24 * 60 * 60 * 1000); // Calculate one week ago
  let r = 0
  for (var i = 1; i < mainDataRangeValues.length; i++) { // Assuming the first row is for headers
  var dateInColumnA = mainDataRangeValues[i][0]; // Assuming column A contains dates

    if (dateInColumnA < lastWeek) {
      r = r+1
    }
  mainDataSheet.getRange("2:r").setRowHidden(true);

  }
}

This is the output error: Line 15: Worksheet getRange: The argument is invalid or missing or has an incorrect format.

This is the problem list: [12, 9] Operator '<' cannot be applied to types 'string | number | boolean' and 'Date'.

I have tried searching online for solutions but can only find ones which are VBA or googlescript.

0

There are 0 answers