-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.ts
281 lines (246 loc) · 8.3 KB
/
main.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
import * as fs from 'fs';
import * as fxl from '@01group/fxl';
// ---------------------------------------------------------------------------
// Types
// ---------------------------------------------------------------------------
interface InventoryData {
[month: string]: MonthsInventory;
}
interface MonthsInventory {
opening: Quantities;
inflows: Quantities;
outflows: Quantities;
closing: Quantities;
}
interface Quantities {
[rawMaterial: string]: number;
}
// ---------------------------------------------------------------------------
// Data
// ---------------------------------------------------------------------------
function unique<T>(xs: T[]): T[] {
return [...new Set(xs)].sort();
}
const values = Object.values;
const keys = Object.keys;
const JSON_PATH = 'packages/example/src/assets/inventory.json';
const RAW_DATA = fs.readFileSync(JSON_PATH).toString();
const INVENTORY = JSON.parse(RAW_DATA) as InventoryData;
const RAW_MATERIALS = unique(
values(INVENTORY).flatMap((monthsInventory) =>
values(monthsInventory).flatMap(keys)
)
);
const FORM_DATA = {
docId: 'F-ABC-123',
revisionNumber: 2,
site: 'Jakarta',
timestamp: new Date().toDateString(),
};
// ---------------------------------------------------------------------------
// Component 0: Title
// ---------------------------------------------------------------------------
const titleCell = fxl.pipe(
fxl.toCell(`INVENTORY REPORT (Year ${new Date().getFullYear()})`),
fxl.setCoord({ row: 0, col: 0, width: 10, height: 3 }),
fxl.setBold(true),
fxl.setUnderline(true),
fxl.setFontSize(18),
fxl.setHorizontalAlignement('center')
);
const subtitleCell = fxl.pipe(
fxl.toCell(`Created at: ${new Date().toDateString()}`),
fxl.setCoord({ row: 0, col: 0, width: 10, height: 2 }),
fxl.setItalic(true),
fxl.setFontSize(12),
fxl.setHorizontalAlignement('center')
);
const titleCells = fxl.concatBelow([titleCell], [subtitleCell]);
// ---------------------------------------------------------------------------
// Component 1: Form Headers
// ---------------------------------------------------------------------------
const plainFormHeader = fxl.tableToCells([
['Document ID:', FORM_DATA.docId],
['Revision Number:', FORM_DATA.revisionNumber],
['Site:', FORM_DATA.site],
['Timestamp:', FORM_DATA.timestamp],
]);
function setFormStyle(cell: fxl.Cell): fxl.Cell {
const setBorders = fxl.setAllBorders(fxl.toBorder('medium', 'black'));
if (cell.coord.col == 0) {
return fxl.pipe(
cell,
setBorders,
fxl.setBold(true),
fxl.setHorizontalAlignement('right'),
fxl.setSolidFg('light_gray')
);
} else {
return fxl.pipe(cell, setBorders, fxl.setHorizontalAlignement('center'));
}
}
const formHeader = plainFormHeader.map(setFormStyle);
// ---------------------------------------------------------------------------
// Component 2: Form Footers
// ---------------------------------------------------------------------------
const plainCreateFooter = fxl.tableToCells([
['Created By:', undefined],
['Date:', undefined],
]);
const plainCheckFooter = fxl.tableToCells([
['Checked By:', undefined],
['Date:', undefined],
]);
const createFooter = plainCreateFooter.map(setFormStyle);
const checkFooter = plainCheckFooter.map(setFormStyle);
// ---------------------------------------------------------------------------
// Component 3: Inventory Table
// ---------------------------------------------------------------------------
// Sub-Component 1: Header
function setHeaderStyle(cell: fxl.Cell): fxl.Cell {
return fxl.pipe(cell, fxl.setBold(true), fxl.setSolidFg('light_gray'));
}
function inventoryHeader(
quarter: string,
months: [string, string, string]
): fxl.Cell[] {
const firstRow = fxl.rowToCells([
quarter,
undefined,
months[0],
undefined,
undefined,
months[1],
undefined,
undefined,
months[2],
undefined,
]);
const secondRow = fxl.rowToCells([
'Raw Material',
'Opening',
'Inflows',
'Outflows',
'Opening',
'Inflows',
'Outflows',
'Opening',
'Inflows',
'Outflows',
]);
return fxl.concatBelow(firstRow, secondRow).map(setHeaderStyle);
}
// Sub-Component 2: Raw-Material Column
const rawMaterialColumn = fxl.colToCells(RAW_MATERIALS).map(setHeaderStyle);
// Sub-Component 3: Single-Month Inventory
function highlightShortage(cell: fxl.Cell): fxl.Cell {
if (typeof cell.value == 'number') {
if (cell.value > 100) {
return cell;
} else if (cell.value > 0) {
return fxl.setSolidFg('yellow')(cell);
} else {
return fxl.pipe(cell, fxl.setSolidFg('red'), fxl.setFontColor('white'));
}
} else {
return cell;
}
}
function setInventoryBodyStyle(cell: fxl.Cell): fxl.Cell {
return fxl.pipe(cell, highlightShortage, fxl.setNumFmt('#,##0'));
}
function singleMonthInventory(month: string): fxl.Cell[] {
const monthsInventory = INVENTORY[month];
const inventoryTable = RAW_MATERIALS.map((name) => [
monthsInventory.opening[name],
monthsInventory.inflows[name],
monthsInventory.outflows[name],
]);
return fxl.tableToCells(inventoryTable).map(setInventoryBodyStyle);
}
// Integration 1: Single-Quarter Inventory
function setInventoryTableStyle(cell: fxl.Cell): fxl.Cell {
return fxl.pipe(
cell,
fxl.setAllBorders(fxl.toBorder('thin', 'black')),
fxl.applyIfElse(
(x) => x.coord.col == 0 && x.coord.row != 0,
fxl.setHorizontalAlignement('right'),
fxl.setHorizontalAlignement('center')
)
);
}
function singleQuarterInventory(
quarter: string,
months: [string, string, string]
): fxl.Cell[] {
const singleMonthBodies = months.map(singleMonthInventory);
const plain = fxl.concatBelow(
inventoryHeader(quarter, months),
fxl.concatRight(rawMaterialColumn, ...singleMonthBodies)
);
return plain.map(setInventoryTableStyle);
}
// Integration 2: Full-Year Inventory
const inventoryTables = fxl.concatBelow(
fxl.padBelow(2, singleQuarterInventory('Q1', ['Jan', 'Feb', 'Mar'])),
fxl.padBelow(2, singleQuarterInventory('Q2', ['Apr', 'May', 'Jun'])),
fxl.padBelow(2, singleQuarterInventory('Q3', ['Jul', 'Aug', 'Sep'])),
singleQuarterInventory('Q4', ['Oct', 'Nov', 'Dec'])
);
// ---------------------------------------------------------------------------
// Integration: Full Report
// ---------------------------------------------------------------------------
const unstyledReport = fxl.concatBelow(
fxl.padBelow(2, titleCells),
fxl.padBelow(2, formHeader),
fxl.padBelow(2, inventoryTables),
fxl.concatRight(fxl.padRight(2, createFooter), checkFooter)
);
function setAutoColWidth(cell: fxl.Cell): fxl.Cell {
if (cell.value && cell.coord.row != 0) {
const colWidth = Math.max(cell.value.toString().length, 10);
return fxl.setColWidth(colWidth)(cell);
} else {
return cell;
}
}
const report = unstyledReport.map(setAutoColWidth);
// ---------------------------------------------------------------------------
// Integration: Full Report
// ---------------------------------------------------------------------------
async function main() {
await fxl.writeXlsx(report, 'inventory-report.xlsx');
console.log(`Wrote to inventory-report.xlsx!\n${new Date()}`);
const loadedCells = await fxl.readXlsx('inventory-report.xlsx');
if (loadedCells.ok) {
const numCells = loadedCells.val.length;
console.log(`Read ${numCells} cells from inventory-report.xlsx!`);
console.log('__________________________________________________');
}
const individualComponents = [
formHeader,
createFooter,
checkFooter,
inventoryHeader('Q3', ['Jul', 'Aug', 'Sep']),
rawMaterialColumn,
singleMonthInventory('Dec'),
singleQuarterInventory('Q3', ['Jul', 'Aug', 'Sep']),
fxl.concatBelow(
fxl.padBelow(2, singleQuarterInventory('Q1', ['Jan', 'Feb', 'Mar'])),
fxl.padBelow(2, singleQuarterInventory('Q2', ['Apr', 'May', 'Jun'])),
fxl.padBelow(2, singleQuarterInventory('Q3', ['Jul', 'Aug', 'Sep'])),
singleQuarterInventory('Q4', ['Oct', 'Nov', 'Dec'])
),
];
const paddedComponents = individualComponents.map((component) =>
fxl.padRight(1, component)
);
await fxl.writeXlsx(
fxl.concatRight(...paddedComponents).map(setAutoColWidth),
'components.xlsx'
);
console.log(`Wrote to components.xlsx!\n${new Date()}`);
console.log('__________________________________________________');
}
main();