-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathRange2DTo1DAndBack.ts
More file actions
46 lines (36 loc) · 1.18 KB
/
Range2DTo1DAndBack.ts
File metadata and controls
46 lines (36 loc) · 1.18 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
41
42
43
44
45
46
function main(workbook: ExcelScript.Workbook)
{
const table = workbook.getActiveWorksheet().getTables()[0];
const header = table.getHeaderRowRange().getTexts();
// Display header as 1D array
console.log(header[0]); // Extract 1st row
const sales = table.getColumnByName('Sales').getRange().getValues();
console.log(sales);
// Get column-0
const salesAs1DArray = extractColumn(sales, 0);
console.log(salesAs1DArray);
// Add 100 to each value
const revisedSales = salesAs1DArray.map(data => data as number + 100);
// Add a column
table.addColumn(-1, revisedSales);
const salesBackTo2D = convertColumnTo2D(salesAs1DArray);
console.log(salesBackTo2D);
}
/**
* Extract a column from 2D array and return result.
*/
function extractColumn(data: (string | number | boolean)[][], index: number): (string | number | boolean)[] {
const column = data.map((row) => {
return row[index];
})
return column;
}
/**
* Convert a flat array into 2D array that can be used as range column
*/
function convertColumnTo2D(data: (string | number | boolean)[]): (string | number | boolean)[][] {
const columnAs2D = data.map((row) => {
return [row];
})
return columnAs2D;
}