Excel.RangeAreas class

RangeAreas represents a collection of one or more rectangular ranges in the same worksheet. To learn how to use discontiguous ranges, read Work with multiple ranges simultaneously in Excel add-ins.

Extends

Remarks

[ API set: ExcelApi 1.9 ]

Properties

address

Returns the RangeAreas reference in A1-style. Address value will contain the worksheet name for each rectangular block of cells (e.g., "Sheet1!A1:B4, Sheet1!D1:D4").

addressLocal

Returns the RangeAreas reference in the user locale.

areaCount

Returns the number of rectangular ranges that comprise this RangeAreas object.

areas

Returns a collection of rectangular ranges that comprise this RangeAreas object.

cellCount

Returns the number of cells in the RangeAreas object, summing up the cell counts of all of the individual rectangular ranges. Returns -1 if the cell count exceeds 2^31-1 (2,147,483,647).

conditionalFormats

Returns a collection of conditional formats that intersect with any cells in this RangeAreas object.

context

The request context associated with the object. This connects the add-in's process to the Office host application's process.

dataValidation

Returns a data validation object for all ranges in the RangeAreas.

format

Returns a RangeFormat object, encapsulating the font, fill, borders, alignment, and other properties for all ranges in the RangeAreas object.

isEntireColumn

Specifies if all the ranges on this RangeAreas object represent entire columns (e.g., "A:C, Q:Z").

isEntireRow

Specifies if all the ranges on this RangeAreas object represent entire rows (e.g., "1:3, 5:7").

style

Represents the style for all ranges in this RangeAreas object. If the styles of the cells are inconsistent, null will be returned. For custom styles, the style name will be returned. For built-in styles, a string representing a value in the BuiltInStyle enum will be returned.

worksheet

Returns the worksheet for the current RangeAreas.

Methods

calculate()

Calculates all cells in the RangeAreas.

clear(applyTo)

Clears values, format, fill, border, and other properties on each of the areas that comprise this RangeAreas object.

clear(applyToString)

Clears values, format, fill, border, and other properties on each of the areas that comprise this RangeAreas object.

convertDataTypeToText()

Converts all cells in the RangeAreas with data types into text.

convertToLinkedDataType(serviceID, languageCulture)

Converts all cells in the RangeAreas into linked data types.

copyFrom(sourceRange, copyType, skipBlanks, transpose)

Copies cell data or formatting from the source range or RangeAreas to the current RangeAreas. The destination RangeAreas can be a different size than the source range or RangeAreas. The destination will be expanded automatically if it is smaller than the source.

copyFrom(sourceRange, copyTypeString, skipBlanks, transpose)

Copies cell data or formatting from the source range or RangeAreas to the current RangeAreas. The destination RangeAreas can be a different size than the source range or RangeAreas. The destination will be expanded automatically if it is smaller than the source.

getEntireColumn()

Returns a RangeAreas object that represents the entire columns of the RangeAreas (for example, if the current RangeAreas represents cells "B4:E11, H2", it returns a RangeAreas that represents columns "B:E, H:H").

getEntireRow()

Returns a RangeAreas object that represents the entire rows of the RangeAreas (for example, if the current RangeAreas represents cells "B4:E11", it returns a RangeAreas that represents rows "4:11").

getIntersection(anotherRange)

Returns the RangeAreas object that represents the intersection of the given ranges or RangeAreas. If no intersection is found, an ItemNotFound error will be thrown.

getIntersectionOrNullObject(anotherRange)

Returns the RangeAreas object that represents the intersection of the given ranges or RangeAreas. If no intersection is found, then this method returns an object with its isNullObject property set to true. For further information, see *OrNullObject methods and properties.

getOffsetRangeAreas(rowOffset, columnOffset)

Returns a RangeAreas object that is shifted by the specific row and column offset. The dimension of the returned RangeAreas will match the original object. If the resulting RangeAreas is forced outside the bounds of the worksheet grid, an error will be thrown.

getSpecialCells(cellType, cellValueType)

Returns a RangeAreas object that represents all the cells that match the specified type and value. Throws an error if no special cells are found that match the criteria.

getSpecialCells(cellTypeString, cellValueTypeString)

Returns a RangeAreas object that represents all the cells that match the specified type and value. Throws an error if no special cells are found that match the criteria.

getSpecialCellsOrNullObject(cellType, cellValueType)

Returns a RangeAreas object that represents all the cells that match the specified type and value. If no special cells are found that match the criteria, then this method returns an object with its isNullObject property set to true. For further information, see *OrNullObject methods and properties.

getSpecialCellsOrNullObject(cellTypeString, cellValueTypeString)

Returns a RangeAreas object that represents all the cells that match the specified type and value. If no special cells are found that match the criteria, then this method returns an object with its isNullObject property set to true. For further information, see *OrNullObject methods and properties.

getTables(fullyContained)

Returns a scoped collection of tables that overlap with any range in this RangeAreas object.

getUsedRangeAreas(valuesOnly)

Returns the used RangeAreas that comprises all the used areas of individual rectangular ranges in the RangeAreas object. If there are no used cells within the RangeAreas, the ItemNotFound error will be thrown.

getUsedRangeAreasOrNullObject(valuesOnly)

Returns the used RangeAreas that comprises all the used areas of individual rectangular ranges in the RangeAreas object. If there are no used cells within the RangeAreas, then this method returns an object with its isNullObject property set to true. For further information, see *OrNullObject methods and properties.

load(options)

Queues up a command to load the specified properties of the object. You must call context.sync() before reading the properties.

load(propertyNames)

Queues up a command to load the specified properties of the object. You must call context.sync() before reading the properties.

load(propertyNamesAndPaths)

Queues up a command to load the specified properties of the object. You must call context.sync() before reading the properties.

set(properties, options)

Sets multiple properties of an object at the same time. You can pass either a plain object with the appropriate properties, or another API object of the same type.

set(properties)

Sets multiple properties on the object at the same time, based on an existing loaded object.

setDirty()

Sets the RangeAreas to be recalculated when the next recalculation occurs.

toJSON()

Overrides the JavaScript toJSON() method in order to provide more useful output when an API object is passed to JSON.stringify(). (JSON.stringify, in turn, calls the toJSON method of the object that is passed to it.) Whereas the original Excel.RangeAreas object is an API object, the toJSON method returns a plain JavaScript object (typed as Excel.Interfaces.RangeAreasData) that contains shallow copies of any loaded child properties from the original object.

track()

Track the object for automatic adjustment based on surrounding changes in the document. This call is a shorthand for context.trackedObjects.add(thisObject). If you are using this object across .sync calls and outside the sequential execution of a ".run" batch, and get an "InvalidObjectPath" error when setting a property or invoking a method on the object, you need to add the object to the tracked object collection when the object was first created.

untrack()

Release the memory associated with this object, if it has previously been tracked. This call is shorthand for context.trackedObjects.remove(thisObject). Having many tracked objects slows down the host application, so please remember to free any objects you add, once you're done using them. You will need to call context.sync() before the memory release takes effect.

Property Details

address

Returns the RangeAreas reference in A1-style. Address value will contain the worksheet name for each rectangular block of cells (e.g., "Sheet1!A1:B4, Sheet1!D1:D4").

readonly address: string;

Property Value

string

Remarks

[ API set: ExcelApi 1.9 ]

addressLocal

Returns the RangeAreas reference in the user locale.

readonly addressLocal: string;

Property Value

string

Remarks

[ API set: ExcelApi 1.9 ]

areaCount

Returns the number of rectangular ranges that comprise this RangeAreas object.

readonly areaCount: number;

Property Value

number

Remarks

[ API set: ExcelApi 1.9 ]

areas

Returns a collection of rectangular ranges that comprise this RangeAreas object.

readonly areas: Excel.RangeCollection;

Property Value

Remarks

[ API set: ExcelApi 1.9 ]

cellCount

Returns the number of cells in the RangeAreas object, summing up the cell counts of all of the individual rectangular ranges. Returns -1 if the cell count exceeds 2^31-1 (2,147,483,647).

readonly cellCount: number;

Property Value

number

Remarks

[ API set: ExcelApi 1.9 ]

conditionalFormats

Returns a collection of conditional formats that intersect with any cells in this RangeAreas object.

readonly conditionalFormats: Excel.ConditionalFormatCollection;

Property Value

Remarks

[ API set: ExcelApi 1.9 ]

context

The request context associated with the object. This connects the add-in's process to the Office host application's process.

context: RequestContext;

Property Value

dataValidation

Returns a data validation object for all ranges in the RangeAreas.

readonly dataValidation: Excel.DataValidation;

Property Value

Remarks

[ API set: ExcelApi 1.9 ]

format

Returns a RangeFormat object, encapsulating the font, fill, borders, alignment, and other properties for all ranges in the RangeAreas object.

readonly format: Excel.RangeFormat;

Property Value

Remarks

[ API set: ExcelApi 1.9 ]

Examples

// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/excel/42-range/range-areas.yaml

await Excel.run(async (context) => {

    const sheet = context.workbook.worksheets.getActiveWorksheet();
    const usedRange = sheet.getUsedRange();

    // Find the ranges with formulas.
    const formulaRanges = usedRange.getSpecialCells("Formulas");
    formulaRanges.format.fill.color = "lightgreen";

    await context.sync();
});

isEntireColumn

Specifies if all the ranges on this RangeAreas object represent entire columns (e.g., "A:C, Q:Z").

readonly isEntireColumn: boolean;

Property Value

boolean

Remarks

[ API set: ExcelApi 1.9 ]

isEntireRow

Specifies if all the ranges on this RangeAreas object represent entire rows (e.g., "1:3, 5:7").

readonly isEntireRow: boolean;

Property Value

boolean

Remarks

[ API set: ExcelApi 1.9 ]

style

Represents the style for all ranges in this RangeAreas object. If the styles of the cells are inconsistent, null will be returned. For custom styles, the style name will be returned. For built-in styles, a string representing a value in the BuiltInStyle enum will be returned.

style: string;

Property Value

string

Remarks

[ API set: ExcelApi 1.9 ]

worksheet

Returns the worksheet for the current RangeAreas.

readonly worksheet: Excel.Worksheet;

Property Value

Remarks

[ API set: ExcelApi 1.9 ]

Method Details

calculate()

Calculates all cells in the RangeAreas.

calculate(): void;

Returns

void

Remarks

[ API set: ExcelApi 1.9 ]

clear(applyTo)

Clears values, format, fill, border, and other properties on each of the areas that comprise this RangeAreas object.

clear(applyTo?: Excel.ClearApplyTo): void;

Parameters

applyTo
Excel.ClearApplyTo

Optional. Determines the type of clear action. See Excel.ClearApplyTo for details. Default is "All".

Returns

void

Remarks

[ API set: ExcelApi 1.9 ]

clear(applyToString)

Clears values, format, fill, border, and other properties on each of the areas that comprise this RangeAreas object.

clear(applyToString?: "All" | "Formats" | "Contents" | "Hyperlinks" | "RemoveHyperlinks"): void;

Parameters

applyToString

"All" | "Formats" | "Contents" | "Hyperlinks" | "RemoveHyperlinks"

Optional. Determines the type of clear action. See Excel.ClearApplyTo for details. Default is "All".

Returns

void

Remarks

[ API set: ExcelApi 1.9 ]

convertDataTypeToText()

Converts all cells in the RangeAreas with data types into text.

convertDataTypeToText(): void;

Returns

void

Remarks

[ API set: ExcelApi 1.9 ]

convertToLinkedDataType(serviceID, languageCulture)

Converts all cells in the RangeAreas into linked data types.

convertToLinkedDataType(serviceID: number, languageCulture: string): void;

Parameters

serviceID

number

The service ID which will be used to query the data.

languageCulture

string

Language culture to query the service for.

Returns

void

Remarks

[ API set: ExcelApi 1.9 ]

copyFrom(sourceRange, copyType, skipBlanks, transpose)

Copies cell data or formatting from the source range or RangeAreas to the current RangeAreas. The destination RangeAreas can be a different size than the source range or RangeAreas. The destination will be expanded automatically if it is smaller than the source.

copyFrom(sourceRange: Range | RangeAreas | string, copyType?: Excel.RangeCopyType, skipBlanks?: boolean, transpose?: boolean): void;

Parameters

sourceRange

Excel.Range | Excel.RangeAreas | string

The source range or RangeAreas to copy from. When the source RangeAreas has multiple ranges, their form must able to be created by removing full rows or columns from a rectangular range.

copyType
Excel.RangeCopyType

The type of cell data or formatting to copy over. Default is "All".

skipBlanks

boolean

True if to skip blank cells in the source range or RangeAreas. Default is false.

transpose

boolean

True if to transpose the cells in the destination RangeAreas. Default is false.

Returns

void

Remarks

[ API set: ExcelApi 1.9 ]

copyFrom(sourceRange, copyTypeString, skipBlanks, transpose)

Copies cell data or formatting from the source range or RangeAreas to the current RangeAreas. The destination RangeAreas can be a different size than the source range or RangeAreas. The destination will be expanded automatically if it is smaller than the source.

copyFrom(sourceRange: Range | RangeAreas | string, copyTypeString?: "All" | "Formulas" | "Values" | "Formats" | "Link" | "ColumnWidths", skipBlanks?: boolean, transpose?: boolean): void;

Parameters

sourceRange

Excel.Range | Excel.RangeAreas | string

The source range or RangeAreas to copy from. When the source RangeAreas has multiple ranges, their form must able to be created by removing full rows or columns from a rectangular range.

copyTypeString

"All" | "Formulas" | "Values" | "Formats" | "Link" | "ColumnWidths"

The type of cell data or formatting to copy over. Default is "All".

skipBlanks

boolean

True if to skip blank cells in the source range or RangeAreas. Default is false.

transpose

boolean

True if to transpose the cells in the destination RangeAreas. Default is false.

Returns

void

Remarks

[ API set: ExcelApi 1.9 ]

getEntireColumn()

Returns a RangeAreas object that represents the entire columns of the RangeAreas (for example, if the current RangeAreas represents cells "B4:E11, H2", it returns a RangeAreas that represents columns "B:E, H:H").

getEntireColumn(): Excel.RangeAreas;

Returns

Remarks

[ API set: ExcelApi 1.9 ]

getEntireRow()

Returns a RangeAreas object that represents the entire rows of the RangeAreas (for example, if the current RangeAreas represents cells "B4:E11", it returns a RangeAreas that represents rows "4:11").

getEntireRow(): Excel.RangeAreas;

Returns

Remarks

[ API set: ExcelApi 1.9 ]

getIntersection(anotherRange)

Returns the RangeAreas object that represents the intersection of the given ranges or RangeAreas. If no intersection is found, an ItemNotFound error will be thrown.

getIntersection(anotherRange: Range | RangeAreas | string): Excel.RangeAreas;

Parameters

anotherRange

Excel.Range | Excel.RangeAreas | string

The range, RangeAreas object, or range address that will be used to determine the intersection.

Returns

Remarks

[ API set: ExcelApi 1.9 ]

getIntersectionOrNullObject(anotherRange)

Returns the RangeAreas object that represents the intersection of the given ranges or RangeAreas. If no intersection is found, then this method returns an object with its isNullObject property set to true. For further information, see *OrNullObject methods and properties.

getIntersectionOrNullObject(anotherRange: Range | RangeAreas | string): Excel.RangeAreas;

Parameters

anotherRange

Excel.Range | Excel.RangeAreas | string

The range, RangeAreas object, or address that will be used to determine the intersection.

Returns

Remarks

[ API set: ExcelApi 1.9 ]

getOffsetRangeAreas(rowOffset, columnOffset)

Returns a RangeAreas object that is shifted by the specific row and column offset. The dimension of the returned RangeAreas will match the original object. If the resulting RangeAreas is forced outside the bounds of the worksheet grid, an error will be thrown.

getOffsetRangeAreas(rowOffset: number, columnOffset: number): Excel.RangeAreas;

Parameters

rowOffset

number

The number of rows (positive, negative, or 0) by which the RangeAreas is to be offset. Positive values are offset downward, and negative values are offset upward.

columnOffset

number

The number of columns (positive, negative, or 0) by which the RangeAreas is to be offset. Positive values are offset to the right, and negative values are offset to the left.

Returns

Remarks

[ API set: ExcelApi 1.9 ]

getSpecialCells(cellType, cellValueType)

Returns a RangeAreas object that represents all the cells that match the specified type and value. Throws an error if no special cells are found that match the criteria.

getSpecialCells(cellType: Excel.SpecialCellType, cellValueType?: Excel.SpecialCellValueType): Excel.RangeAreas;

Parameters

cellType
Excel.SpecialCellType

The type of cells to include.

cellValueType
Excel.SpecialCellValueType

If cellType is either constants or formulas, this argument is used to determine which types of cells to include in the result. These values can be combined together to return more than one type. The default is to select all constants or formulas, no matter what the type.

Returns

Remarks

[ API set: ExcelApi 1.9 ]

getSpecialCells(cellTypeString, cellValueTypeString)

Returns a RangeAreas object that represents all the cells that match the specified type and value. Throws an error if no special cells are found that match the criteria.

getSpecialCells(cellTypeString: "ConditionalFormats" | "DataValidations" | "Blanks" | "Constants" | "Formulas" | "SameConditionalFormat" | "SameDataValidation" | "Visible", cellValueTypeString?: "All" | "Errors" | "ErrorsLogical" | "ErrorsNumbers" | "ErrorsText" | "ErrorsLogicalNumber" | "ErrorsLogicalText" | "ErrorsNumberText" | "Logical" | "LogicalNumbers" | "LogicalText" | "LogicalNumbersText" | "Numbers" | "NumbersText" | "Text"): Excel.RangeAreas;

Parameters

cellTypeString

"ConditionalFormats" | "DataValidations" | "Blanks" | "Constants" | "Formulas" | "SameConditionalFormat" | "SameDataValidation" | "Visible"

The type of cells to include.

cellValueTypeString

"All" | "Errors" | "ErrorsLogical" | "ErrorsNumbers" | "ErrorsText" | "ErrorsLogicalNumber" | "ErrorsLogicalText" | "ErrorsNumberText" | "Logical" | "LogicalNumbers" | "LogicalText" | "LogicalNumbersText" | "Numbers" | "NumbersText" | "Text"

If cellType is either constants or formulas, this argument is used to determine which types of cells to include in the result. These values can be combined together to return more than one type. The default is to select all constants or formulas, no matter what the type.

Returns

Remarks

[ API set: ExcelApi 1.9 ]

getSpecialCellsOrNullObject(cellType, cellValueType)

Returns a RangeAreas object that represents all the cells that match the specified type and value. If no special cells are found that match the criteria, then this method returns an object with its isNullObject property set to true. For further information, see *OrNullObject methods and properties.

getSpecialCellsOrNullObject(cellType: Excel.SpecialCellType, cellValueType?: Excel.SpecialCellValueType): Excel.RangeAreas;

Parameters

cellType
Excel.SpecialCellType

The type of cells to include.

cellValueType
Excel.SpecialCellValueType

If cellType is either constants or formulas, this argument is used to determine which types of cells to include in the result. These values can be combined together to return more than one type. The default is to select all constants or formulas, no matter what the type.

Returns

Remarks

[ API set: ExcelApi 1.9 ]

getSpecialCellsOrNullObject(cellTypeString, cellValueTypeString)

Returns a RangeAreas object that represents all the cells that match the specified type and value. If no special cells are found that match the criteria, then this method returns an object with its isNullObject property set to true. For further information, see *OrNullObject methods and properties.

getSpecialCellsOrNullObject(cellTypeString: "ConditionalFormats" | "DataValidations" | "Blanks" | "Constants" | "Formulas" | "SameConditionalFormat" | "SameDataValidation" | "Visible", cellValueTypeString?: "All" | "Errors" | "ErrorsLogical" | "ErrorsNumbers" | "ErrorsText" | "ErrorsLogicalNumber" | "ErrorsLogicalText" | "ErrorsNumberText" | "Logical" | "LogicalNumbers" | "LogicalText" | "LogicalNumbersText" | "Numbers" | "NumbersText" | "Text"): Excel.RangeAreas;

Parameters

cellTypeString

"ConditionalFormats" | "DataValidations" | "Blanks" | "Constants" | "Formulas" | "SameConditionalFormat" | "SameDataValidation" | "Visible"

The type of cells to include.

cellValueTypeString

"All" | "Errors" | "ErrorsLogical" | "ErrorsNumbers" | "ErrorsText" | "ErrorsLogicalNumber" | "ErrorsLogicalText" | "ErrorsNumberText" | "Logical" | "LogicalNumbers" | "LogicalText" | "LogicalNumbersText" | "Numbers" | "NumbersText" | "Text"

If cellType is either constants or formulas, this argument is used to determine which types of cells to include in the result. These values can be combined together to return more than one type. The default is to select all constants or formulas, no matter what the type.

Returns

Remarks

[ API set: ExcelApi 1.9 ]

getTables(fullyContained)

Returns a scoped collection of tables that overlap with any range in this RangeAreas object.

getTables(fullyContained?: boolean): Excel.TableScopedCollection;

Parameters

fullyContained

boolean

If true, returns only tables that are fully contained within the range bounds. Default is false.

Returns

Remarks

[ API set: ExcelApi 1.9 ]

getUsedRangeAreas(valuesOnly)

Returns the used RangeAreas that comprises all the used areas of individual rectangular ranges in the RangeAreas object. If there are no used cells within the RangeAreas, the ItemNotFound error will be thrown.

getUsedRangeAreas(valuesOnly?: boolean): Excel.RangeAreas;

Parameters

valuesOnly

boolean

Whether to only consider cells with values as used cells. Default is false.

Returns

Remarks

[ API set: ExcelApi 1.9 ]

getUsedRangeAreasOrNullObject(valuesOnly)

Returns the used RangeAreas that comprises all the used areas of individual rectangular ranges in the RangeAreas object. If there are no used cells within the RangeAreas, then this method returns an object with its isNullObject property set to true. For further information, see *OrNullObject methods and properties.

getUsedRangeAreasOrNullObject(valuesOnly?: boolean): Excel.RangeAreas;

Parameters

valuesOnly

boolean

Whether to only consider cells with values as used cells.

Returns

Remarks

[ API set: ExcelApi 1.9 ]

load(options)

Queues up a command to load the specified properties of the object. You must call context.sync() before reading the properties.

load(options?: Excel.Interfaces.RangeAreasLoadOptions): Excel.RangeAreas;

Parameters

options
Excel.Interfaces.RangeAreasLoadOptions

Provides options for which properties of the object to load.

Returns

load(propertyNames)

Queues up a command to load the specified properties of the object. You must call context.sync() before reading the properties.

load(propertyNames?: string | string[]): Excel.RangeAreas;

Parameters

propertyNames

string | string[]

A comma-delimited string or an array of strings that specify the properties to load.

Returns

load(propertyNamesAndPaths)

Queues up a command to load the specified properties of the object. You must call context.sync() before reading the properties.

load(propertyNamesAndPaths?: {
            select?: string;
            expand?: string;
        }): Excel.RangeAreas;

Parameters

propertyNamesAndPaths

{ select?: string; expand?: string; }

propertyNamesAndPaths.select is a comma-delimited string that specifies the properties to load, and propertyNamesAndPaths.expand is a comma-delimited string that specifies the navigation properties to load.

Returns

set(properties, options)

Sets multiple properties of an object at the same time. You can pass either a plain object with the appropriate properties, or another API object of the same type.

set(properties: Interfaces.RangeAreasUpdateData, options?: OfficeExtension.UpdateOptions): void;

Parameters

properties
Excel.Interfaces.RangeAreasUpdateData

A JavaScript object with properties that are structured isomorphically to the properties of the object on which the method is called.

options
OfficeExtension.UpdateOptions

Provides an option to suppress errors if the properties object tries to set any read-only properties.

Returns

void

set(properties)

Sets multiple properties on the object at the same time, based on an existing loaded object.

set(properties: Excel.RangeAreas): void;

Parameters

properties
Excel.RangeAreas

Returns

void

setDirty()

Sets the RangeAreas to be recalculated when the next recalculation occurs.

setDirty(): void;

Returns

void

Remarks

[ API set: ExcelApi 1.9 ]

toJSON()

Overrides the JavaScript toJSON() method in order to provide more useful output when an API object is passed to JSON.stringify(). (JSON.stringify, in turn, calls the toJSON method of the object that is passed to it.) Whereas the original Excel.RangeAreas object is an API object, the toJSON method returns a plain JavaScript object (typed as Excel.Interfaces.RangeAreasData) that contains shallow copies of any loaded child properties from the original object.

toJSON(): Excel.Interfaces.RangeAreasData;

Returns

track()

Track the object for automatic adjustment based on surrounding changes in the document. This call is a shorthand for context.trackedObjects.add(thisObject). If you are using this object across .sync calls and outside the sequential execution of a ".run" batch, and get an "InvalidObjectPath" error when setting a property or invoking a method on the object, you need to add the object to the tracked object collection when the object was first created.

track(): Excel.RangeAreas;

Returns

untrack()

Release the memory associated with this object, if it has previously been tracked. This call is shorthand for context.trackedObjects.remove(thisObject). Having many tracked objects slows down the host application, so please remember to free any objects you add, once you're done using them. You will need to call context.sync() before the memory release takes effect.

untrack(): Excel.RangeAreas;

Returns