Skip to content

Commit

Permalink
feat: allow dynamic format & static cellStyle definition
Browse files Browse the repository at this point in the history
  • Loading branch information
ChronicStone committed Dec 5, 2023
1 parent 8df4369 commit f885be2
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 6 deletions.
Binary file modified consumption.xlsx
Binary file not shown.
Binary file modified example.xlsx
Binary file not shown.
7 changes: 4 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,6 @@ export class ExcelBuilder<UsedSheetKeys extends string = never> {

return column.transform ? (column.transform as ValueTransformer)(value) : value
},
format: column.format,
_ref: column,
} satisfies (IColumn & { _ref: Column<any, any, any, any> })
}),
Expand Down Expand Up @@ -212,7 +211,9 @@ export class ExcelBuilder<UsedSheetKeys extends string = never> {
} satisfies CellStyle
sheetConfig.content.forEach((row, rowIndex) => {
const cellRef = getSheetCellKey(index + 1, rowIndex + 2)
const style = column._ref.cellStyle?.(row) ?? {}
const style = typeof column._ref.cellStyle === 'function'
? column._ref.cellStyle(row)
: column._ref.cellStyle ?? {}

if (!workbook.Sheets[sheetName][cellRef])
workbook.Sheets[sheetName][cellRef] = { v: '', t: 's' } satisfies XLSX.CellObject
Expand All @@ -229,7 +230,7 @@ export class ExcelBuilder<UsedSheetKeys extends string = never> {
top: { style: 'thin', color: { rgb: '000000' } },
}
: {},
numFmt: column._ref.format,
numFmt: typeof column._ref.format === 'function' ? column._ref.format(row) : column._ref.format,
} satisfies CellStyle,
)
})
Expand Down
11 changes: 8 additions & 3 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ export type AllKeysMatch<T extends object, U> = {
[K in keyof T]: T[K] extends U ? true : false;
}[keyof T] extends true ? true : false

// https://twitter.com/mattpocockuk/status/1622730173446557697?s=20
export type Prettify<T> = {
[K in keyof T]: T[K]
} & {}

export type CellValue = string | number | boolean | null | undefined | Date

export type ValueTransformer = (value: any) => CellValue
Expand Down Expand Up @@ -78,8 +83,8 @@ export type Column<
columnKey: ColKey
key: FieldValue
default?: CellValue
format?: string
cellStyle?: (rowData: T) => CellStyle
format?: string | ((rowData: T) => string)
cellStyle?: CellStyle | ((rowData: T) => CellStyle)
} & (
ExtractColumnValue<T, FieldValue> extends CellValue
? { transform?: TypedTransformersMap<TransformMap, ExtractColumnValue<T, FieldValue>> | ((value: ExtractColumnValue<T, FieldValue>) => CellValue) }
Expand Down Expand Up @@ -138,7 +143,7 @@ export type Sheet<
data: T[]
select?: SelectColsMap
context?: {}
} & (keyof SelectedContextMap extends never ? {} : { context: SelectedContextMap })
} & (keyof SelectedContextMap extends never ? {} : { context: Prettify<SelectedContextMap> })

export type ExtractContextMap<
Schema extends ExcelSchema<any, any, string, any>,
Expand Down

0 comments on commit f885be2

Please sign in to comment.