Created
July 23, 2024 08:21
-
-
Save tifandotme/0eb0dc1608ebba415494ff106b2fe9fe to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type BaseHeader = { | |
name: string; | |
colIndex: number; | |
}; | |
type HeaderWithChildren = BaseHeader & { | |
colEnd: number; | |
children: Header[]; | |
}; | |
type Header = BaseHeader | HeaderWithChildren; | |
type Response = { | |
headers: Header[]; | |
maxRow: number; | |
}; | |
/** | |
* From DB | |
*/ | |
async function getParameterHeaders(): Promise<Response> { | |
await new Promise((resolve) => { | |
setTimeout(resolve, 500); | |
}); | |
const MAX_ROW = 2; // 0-indexed | |
const headers: (BaseHeader | HeaderWithChildren)[] = [ | |
{ | |
name: 'Tanggal', | |
colIndex: 0, | |
}, | |
{ | |
name: 'Kolam', | |
colIndex: 1, | |
}, | |
{ | |
name: 'Jumlah Benur', | |
colIndex: 2, | |
}, | |
{ | |
name: 'DOC', | |
colIndex: 3, | |
}, | |
{ | |
name: 'Pagi', | |
colIndex: 4, | |
colEnd: 6, | |
children: [ | |
{ | |
name: 'Salinitas', | |
colIndex: 4, | |
}, | |
{ | |
name: 'Temperature', | |
colIndex: 5, | |
}, | |
{ | |
name: 'Kecerahan', | |
colIndex: 6, | |
}, | |
], | |
}, | |
{ | |
name: 'Siang', | |
colIndex: 7, | |
colEnd: 9, | |
children: [ | |
{ | |
name: 'Salinitas', | |
colIndex: 7, | |
}, | |
{ | |
name: 'Temperature', | |
colIndex: 8, | |
}, | |
{ | |
name: 'Kecerahan', | |
colIndex: 9, | |
}, | |
], | |
}, | |
{ | |
name: '111', | |
colIndex: 10, | |
colEnd: 13, | |
children: [ | |
{ | |
name: '222a', | |
colIndex: 10, | |
colEnd: 11, | |
children: [ | |
{ | |
name: '333b', | |
colIndex: 10, | |
}, | |
{ | |
name: '333c', | |
colIndex: 11, | |
}, | |
], | |
}, | |
{ | |
name: '222b', | |
colIndex: 12, | |
}, | |
{ | |
name: '222c', | |
colIndex: 13, | |
}, | |
], | |
}, | |
]; | |
return { | |
headers, | |
maxRow: MAX_ROW, | |
}; | |
} | |
/** | |
* Generate cellData and mergeData for Worksheet transformed from DB response | |
*/ | |
function createCellData(response: Response) { | |
const { headers, maxRow } = response; | |
const cellData: IObjectMatrixPrimitiveType<ICellData> = {}; | |
const mergeData: IRange[] = []; | |
const styles = new Styles(); | |
const verticalAlign = styles.add( | |
{ | |
ht: HorizontalAlign.CENTER, | |
vt: VerticalAlign.MIDDLE, | |
}, | |
'verticalAlign', | |
); | |
const assignHeaders = (rootHeaders: Header[], currRow = 0) => { | |
if (currRow > maxRow) return; | |
rootHeaders.forEach((header) => { | |
if ('children' in header) { | |
for (let col = header.colIndex; col <= header.colEnd; col += 1) { | |
cellData[currRow] = cellData[currRow] ?? {}; | |
cellData[currRow]![col] = | |
col === header.colIndex | |
? { | |
v: header.name, | |
t: CellValueType.STRING, | |
s: verticalAlign, | |
} | |
: {}; | |
} | |
mergeData.push({ | |
startRow: currRow, | |
endRow: currRow, | |
startColumn: header.colIndex, | |
endColumn: header.colEnd, | |
}); | |
assignHeaders(header.children, currRow + 1); | |
} else { | |
for (let row = currRow; row <= maxRow; row += 1) { | |
cellData[row] = cellData[row] ?? {}; | |
cellData[row]![header.colIndex] = | |
row === currRow | |
? { | |
v: header.name, | |
t: CellValueType.STRING, | |
s: verticalAlign, | |
} | |
: {}; | |
} | |
mergeData.push({ | |
startRow: currRow, | |
endRow: maxRow, | |
startColumn: header.colIndex, | |
endColumn: header.colIndex, | |
}); | |
} | |
}); | |
}; | |
assignHeaders(headers); | |
return { | |
cellData, | |
mergeData, | |
styles: styles.toJSON(), | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment