'Office Scripts- How to delete last row

I am trying to delete the last row in my Excel Online document using Office Scripts.

I do not know what I am doing wrong, it is not deleting, does anyone have an idea?

My code:

function main(workbook: ExcelScript.Workbook) {
    let selectedSheet = workbook.getActiveWorksheet();
    //getting the used range in the worksheet
    let usedRange = workbook.getActiveWorksheet().getUsedRange();
    //getting the row count of the used range
    let lastRow = usedRange.getRowCount();
    //getting the row count of the range
    let rowCount = usedRange.getRowCount();
    usedRange.getCell(lastRow, 0).getEntireRow().delete(ExcelScript.DeleteShiftDirection.up)
}


Solution 1:[1]

This may be a little more foolproof ...

function main(workbook: ExcelScript.Workbook) {
  let selectedSheet = workbook.getActiveWorksheet();
  let usedRange = selectedSheet.getUsedRange();  
  let lastRowAddress = usedRange.getLastCell().getAddress();
  selectedSheet.getRange(lastRowAddress).getEntireRow().delete(ExcelScript.DeleteShiftDirection.up);
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1