Range represents a set of one or more contiguous cells such as a cell, a row, a column, block of cells, etc.
Property | Type | Description |
---|---|---|
address | string | Represents the range reference in A1-style. Address value will contain the Sheet reference (e.g. Sheet1!A1:B4). Read-only. |
addressLocal | string | Represents range reference for the specified range in the language of the user. Read-only. |
cellCount | int | Number of cells in the range. Read-only. |
columnCount | int | Represents the total number of columns in the range. Read-only. |
columnIndex | int | Represents the column number of the first cell in the range. Zero-indexed. Read-only. |
formulas | object[][] | Represents the formula in A1-style notation. |
formulasLocal | object[][] | Represents the formula in A1-style notation, in the user's language and number-formatting locale. For example, the English "=SUM(A1, 1.5)" formula would become "=SUMME(A1; 1,5)" in German. |
numberFormat | object[][] | Represents Excel's number format code for the given cell. |
rowCount | int | Returns the total number of rows in the range. Read-only. |
rowIndex | int | Returns the row number of the first cell in the range. Zero-indexed. Read-only. |
text | object[][] | Text values of the specified range. The Text value will not depend on the cell width. The # sign substitution that happens in Excel UI will not affect the text value returned by the API. Read-only. |
valueTypes | string | Represents the type of data of each cell. Read-only. Possible values are: Unknown, Empty, String, Integer, Double, Boolean, Error. |
values | object[][] | Represents the raw values of the specified range. The data returned could be of type string, number, or a boolean. Cell that contain an error will return the error string. |
Relationship | Type | Description |
---|---|---|
format | RangeFormat | Returns a format object, encapsulating the range's font, fill, borders, alignment, and other properties. Read-only. |
worksheet | Worksheet | The worksheet containing the current range. Read-only. |
Method | Return Type | Description |
---|---|---|
clear(applyTo: string) | void | Clear range values, format, fill, border, etc. |
delete(shift: string) | void | Deletes the cells associated with the range. |
getBoundingRect(anotherRange: object) | Range | Gets the smallest range object that encompasses the given ranges. For example, the GetBoundingRect of "B2:C5" and "D10:E15" is "B2:E16". |
getCell(row: number, column: number) | Range | Gets the range object containing the single cell based on row and column numbers. The cell can be outside the bounds of its parent range, so long as it's stays within the worksheet grid. The returned cell is located relative to the top left cell of the range. |
getColumn(column: number) | Range | Gets a column contained in the range. |
getEntireColumn() | Range | Gets an object that represents the entire column of the range. |
getEntireRow() | Range | Gets an object that represents the entire row of the range. |
getIntersection(anotherRange: object) | Range | Gets the range object that represents the rectangular intersection of the given ranges. |
getLastCell() | Range | Gets the last cell within the range. For example, the last cell of "B2:D5" is "D5". |
getLastColumn() | Range | Gets the last column within the range. For example, the last column of "B2:D5" is "D2:D5". |
getLastRow() | Range | Gets the last row within the range. For example, the last row of "B2:D5" is "B5:D5". |
getOffsetRange(rowOffset: number, columnOffset: number) | Range | Gets an object which represents a range that's offset from the specified range. The dimension of the returned range will match this range. If the resulting range is forced outside the bounds of the worksheet grid, an exception will be thrown. |
getRow(row: number) | Range | Gets a row contained in the range. |
getUsedRange() | Range | Returns the used range of the given range object. |
insert(shift: string) | Range | Inserts a cell or a range of cells into the worksheet in place of this range, and shifts the other cells to make space. Returns a new Range object at the now blank space. |
load(param: object) | void | Fills the proxy object created in JavaScript layer with property and object values specified in the parameter. |
select() | void | Selects the specified range in the Excel UI. |
Clear range values, format, fill, border, etc.
rangeObject.clear(applyTo);
Parameter | Type | Description |
---|---|---|
applyTo | string | Optional. Determines the type of clear action. Possible values are: All Default-option,Formats ,Contents |
void
Below example clears format and contents of the range.
var sheetName = "Sheet1";
var rangeAddress = "D:F";
var ctx = new Excel.RequestContext();
var range = ctx.workbook.worksheets.getItem(sheetName).getRange(rangeAddress);
range.clear();
ctx.executeAsync();
Deletes the cells associated with the range.
rangeObject.delete(shift);
Parameter | Type | Description |
---|---|---|
shift | string | Specifies which way to shift the cells. Possible values are: Up, Left |
void
var sheetName = "Sheet1";
var rangeAddress = "D:F";
var ctx = new Excel.RequestContext();
var range = ctx.workbook.worksheets.getItem(sheetName).getRange(rangeAddress);
range.delete();
ctx.executeAsync();
Gets the smallest range object that encompasses the given ranges. For example, the GetBoundingRect of "B2:C5" and "D10:E15" is "B2:E16".
rangeObject.getBoundingRect(anotherRange);
Parameter | Type | Description |
---|---|---|
anotherRange | object | The range object or address or range name. |
var sheetName = "Sheet1";
var rangeAddress = "D4:G6";
var ctx = new Excel.RequestContext();
var range = ctx.workbook.worksheets.getItem(sheetName).getRange(rangeAddress);
var range = range.getBoundingRect("G4:H8");
range.load();
ctx.executeAsync().then(function() {
Console.log(range.address); // Prints Sheet1!D4:H8
});
Gets the range object containing the single cell based on row and column numbers. The cell can be outside the bounds of its parent range, so long as it's stays within the worksheet grid. The returned cell is located relative to the top left cell of the range.
rangeObject.getCell(row, column);
Parameter | Type | Description |
---|---|---|
row | number | Row number of the cell to be retrieved. Zero-indexed. |
column | number | Column number of the cell to be retrieved. Zero-indexed. |
var sheetName = "Sheet1";
var rangeAddress = "A1:F8";
var ctx = new Excel.RequestContext();
var worksheet = ctx.workbook.worksheets.getItem(sheetName);
var range = worksheet.getRange(rangeAddress);
var cell = range.cell(0,0);
ctx.load(cell);
ctx.executeAsync().then(function() {
Console.log(cell.address);
});
Gets a column contained in the range.
rangeObject.getColumn(column);
Parameter | Type | Description |
---|---|---|
column | number | Column number of the range to be retrieved. Zero-indexed. |
var sheetName = "Sheet19";
var rangeAddress = "A1:F8";
var ctx = new Excel.RequestContext();
var range = ctx.workbook.worksheets.getItem(sheetName).getRange(rangeAddress).getColumn(1);
range.load();
ctx.executeAsync().then(function() {
Console.log(range.address); // prints Sheet1!B1:B8
});
Gets an object that represents the entire column of the range.
rangeObject.getEntireColumn();
None
Note: the grid properties of the Range (values, numberFormat, formula) contains null
since the Range in question is unbounded.
var sheetName = "Sheet1";
var rangeAddress = "D:F";
var ctx = new Excel.RequestContext();
var range = ctx.workbook.worksheets.getItem(sheetName).getRange(rangeAddress);
var rangeEC = range.getEntireColumn();
rangeEC.load();
ctx.executeAsync().then(function() {
Console.log(rangeEC.address);
});
Gets an object that represents the entire row of the range.
rangeObject.getEntireRow();
None
var sheetName = "Sheet1";
var rangeAddress = "D:F";
var ctx = new Excel.RequestContext();
var range = ctx.workbook.worksheets.getItem(sheetName).getRange(rangeAddress);
var rangeER = range.getEntireRow();
rangeER.load();
ctx.executeAsync().then(function() {
Console.log(rangeER.address);
});
The grid properties of the Range (values, numberFormat, formula) contains null
since the Range in question is unbounded.
Gets the range object that represents the rectangular intersection of the given ranges.
rangeObject.getIntersection(anotherRange);
Parameter | Type | Description |
---|---|---|
anotherRange | object | The range object or range address that will be used to determine the intersection of ranges. |
var sheetName = "Sheet1";
var rangeAddress = "A1:F8";
var ctx = new Excel.RequestContext();
var range = ctx.workbook.worksheets.getItem(sheetName).getRange(rangeAddress).getIntersection("D4:G6");
range.load();
ctx.executeAsync().then(function() {
Console.log(range.address); // prints Sheet1!D4:F6
});
Gets the last cell within the range. For example, the last cell of "B2:D5" is "D5".
rangeObject.getLastCell();
None
var sheetName = "Sheet1";
var rangeAddress = "A1:F8";
var ctx = new Excel.RequestContext();
var range = ctx.workbook.worksheets.getItem(sheetName).getRange(rangeAddress).getLastCell();
range.load(address);
ctx.executeAsync().then(function() {
Console.log(range.address); // prints Sheet1!F8
});
Gets the last column within the range. For example, the last column of "B2:D5" is "D2:D5".
rangeObject.getLastColumn();
None
var sheetName = "Sheet1";
var rangeAddress = "A1:F8";
var ctx = new Excel.RequestContext();
var range = ctx.workbook.worksheets.getItem(sheetName).getRange(rangeAddress).getLastColumn();
range.load(address);
ctx.executeAsync().then(function() {
Console.log(range.address); // prints Sheet1!F1:F8
});
Gets the last row within the range. For example, the last row of "B2:D5" is "B5:D5".
rangeObject.getLastRow();
None
var sheetName = "Sheet1";
var rangeAddress = "A1:F8";
var ctx = new Excel.RequestContext();
var range = ctx.workbook.worksheets.getItem(sheetName).getRange(rangeAddress).getLastRow();
range.load(address);
ctx.executeAsync().then(function() {
Console.log(range.address); // prints Sheet1!A8:F8
});
Gets an object which represents a range that's offset from the specified range. The dimension of the returned range will match this range. If the resulting range is forced outside the bounds of the worksheet grid, an exception will be thrown.
rangeObject.getOffsetRange(rowOffset, columnOffset);
Parameter | Type | Description |
---|---|---|
rowOffset | number | The number of rows (positive, negative, or 0) by which the range 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 range is to be offset. Positive values are offset to the right, and negative values are offset to the left. |
var sheetName = "Sheet1";
var rangeAddress = "D4:F6";
var ctx = new Excel.RequestContext();
var range = ctx.workbook.worksheets.getItem(sheetName).getRange(rangeAddress).getOffsetRange(-1,4);
range.load();
ctx.executeAsync().then(function() {
Console.log(range.address); // prints Sheet1!H3:K5
});
Gets a row contained in the range.
rangeObject.getRow(row);
Parameter | Type | Description |
---|---|---|
row | number | Row number of the range to be retrieved. Zero-indexed. |
var sheetName = "Sheet1";
var rangeAddress = "A1:F8";
var ctx = new Excel.RequestContext();
var range = ctx.workbook.worksheets.getItem(sheetName).getRange(rangeAddress).getRow(1);
range.load();
ctx.executeAsync().then(function() {
Console.log(range.address); // prints Sheet1!A2:F2
});
Returns the used range of the given range object.
rangeObject.getUsedRange();
None
var sheetName = "Sheet1";
var rangeAddress = "D:F";
var ctx = new Excel.RequestContext();
var range = ctx.workbook.worksheets.getItem(sheetName).getRange(rangeAddress);
var rangeUR = range.getUsedRange();
rangeUR.load();
ctx.executeAsync().then(function() {
Console.log(rangeUR.address);
});
Inserts a cell or a range of cells into the worksheet in place of this range, and shifts the other cells to make space. Returns a new Range object at the now blank space.
rangeObject.insert(shift);
Parameter | Type | Description |
---|---|---|
shift | string | Specifies which way to shift the cells. Possible values are: Down, Right |
var sheetName = "Sheet1";
var rangeAddress = "F5:F10";
var ctx = new Excel.RequestContext();
var range = ctx.workbook.worksheets.getItem(sheetName).getRange(rangeAddress);
range.insert();
ctx.executeAsync();
Fills the proxy object created in JavaScript layer with property and object values specified in the parameter.
object.load(param);
Parameter | Type | Description |
---|---|---|
param | object | Optional. Accepts parameter and relationship names as delimited string or an array. Or, provide loadOption object. |
void
Selects the specified range in the Excel UI.
rangeObject.select();
None
void
var sheetName = "Sheet1";
var rangeAddress = "F5:F10";
var ctx = new Excel.RequestContext();
var range = ctx.workbook.worksheets.getItem(sheetName).getRange(rangeAddress);
range.select();
ctx.executeAsync();
Below example uses range address to get the range object.
var sheetName = "Sheet1";
var rangeAddress = "A1:F8";
var ctx = new Excel.RequestContext();
var worksheet = ctx.workbook.worksheets.getItem(sheetName);
var range = worksheet.getRange(rangeAddress);
range.load();
ctx.executeAsync().then(function() {
Console.log(range.cellCount);
});
Below example uses a named-range to get the range object.
var rangeName = 'MyRange';
var ctx = new Excel.RequestContext();
var range = ctx.workbook.names.getItem(rangeName).range;
range.load();
ctx.executeAsync().then(function() {
Console.log(range.cellCount);
});
The example below sets number-format, values and formulas on a grid that contains 2x3 grid.
var sheetName = "Sheet1";
var rangeAddress = "F5:G7";
var numberFormat = [[null, "d-mmm"], [null, "d-mmm"], [null, null]]
var values = [["Today", 42147], ["Tomorrow", "5/24"], ["Difference in days", null]];
var formula = [[null,null], [null,null], [null,"=G6-G5"]];
var range = ctx.workbook.worksheets.getItem(sheetName).getRange(rangeAddress);
range.numberFormat = numberFormat;
range.values = values;
range.formula = formula;
range.load();
ctx.executeAsync().then(function() {
Console.log(range.text);
});
Get the worksheet containing the range.
var ctx = new Excel.RequestContext();
var names = ctx.workbook.names;
var namedItem = names.getItem('MyRange');
range = namedItem.range;
var rangeWorksheet = range.worksheet;
load(rangeWorksheet)
ctx.executeAsync().then(function () {
Console.log(rangeWorksheet.name);
});