Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions src/generate-excel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ import { generateStyleXml } from "./xl/styles.xml";
import { generateTheme1 } from "./xl/theme/theme1.xml";
import { generateWorkBookXml } from "./xl/workbook.xml";
import { generateSheetXml } from "./xl/worksheets/sheet.xml";
import { ICellType, IPage, ISheet, IWorkbook, IBorder } from "./index";
import { Equation, SkipCell, getBorderKey } from "./util";
import { ICellType, IPage, ISheet, IWorkbook, IBorder, ICellStyle } from "./index";
import { Equation, SkipCell, getBorderKey, getStyleKey } from "./util";

/**
* Generates the complete XML file structure for an Excel workbook
Expand All @@ -28,9 +28,9 @@ import { Equation, SkipCell, getBorderKey } from "./util";
* @internal
*/
const generateTree = (workbook: IWorkbook) => {
// Collect all unique border styles from the workbook
const borderStyles = new Map<string, IBorder>();
borderStyles.set("none", {}); // Default no-border style
// Collect all unique complete styles from the workbook
const styleMap = new Map<string, ICellStyle>();
styleMap.set("default", {}); // Default no-style

// Check if workbook contains any date cells
let hasDateCells = false;
Expand All @@ -43,10 +43,10 @@ const generateTree = (workbook: IWorkbook) => {
hasDateCells = true;
}

// Collect border styles
if ('style' in cell && cell.style?.border) {
const borderKey = getBorderKey(cell.style.border);
borderStyles.set(borderKey, cell.style.border);
// Collect complete styles (border, background, foreground colors)
if ('style' in cell && cell.style) {
const styleKey = getStyleKey(cell.style);
styleMap.set(styleKey, cell.style);
}
});
});
Expand All @@ -59,10 +59,10 @@ const generateTree = (workbook: IWorkbook) => {
"docProps/core.xml": generateCoreXml({}),
"xl/_rels/workbook.xml.rels": generateWorkBookXmlRels(workbook),
"xl/sharedStrings.xml": generateSharedStrings(workbook),
"xl/styles.xml": generateStyleXml(borderStyles, hasDateCells),
"xl/styles.xml": generateStyleXml(styleMap, hasDateCells),
"xl/theme/theme1.xml": generateTheme1(),
"xl/workbook.xml": generateWorkBookXml(workbook),
...workbook.sheets.reduce((acc, sheet, idx) => ({ ...acc, [`xl/worksheets/sheet${idx + 1}.xml`]: generateSheetXml(sheet, borderStyles, hasDateCells) }), {})
...workbook.sheets.reduce((acc, sheet, idx) => ({ ...acc, [`xl/worksheets/sheet${idx + 1}.xml`]: generateSheetXml(sheet, styleMap, hasDateCells) }), {})
};
};

Expand Down
55 changes: 53 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
*/

import { generateExcel, EnvironmentType } from "./generate-excel";
import { SkipCell, skipCell, Equation, writeEquation, createBorder, createAllBorders, createTopBorder, createBottomBorder, createLeftBorder, createRightBorder, createStyledCell, createBorderedCell, createDateCell, createBorderedDateCell } from "./util";
import { SkipCell, skipCell, Equation, writeEquation, createBorder, createAllBorders, createTopBorder, createBottomBorder, createLeftBorder, createRightBorder, createStyledCell, createBorderedCell, createDateCell, createBorderedDateCell, createBackgroundCell, createForegroundCell, createColoredCell, createBackgroundDateCell } from "./util";

/**
* Enum representing different cell types in Excel
Expand Down Expand Up @@ -72,6 +72,10 @@ interface IBorder {
interface ICellStyle {
/** Border configuration for the cell */
border?: IBorder;
/** Background (fill) color in hex format (e.g., "#FFFF00" for yellow) */
backgroundColor?: string;
/** Foreground (font) color in hex format (e.g., "#000000" for black text) */
foregroundColor?: string;
}

/**
Expand Down Expand Up @@ -273,6 +277,53 @@ const sampleData = [
'Mixed Content'
]
]
},
// Demonstration of color functionality
{
title: 'ColorDemo',
content: [
[
// Header row with colors
'Feature',
'Background Color',
'Foreground Color',
'Both Colors'
],
[
// Background colors
'Yellow Background',
createBackgroundCell('Highlighted', '#FFFF00'),
createForegroundCell('Red Text', '#FF0000'),
createColoredCell('Both', '#00FF00', '#FF0000')
],
[
'More Colors',
createBackgroundCell('Blue BG', '#0000FF'),
createForegroundCell('Green Text', '#00FF00'),
createColoredCell('Purple/White', '#800080', '#FFFFFF')
],
[
// Colored dates
'Date Colors',
createBackgroundDateCell(new Date(), '#FFCCCC'),
createDateCell(new Date('2024-12-25'), {
backgroundColor: '#00FF00',
foregroundColor: '#FF0000'
}),
'Mixed with dates'
],
[
// Combined with borders
'With Borders',
createStyledCell('Complex', {
backgroundColor: '#FFFFCC',
foregroundColor: '#0000FF',
border: createAllBorders(BorderStyle.thick, '#000000')
}),
'Plain text',
42
]
]
}
]

Expand All @@ -283,4 +334,4 @@ const sampleData = [
* This includes the main generation function, sample data, environment types,
* utility functions, and all border/styling helper functions
*/
export { generateExcel, sampleData, EnvironmentType, skipCell, writeEquation, createBorder, createAllBorders, createTopBorder, createBottomBorder, createLeftBorder, createRightBorder, createStyledCell, createBorderedCell, createDateCell, createBorderedDateCell };
export { generateExcel, sampleData, EnvironmentType, skipCell, writeEquation, createBorder, createAllBorders, createTopBorder, createBottomBorder, createLeftBorder, createRightBorder, createStyledCell, createBorderedCell, createDateCell, createBorderedDateCell, createBackgroundCell, createForegroundCell, createColoredCell, createBackgroundDateCell };
88 changes: 87 additions & 1 deletion src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,25 @@ const getBorderKey = (border?: IBorder): string => {
return parts.join("-");
};

/**
* Generates a unique key string for complete cell style configuration
* Used internally to identify unique style combinations for Excel styling
* @param {ICellStyle} style - Complete style configuration object
* @returns {string} Unique key representing the complete style configuration
* @internal
*/
const getStyleKey = (style?: ICellStyle): string => {
if (!style) return "default";

const parts = [
getBorderKey(style.border),
style.backgroundColor || "no-bg",
style.foregroundColor || "no-fg"
];

return parts.join("|");
};

/**
* Creates a styled cell with custom styling options
* @param {string | number} value - Cell value (string or number)
Expand Down Expand Up @@ -320,6 +339,68 @@ const createBorderedDateCell = (
return createDateCell(date, { border });
};

/**
* Creates a cell with background color styling
* Convenience function for applying background color to cells
* @param {string | number} value - Cell value (string or number)
* @param {string} backgroundColor - Background color in hex format (e.g., "#FFFF00")
* @returns {ICell} Cell with background color styling applied
* @example createBackgroundCell('Highlighted', '#FFFF00')
*/
const createBackgroundCell = (
value: string | number,
backgroundColor: string
): ICell => {
return createStyledCell(value, { backgroundColor });
};

/**
* Creates a cell with foreground (text) color styling
* Convenience function for applying text color to cells
* @param {string | number} value - Cell value (string or number)
* @param {string} foregroundColor - Text color in hex format (e.g., "#FF0000")
* @returns {ICell} Cell with text color styling applied
* @example createForegroundCell('Red Text', '#FF0000')
*/
const createForegroundCell = (
value: string | number,
foregroundColor: string
): ICell => {
return createStyledCell(value, { foregroundColor });
};

/**
* Creates a cell with both background and foreground color styling
* Convenience function for applying both background and text colors
* @param {string | number} value - Cell value (string or number)
* @param {string} backgroundColor - Background color in hex format
* @param {string} foregroundColor - Text color in hex format
* @returns {ICell} Cell with color styling applied
* @example createColoredCell('Styled Text', '#FFFF00', '#FF0000')
*/
const createColoredCell = (
value: string | number,
backgroundColor: string,
foregroundColor: string
): ICell => {
return createStyledCell(value, { backgroundColor, foregroundColor });
};

/**
* Creates a date cell with background color styling
* Convenience function for applying background color to date cells
* @param {Date} date - JavaScript Date object
* @param {string} backgroundColor - Background color in hex format
* @returns {ICell} Date cell with background color styling applied
* @example createBackgroundDateCell(new Date(), '#FFFF00')
*/
const createBackgroundDateCell = (
date: Date,
backgroundColor: string
): ICell => {
return createDateCell(date, { backgroundColor });
};

/**
* Export all utility functions and classes for external use
* Includes positioning utilities, cell creation helpers, border functions, and internal utilities
Expand All @@ -341,9 +422,14 @@ export {
createLeftBorder,
createRightBorder,
getBorderKey,
getStyleKey,
createStyledCell,
createBorderedCell,
dateToExcelSerial,
createDateCell,
createBorderedDateCell
createBorderedDateCell,
createBackgroundCell,
createForegroundCell,
createColoredCell,
createBackgroundDateCell
}
Loading