-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathExcelTableAsJSON.ts
More file actions
36 lines (33 loc) · 980 Bytes
/
ExcelTableAsJSON.ts
File metadata and controls
36 lines (33 loc) · 980 Bytes
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
function main(workbook: ExcelScript.Workbook): BasicObj[] {
const sheet = workbook.getWorksheet('Interviews');
const table = sheet.getTables()[0];
const dataRows = table.getRange().getTexts() as string[][];
// or
// let dataRows = sheet.getUsedRange().getValues();
const recordDetails: BasicObj[] = returnObjectFromValues(dataRows as string[][]);
console.log(recordDetails);
return recordDetails;
}
/**
* This helper funciton converts table values into an object array.
*/
function returnObjectFromValues(values: string[][]): BasicObj[] {
let objArray: BasicObj[] = [];
let objKeys: string[] = [];
for (let i = 0; i < values.length; i++) {
if (i === 0) {
objKeys = values[i]
continue;
}
let obj = {}
for (let j = 0; j < values[i].length; j++) {
obj[objKeys[j]] = values[i][j]
}
objArray.push(obj);
}
console.log(JSON.stringify(objArray));
return objArray;
}
interface BasicObj {
[key: string]: string
}