Skip to content

Commit

Permalink
feat: add column header styling support
Browse files Browse the repository at this point in the history
  • Loading branch information
ChronicStone committed Apr 29, 2024
1 parent 7b18fb7 commit 0e217ca
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 11 deletions.
Binary file modified examples/financial-report.xlsx
Binary file not shown.
Binary file modified examples/kitchen-sink.xlsx
Binary file not shown.
Binary file modified examples/playground.xlsx
Binary file not shown.
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ export class ExcelBuilder<UsedSheetKeys extends string = never> {
worksheet[headerCellRef] = createCell({
value: column.label,
bordered: params?.bordered ?? true,
style: getColumnHeaderStyle({ bordered: params?.bordered ?? true }),
style: getColumnHeaderStyle({ bordered: params?.bordered ?? true, customStyle: column._ref.headerStyle }),
})

tableConfig.content.forEach((row, rowIndex) => {
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ export type Column<
default?: CellValue
format?: string | ((rowData: T, rowIndex: number, subRowIndex: number) => string)
cellStyle?: CellStyle | ((rowData: T, rowIndex: number, subRowIndex: number) => CellStyle)
headerStyle?: CellStyle
summary?: Array<{
value: (data: T[]) => BaseCellValue
format?: string | ((data: T[]) => string)
Expand Down
21 changes: 12 additions & 9 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,15 +104,18 @@ export function buildSheetConfig(sheets: Array<SheetConfig>) {
}))
}

export function getColumnHeaderStyle(params: { bordered: boolean }) {
return {
font: { bold: true },
alignment: { horizontal: 'center', vertical: 'center' },
fill: { fgColor: { rgb: 'E9E9E9' } },
border: (params?.bordered ?? true)
? THICK_BORDER_STYLE
: {},
} satisfies CellStyle
export function getColumnHeaderStyle(params: { bordered: boolean, customStyle?: CellStyle }) {
return deepmerge(
{
font: { bold: true },
alignment: { horizontal: 'center', vertical: 'center' },
fill: { fgColor: { rgb: 'E9E9E9' } },
border: (params?.bordered ?? true)
? THICK_BORDER_STYLE
: {},
},
params?.customStyle ?? {},
) satisfies CellStyle
}

export function getWorksheetColumnWidths(worksheet: WorkSheet, extraLength: number = 1) {
Expand Down
6 changes: 5 additions & 1 deletion test/play.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ describe('should generate the play excel file', () => {
// Group definition within the schema
const schema = ExcelSchemaBuilder.create<User>()
.column('id', { key: 'id' })
.column('name', { key: 'name' })
.column('name', {
key: 'name',
cellStyle: { fill: { fgColor: { rgb: 'FFFF00' } } },
headerStyle: { fill: { fgColor: { rgb: '00FF00' } } },
})
.build()

const users: User[] = Array.from({ length: 100000 }, (_, i) => ({
Expand Down

0 comments on commit 0e217ca

Please sign in to comment.