Skip to content

Latest commit

 

History

History
82 lines (57 loc) · 2.92 KB

excel-add-ins-ranges-get.md

File metadata and controls

82 lines (57 loc) · 2.92 KB
title description ms.date ms.localizationpriority
Get a range using the Excel JavaScript API
Learn how to retrieve a range using the Excel JavaScript API.
02/17/2022
medium

Get a range using the Excel JavaScript API

This article provides examples that show different ways to get a range within a worksheet using the Excel JavaScript API. For the complete list of properties and methods that the Range object supports, see Excel.Range class.

[!includeExcel cells and ranges note]

Get range by address

The following code sample gets the range with address B2:C5 from the worksheet named Sample, loads its address property, and writes a message to the console.

await Excel.run(async (context) => {
    let sheet = context.workbook.worksheets.getItem("Sample");
    
    let range = sheet.getRange("B2:C5");
    range.load("address");
    await context.sync();
    
    console.log(`The address of the range B2:C5 is "${range.address}"`);
});

Get range by name

The following code sample gets the range named MyRange from the worksheet named Sample, loads its address property, and writes a message to the console.

await Excel.run(async (context) => {
    let sheet = context.workbook.worksheets.getItem("Sample");

    let range = sheet.getRange("MyRange");
    range.load("address");
    await context.sync();

    console.log(`The address of the range "MyRange" is "${range.address}"`);
});

Get used range

The following code sample gets the used range from the worksheet named Sample, loads its address property, and writes a message to the console. The used range is the smallest range that encompasses any cells in the worksheet that have a value or formatting assigned to them. If the entire worksheet is blank, the getUsedRange() method returns a range that consists of only the top-left cell.

await Excel.run(async (context) => {
    let sheet = context.workbook.worksheets.getItem("Sample");

    let range = sheet.getUsedRange();
    range.load("address");
    await context.sync();
    
    console.log(`The address of the used range in the worksheet is "${range.address}"`);
});

Get entire range

The following code sample gets the entire worksheet range from the worksheet named Sample, loads its address property, and writes a message to the console.

await Excel.run(async (context) => {
    let sheet = context.workbook.worksheets.getItem("Sample");

    let range = sheet.getRange();
    range.load("address");
    await context.sync();
    
    console.log(`The address of the entire worksheet range is "${range.address}"`);
});

See also