8000 feat(excel): add centeredCell function · composize/composize@6bcda0c · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Commit 6bcda0c

Browse files
committed
feat(excel): add centeredCell function
1 parent ea462a5 commit 6bcda0c

File tree

2 files changed

+37
-3
lines changed

2 files changed

+37
-3
lines changed

packages/excel/src/dsl.spec.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { borderedCell, cell, row, workbook, worksheet } from './dsl';
1+
import { borderedCell, cell, centeredCell, row, workbook, worksheet } from './dsl';
22

33
describe('Excel DSL', () => {
44

@@ -241,4 +241,25 @@ describe('Excel DSL', () => {
241241
expect(createdCell.border?.bottom?.style).toBe('thin');
242242
expect(createdCell.border?.right?.style).toBe('thin');
243243
});
244+
245+
it('should create a centered cell with thin borders and centered alignment', () => {
246+
const book = workbook(() => {
247+
row(() => {
248+
centeredCell('centered', { numFmt: '0.00%' });
249+
});
250+
});
251+
252+
const sheet = book.worksheets[0];
253+
const createdCell = sheet.getRow(1).getCell(1);
254+
255+
expect(createdCell.value).toBe('centered');
256+
expect(createdCell.numFmt).toBe('0.00%');
257+
expect(createdCell.border?.top?.style).toBe('thin');
258+
expect(createdCell.border?.left?.style).toBe('thin');
259+
expect(createdCell.border?.bottom?.style).toBe('thin');
260+
expect(createdCell.border?.right?.style).toBe('thin');
261+
expect(createdCell.alignment?.horizontal).toBe('center');
262+
expect(createdCell.alignment?.vertical).toBe('middle');
263+
});
264+
244265
});

packages/excel/src/dsl.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ export type CellOptions = Partial<{ colSpan: number, rowSpan: number } & Pick<Ce
8181
/**
8282
* Creates and configures a cell within the current worksheet at the current row and column position.
8383
*
84-
* @param value The value to be placed into the cell. Can be of any type supported by exceljs.
84+
* @param value The value to be placed into the cell.
8585
* @param options Optional configuration for the cell.
8686
* @returns The `exceljs.Cell` object that was created and configured.
8787
*/
@@ -124,7 +124,7 @@ export function cell(value: any, options: CellOptions = {}) {
124124
/**
125125
* Creates a cell object with a thin border applied to all four sides.
126126
*
127-
* @param value - The value to be placed into the cell. Can be of any type supported by exceljs.
127+
* @param value - The value to be placed into the cell.
128128
* @param options - Optional configuration for the cell.
129129
* @returns The `exceljs.Cell` object that was created and configured.
130130
*/
@@ -140,6 +140,19 @@ export function borderedCell(value: any, options: CellOptions = {}) {
140140
})
141141
}
142142

143+
/**
144+
* Creates a cell configuration with content centered horizontally and vertically.
145+
*
146+
* @param value - The value to be placed into the cell.
147+
* @param options - Optional configuration for the cell.
148+
*/
149+
export function centeredCell(value: any, options: CellOptions = {}) {
150+
return borderedCell(value, {
151+
alignment: { horizontal: 'center', vertical: 'middle' },
152+
...options
153+
});
154+
}
155+
143156
const DEFAULT_FONT_SIZE = 11;
144157
const DEFAULT_COL_WIDTH = 8.43;
145158

0 commit comments

Comments
 (0)
0