-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathTableAsArrayOfObjects.ts
More file actions
41 lines (38 loc) · 1.03 KB
/
TableAsArrayOfObjects.ts
File metadata and controls
41 lines (38 loc) · 1.03 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
function main(workbook: ExcelScript.Workbook): TableData[] {
const table = workbook.getWorksheet('PlainTable').getTables()[0];
// If you know the table name, you can also do this...
// const table = workbook.getTable('Table13436');
const texts = table.getRange().getTexts();
let returnObjects: TableData[] = [];
if (table.getRowCount() > 0) {
returnObjects = returnObjectFromValues(texts);
}
console.log(JSON.stringify(returnObjects));
return returnObjects
}
function returnObjectFromValues(values: string[][]): TableData[] {
let objArray = [];
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);
}
return objArray as TableData[];
}
interface BasicObj {
[key: string]: string
}
interface TableData {
"Event ID": string
Date: string
Location: string
Capacity: string
Speakers: string
}