Skip to content

Latest commit

 

History

History
64 lines (41 loc) · 1.99 KB

excel-add-ins-ranges-clear-delete.md

File metadata and controls

64 lines (41 loc) · 1.99 KB
title description ms.date ms.localizationpriority
Clear or delete ranges using the Excel JavaScript API
Learn how to clear or delete ranges using the Excel JavaScript API.
02/16/2022
medium

Clear or delete ranges using the Excel JavaScript API

This article provides code samples that clear and delete ranges with the Excel JavaScript API. For the complete list of properties and methods supported by the Range object, see Excel.Range class.

[!includeExcel cells and ranges note]

Clear a range of cells

The following code sample clears all contents and formatting of cells in the range E2:E5.

await Excel.run(async (context) => {
    let sheet = context.workbook.worksheets.getItem("Sample");
    let range = sheet.getRange("E2:E5");

    range.clear();

    await context.sync();
});

Data before range is cleared

Data in Excel before range is cleared.

Data after range is cleared

Data in Excel after range is cleared.

Delete a range of cells

The following code sample deletes the cells in the range B4:E4 and shifts other cells up to fill the space that was vacated by the deleted cells.

await Excel.run(async (context) => {
    let sheet = context.workbook.worksheets.getItem("Sample");
    let range = sheet.getRange("B4:E4");

    range.delete(Excel.DeleteShiftDirection.up);

    await context.sync();
});

Data before range is deleted

Data in Excel before range is deleted.

Data after range is deleted

Data in Excel after range is deleted.

See also