-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathReadWriteBasics.ts
More file actions
40 lines (38 loc) · 1.6 KB
/
ReadWriteBasics.ts
File metadata and controls
40 lines (38 loc) · 1.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/**
* This script demonstrates basic read-write operations on the range object.
*/
function main(workbook: ExcelScript.Workbook) {
const cell = workbook.getActiveCell();
const prevValue = cell.getValue();
if (prevValue) {
console.log(`Active cell's value is: ${prevValue}`);
} else {
console.log("Setting active cell's value..");
cell.setValue("Sample");
}
// Get next cell to the right column and set its value and fill color.
const nextCell = cell.getOffsetRange(0,1);
nextCell.setValue("Next cell");
console.log(`Next cell's address is: ${nextCell.getAddress()}`);
console.log("Setting fill color and font color of next cell..");
nextCell.getFormat().getFill().setColor("Magenta");
nextCell.getFormat().getFill().setColor("Cyan");
// Get the target range address to update with 2-dimensional value.
const dataRange = nextCell.getOffsetRange(1, 0).getResizedRange(2, 1);
const DATA = [
[10, 7],
[8, 15],
[12, 1]
];
console.log(`Updating range ${dataRange.getAddress()} with values: ${DATA}`);
dataRange.setValues(DATA);
// Formula range
const formulaRange = dataRange.getOffsetRange(3, 0).getRow(0);
console.log(`Updating formula for range: ${formulaRange.getAddress()}`)
// Since relative formula is being set, we can set the formula of the entire range to same value.
formulaRange.setFormulaR1C1("=SUM(R[-3]C:R[-1]C)");
console.log(`Updating Number Format for range: ${formulaRange.getAddress()}`)
// Since the number format is common to the entire range, we can set it to a common format.
formulaRange.setNumberFormat("0.00");
return;
}