Skip to content

Commit

Permalink
feat: make sub-row index available contextually on format & cellStyle…
Browse files Browse the repository at this point in the history
… function
  • Loading branch information
ChronicStone committed Apr 24, 2024
1 parent 991da03 commit eb482ef
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 20 deletions.
6 changes: 3 additions & 3 deletions docs/.examples/financial-report/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ export const financialReportSchema = ExcelSchemaBuilder.create<FinancialReport>(
label: 'Profit',
transform: departments => departments.map(d => d.profit),
format: '$#,##0.00',
// cellStyle: data => ({
// font: { color: { rgb: data.profit >= 0 ? '007500' : 'FF0000' } },
// }),
cellStyle: (data, _, index) => ({
font: { color: { rgb: data[].profit >= 0 ? '007500' : 'FF0000' } },
}),
})
.column('Profit Margin', {
key: 'departments',
Expand Down
16 changes: 9 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable ts/ban-types */
import XLSX, { type WorkSheet, utils } from 'xlsx-js-style'
import type { CellValue, Column, ColumnGroup, ExcelBuildOutput, ExcelBuildParams, ExcelSchema, GenericObject, NestedPaths, Not, SchemaColumnKeys, SheetConfig, SheetParams, SheetTable, SheetTableBuilder, TOutputType, TransformersMap } from './types'
import { applyGroupBorders, buildCell, buildSheetConfig, computeSheetRange, getColumnHeaderStyle, getColumnSeparatorIndexes, getPrevRowsHeight, getRowMaxHeight, getRowValue, getSheetChunkMaxHeight, getWorksheetColumnWidths, splitIntoChunks, tableHasSummary } from './utils'
import { applyGroupBorders, buildSheetConfig, computeSheetRange, createCell, getColumnHeaderStyle, getColumnSeparatorIndexes, getPrevRowsHeight, getRowMaxHeight, getRowValue, getSheetChunkMaxHeight, getWorksheetColumnWidths, splitIntoChunks, tableHasSummary } from './utils'

export type * from './types'

Expand Down Expand Up @@ -178,7 +178,7 @@ export class ExcelBuilder<UsedSheetKeys extends string = never> {
if (hasTitle) {
tableConfig.columns.forEach((_, colIndex) => {
const titleCellRef = utils.encode_cell({ c: COL_OFFSET + colIndex, r: ROW_OFFSET })
worksheet[titleCellRef] = buildCell({
worksheet[titleCellRef] = createCell({
value: colIndex === 0 ? tableConfig.title : '',
style: getColumnHeaderStyle({ bordered: params?.bordered ?? true }),
extraStyle: {
Expand All @@ -197,7 +197,7 @@ export class ExcelBuilder<UsedSheetKeys extends string = never> {

tableConfig.columns.forEach((column, colIndex) => {
const headerCellRef = utils.encode_cell({ c: colIndex + COL_OFFSET, r: ROW_OFFSET + (rowHasTitle ? 1 : 0) })
worksheet[headerCellRef] = buildCell({
worksheet[headerCellRef] = createCell({
value: column.label,
bordered: params?.bordered ?? true,
style: getColumnHeaderStyle({ bordered: params?.bordered ?? true }),
Expand All @@ -214,12 +214,14 @@ export class ExcelBuilder<UsedSheetKeys extends string = never> {
r: prevRowHeight + ROW_OFFSET + (rowHasTitle ? 1 : 0) + (valueIndex + 1),
})

worksheet[cellRef] = buildCell({
worksheet[cellRef] = createCell({
value,
data: row,
format: column._ref.format,
style: column._ref.cellStyle,
bordered: params?.bordered ?? true,
rowIndex,
subRowIndex: valueIndex,
})
})

Expand All @@ -229,7 +231,7 @@ export class ExcelBuilder<UsedSheetKeys extends string = never> {
c: colIndex + COL_OFFSET,
r: prevRowHeight + ROW_OFFSET + (rowHasTitle ? 1 : 0) + (valueIndex + 1),
})
worksheet[cellRef] = buildCell({ value: '', bordered: params?.bordered ?? true })
worksheet[cellRef] = createCell({ value: '', bordered: params?.bordered ?? true })
}
if (values.length === 1) {
worksheet['!merges'].push({
Expand All @@ -249,7 +251,7 @@ export class ExcelBuilder<UsedSheetKeys extends string = never> {
r: summaryRowIndex + ROW_OFFSET + +summaryIndex + (rowHasTitle ? 1 : 0),
})
if (!summary) {
worksheet[cellRef] = buildCell({
worksheet[cellRef] = createCell({
value: '',
bordered: params?.bordered ?? true,
style: getColumnHeaderStyle({ bordered: params?.bordered ?? true }),
Expand All @@ -258,7 +260,7 @@ export class ExcelBuilder<UsedSheetKeys extends string = never> {
}

const value = summary.value(tableConfig.content)
worksheet[cellRef] = buildCell({
worksheet[cellRef] = createCell({
value,
data: tableConfig.content,
format: summary.format,
Expand Down
4 changes: 2 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ export type Column<
columnKey: ColKey
key: FieldValue
default?: CellValue
format?: string | ((rowData: T) => string)
cellStyle?: CellStyle | ((rowData: T) => CellStyle)
format?: string | ((rowData: T, rowIndex: number, subRowIndex: number) => string)
cellStyle?: CellStyle | ((rowData: T, rowIndex: number, subRowIndex: number) => CellStyle)
summary?: Array<{
value: (data: T[]) => BaseCellValue
format?: string | ((data: T[]) => string)
Expand Down
22 changes: 14 additions & 8 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,9 @@ export function buildSheetConfig(sheets: Array<SheetConfig>) {

if (
typeof value === 'undefined'
|| value === null
|| value === ''
|| (Array.isArray(value) && value.length === 0 && column.default)
|| value === null
|| value === ''
|| (Array.isArray(value) && value.length === 0 && column.default)
)
return column.default

Expand Down Expand Up @@ -327,16 +327,22 @@ export function getColumnSeparatorIndexes(params: {
}).flat()
}

export function buildCell(params: {
export function createCell(params: {
data?: GenericObject
value?: BaseCellValue
style?: CellStyle | ((rowData: any) => CellStyle)
format?: string | ((rowData: any) => string)
style?: CellStyle | ((rowData: any, rowIndex: number, subRowIndex: number) => CellStyle)
format?: string | ((rowData: any, rowIndex: number, subRowIndex: number) => string)
extraStyle?: CellStyle
bordered?: boolean
rowIndex?: number
subRowIndex?: number
}): XLSX.CellObject {
const style = typeof params.style === 'function' ? params.style(params.data ?? {}) : params.style ?? {}
const format = typeof params.format === 'function' ? params.format(params.data ?? {}) : params.format
const style = typeof params.style === 'function'
? params.style(params.data ?? {}, params?.rowIndex ?? 0, params?.subRowIndex ?? 0)
: params.style ?? {}
const format = typeof params.format === 'function'
? params.format(params.data ?? {}, params?.rowIndex ?? 0, params?.subRowIndex ?? 0)
: params.format
return {
v: params.value === null ? '' : params.value,
t: getCellDataType(params.value),
Expand Down

0 comments on commit eb482ef

Please sign in to comment.