-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbasicExcel.ts
55 lines (44 loc) · 1.54 KB
/
basicExcel.ts
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
49
50
51
52
53
54
55
import type { Debugger, EvaluateCollectionType } from '../lib'
import { ClubRecord, DMap, FirestoreCollection } from '../lib'
import { Workbook } from '../lib/builtin/data/Workbook'
import { Worksheet } from '../lib/builtin/data/Worksheet'
export const basicExcel = async (debug: Debugger) => {
/*
This example demonstrates the basic usages of lab resources.
By utilising Resource Control Class ex. Collection()
and Data Controls ex. ClubRecord() and DMap()
*/
// Initialise data collection
const evalColl = new FirestoreCollection<EvaluateCollectionType>('evaluate')
// Load data from the local cache and fetch if there was no cache.
const evalData = await evalColl.readFromCache(true)
if (!evalData) return
const evalRecords = new ClubRecord(evalData.getRecord())
// Mutate data to a table compatible form and create sheets.
const book = evalRecords.map((clubId, v) => {
const sheetData = new DMap(v.data()).map((k, v) => ({
ID: k,
report: v.action
}))
return new Worksheet(sheetData).setName(clubId)
})
// Create a workbook
const workbook = new Workbook(book)
workbook.setStyle((l, cell) => {
cell.border = {
top: { style: 'thin' },
left: { style: 'thin' },
bottom: { style: 'thin' },
right: { style: 'thin' }
}
if (l.r === 1) {
cell.font = { bold: true }
cell.alignment = { horizontal: 'center' }
}
if (l.c === 2) {
cell.alignment = { horizontal: 'center' }
}
})
// Save the workbook as .xlsx file.
workbook.save('evals.xlsx')
}