-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathTableAsArrayOfObjectsWithHyperlink.ts
More file actions
48 lines (45 loc) · 1.33 KB
/
TableAsArrayOfObjectsWithHyperlink.ts
File metadata and controls
48 lines (45 loc) · 1.33 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
47
48
function main(workbook: ExcelScript.Workbook): TableData[] {
const table = workbook.getWorksheet('WithHyperLink').getTables()[0];
const range = table.getRange();
// 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, range);
}
console.log(JSON.stringify(returnObjects));
return returnObjects
}
function returnObjectFromValues(values: string[][], range: ExcelScript.Range): 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++) {
// For the 4th column (0 index), extract the hyperlink and use that instead of text.
if (j === 4) {
obj[objKeys[j]] = range.getCell(i, j).getHyperlink().address;
} else {
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
"Search link": string
Speakers: string
}