title | description | ms.date | ms.localizationpriority |
---|---|---|---|
Set and get range values, text, or formulas using the Excel JavaScript API |
Learn how to use the Excel JavaScript API to set and get range values, text, or formulas. |
02/17/2022 |
medium |
This article provides code samples that set and get range values, text, or formulas with 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]
The following code samples set values and formulas for a single cell or a range of cells.
The following code sample sets the value of cell C3 to "5" and then sets the width of the columns to best fit the data.
await Excel.run(async (context) => {
let sheet = context.workbook.worksheets.getItem("Sample");
let range = sheet.getRange("C3");
range.values = [[ 5 ]];
range.format.autofitColumns();
await context.sync();
});
The following code sample sets values for the cells in the range B5:D5 and then sets the width of the columns to best fit the data.
await Excel.run(async (context) => {
let sheet = context.workbook.worksheets.getItem("Sample");
let data = [
["Potato Chips", 10, 1.80],
];
let range = sheet.getRange("B5:D5");
range.values = data;
range.format.autofitColumns();
await context.sync();
});
The following code sample sets a formula for cell E3 and then sets the width of the columns to best fit the data.
await Excel.run(async (context) => {
let sheet = context.workbook.worksheets.getItem("Sample");
let range = sheet.getRange("E3");
range.formulas = [[ "=C3 * D3" ]];
range.format.autofitColumns();
await context.sync();
});
The following code sample sets formulas for cells in the range E2:E6 and then sets the width of the columns to best fit the data.
await Excel.run(async (context) => {
let sheet = context.workbook.worksheets.getItem("Sample");
let data = [
["=C3 * D3"],
["=C4 * D4"],
["=C5 * D5"],
["=SUM(E3:E5)"]
];
let range = sheet.getRange("E3:E6");
range.formulas = data;
range.format.autofitColumns();
await context.sync();
});
These code samples get values, text, and formulas from a range of cells.
The following code sample gets the range B2:E6, loads its values
property, and writes the values to the console. The values
property of a range specifies the raw values that the cells contain. Even if some cells in a range contain formulas, the values
property of the range specifies the raw values for those cells, not any of the formulas.
await Excel.run(async (context) => {
let sheet = context.workbook.worksheets.getItem("Sample");
let range = sheet.getRange("B2:E6");
range.load("values");
await context.sync();
console.log(JSON.stringify(range.values, null, 4));
});
[
[
"Product",
"Qty",
"Unit Price",
"Total Price"
],
[
"Almonds",
2,
7.5,
15
],
[
"Coffee",
1,
34.5,
34.5
],
[
"Chocolate",
5,
9.56,
47.8
],
[
"",
"",
"",
97.3
]
]
The following code sample gets the range B2:E6, loads its text
property, and writes it to the console. The text
property of a range specifies the display values for cells in the range. Even if some cells in a range contain formulas, the text
property of the range specifies the display values for those cells, not any of the formulas.
await Excel.run(async (context) => {
let sheet = context.workbook.worksheets.getItem("Sample");
let range = sheet.getRange("B2:E6");
range.load("text");
await context.sync();
console.log(JSON.stringify(range.text, null, 4));
});
[
[
"Product",
"Qty",
"Unit Price",
"Total Price"
],
[
"Almonds",
"2",
"7.5",
"15"
],
[
"Coffee",
"1",
"34.5",
"34.5"
],
[
"Chocolate",
"5",
"9.56",
"47.8"
],
[
"",
"",
"",
"97.3"
]
]
The following code sample gets the range B2:E6, loads its formulas
property, and writes it to the console. The formulas
property of a range specifies the formulas for cells in the range that contain formulas and the raw values for cells in the range that do not contain formulas.
await Excel.run(async (context) => {
let sheet = context.workbook.worksheets.getItem("Sample");
let range = sheet.getRange("B2:E6");
range.load("formulas");
await context.sync();
console.log(JSON.stringify(range.formulas, null, 4));
});
[
[
"Product",
"Qty",
"Unit Price",
"Total Price"
],
[
"Almonds",
2,
7.5,
"=C3 * D3"
],
[
"Coffee",
1,
34.5,
"=C4 * D4"
],
[
"Chocolate",
5,
9.56,
"=C5 * D5"
],
[
"",
"",
"",
"=SUM(E3:E5)"
]
]