@20baht/export 0.1.3 → 0.1.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -110,8 +110,8 @@ const page: LaidSheet = {
110
110
  const bytes = await xlsx([page, expenses]); // beside sheets of rows, a tab each
111
111
  ```
112
112
 
113
- A cell is its value (`string | number | boolean | Date | null`) or `{ value, bold, italic, underline, color, shade, fill, border, wrap, center, align, format }`
114
- (`color` and `fill` as `#rrggbb`; `align` is `left` / `center` / `right`; `border` is `true` or the sides, `['top', 'bottom']`). With `formula` (Excel's spelling, `SUM(A1:A9)`), the cell is
113
+ A cell is its value (`string | number | boolean | Date | null`) or `{ value, bold, italic, underline, color, shade, fill, border, wrap, center, align, valign, format }`
114
+ (`color` and `fill` as `#rrggbb`; `align` is `left` / `center` / `right`, `valign` `top` / `middle` / `bottom`; `border` is `true` or the sides, `['top', 'bottom']`). With `formula` (Excel's spelling, `SUM(A1:A9)`), the cell is
115
115
  that formula and `value` is what it had when the file was written: the workbook asks its reader to work it out again.
116
116
  `@20baht/spreadsheet`'s `bookWorkbook` lays a book out this way.
117
117
  `@20baht/sheet`'s `sheetWorkbook(sheet, data)` lays a filled sheet out this way.
package/dist/sheet.d.ts CHANGED
@@ -36,6 +36,8 @@ export interface CellLook {
36
36
  underline?: boolean;
37
37
  /** The text's colour, `#rrggbb`. */
38
38
  color?: string;
39
+ /** The text's size in points (6 to 72). Unset: 11. */
40
+ size?: number;
39
41
  /** A light grey fill: a header, a band. */
40
42
  shade?: boolean;
41
43
  /** A fill of this colour, `#rrggbb`; it wins over `shade`. */
@@ -47,6 +49,8 @@ export interface CellLook {
47
49
  center?: boolean;
48
50
  /** Across only: left, centre or right. It wins over `center`'s across; up and down stays as it is. */
49
51
  align?: 'left' | 'center' | 'right';
52
+ /** Up and down only: top, middle or bottom. It wins over `center`'s and `wrap`'s up and down. */
53
+ valign?: 'top' | 'middle' | 'bottom';
50
54
  /** An Excel number format for a number or a date: `'#,##0.00'`, `'dd/mm/yyyy hh:mm'`. */
51
55
  format?: string;
52
56
  }
package/dist/xlsx.js CHANGED
@@ -37,9 +37,18 @@ const rgb = (colour) => {
37
37
  return colour.slice(1).toUpperCase();
38
38
  };
39
39
  const SIDES = ['left', 'right', 'top', 'bottom'];
40
+ const VERTICAL = {
41
+ top: 'top',
42
+ middle: 'center',
43
+ bottom: 'bottom',
44
+ };
40
45
  const sidesOf = (border) => border === true
41
46
  ? SIDES.join(',')
42
47
  : SIDES.filter((side) => border !== undefined && border !== false && border.includes(side)).join(',');
48
+ const DEFAULT_SIZE = 11;
49
+ const sizeOf = (size) => size !== undefined && Number.isFinite(size)
50
+ ? Math.min(72, Math.max(6, Math.round(size * 2) / 2))
51
+ : DEFAULT_SIZE;
43
52
  class Styles {
44
53
  #formats = new Map();
45
54
  #looks = new Map();
@@ -62,8 +71,10 @@ class Styles {
62
71
  look.fill ?? '',
63
72
  look.align ?? '',
64
73
  look.border === true ? '' : sidesOf(look.border),
74
+ look.valign ?? '',
75
+ look.size === undefined || look.size === DEFAULT_SIZE ? '' : String(look.size),
65
76
  ].join('|');
66
- const key = flags + (more === '|||||' ? '' : `${more}|`) + (format ?? '');
77
+ const key = flags + (more === '|||||||' ? '' : `${more}|`) + (format ?? '');
67
78
  if (key === '00000')
68
79
  return 0;
69
80
  if (key === '10000')
@@ -87,13 +98,15 @@ class Styles {
87
98
  return 164 + at;
88
99
  }
89
100
  #font(look) {
101
+ const size = sizeOf(look.size);
90
102
  const parts = (look.bold === true ? '<b/>' : '') +
91
103
  (look.italic === true ? '<i/>' : '') +
92
104
  (look.underline === true ? '<u/>' : '') +
105
+ `<sz val="${String(size)}"/>` +
93
106
  (look.color === undefined ? '' : `<color rgb="FF${rgb(look.color)}"/>`);
94
- if (parts === '')
107
+ if (parts === `<sz val="${String(DEFAULT_SIZE)}"/>`)
95
108
  return 0;
96
- if (parts === '<b/>')
109
+ if (parts === `<b/><sz val="${String(DEFAULT_SIZE)}"/>`)
97
110
  return 1;
98
111
  let at = this.#fonts.get(parts);
99
112
  if (at === undefined) {
@@ -130,8 +143,9 @@ class Styles {
130
143
  const font = this.#font(look);
131
144
  const fill = this.#fill(look);
132
145
  const across = look.align ?? (look.center === true ? 'center' : undefined);
133
- const upDown = look.wrap === true || look.center === true;
134
- const aligned = upDown || across !== undefined;
146
+ const middle = look.center === true ? 'center' : undefined;
147
+ const upDown = VERTICAL[look.valign ?? ''] ?? middle ?? (look.wrap === true ? 'top' : undefined);
148
+ const aligned = upDown !== undefined || across !== undefined;
135
149
  const attrs = `numFmtId="${String(this.#numFmt(format))}" fontId="${String(font)}"` +
136
150
  ` fillId="${String(fill)}" borderId="${String(border)}" xfId="0"` +
137
151
  on(format !== undefined, ' applyNumberFormat="1"') +
@@ -142,7 +156,7 @@ class Styles {
142
156
  if (!aligned)
143
157
  return `<xf ${attrs}/>`;
144
158
  const alignment = (across === undefined ? '' : ` horizontal="${across}"`) +
145
- on(upDown, ` vertical="${on(look.center, 'center') || 'top'}"`) +
159
+ (upDown === undefined ? '' : ` vertical="${upDown}"`) +
146
160
  on(look.wrap, ' wrapText="1"');
147
161
  return `<xf ${attrs}><alignment${alignment}/></xf>`;
148
162
  }
@@ -157,7 +171,7 @@ class Styles {
157
171
  `<fonts count="${String(this.#fonts.size + 2)}"><font><sz val="11"/><name val="Calibri"/></font><font><b/><sz val="11"/><name val="Calibri"/></font>${[
158
172
  ...this.#fonts.keys(),
159
173
  ]
160
- .map((parts) => `<font>${parts}<sz val="11"/><name val="Calibri"/></font>`)
174
+ .map((parts) => `<font>${parts}<name val="Calibri"/></font>`)
161
175
  .join('')}</fonts>` +
162
176
  `<fills count="${String(this.#fills.size + 2)}"><fill><patternFill patternType="none"/></fill><fill><patternFill patternType="gray125"/></fill>${[
163
177
  ...this.#fills.keys(),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@20baht/export",
3
- "version": "0.1.3",
3
+ "version": "0.1.5",
4
4
  "type": "module",
5
5
  "engines": {
6
6
  "node": ">=20.19.0"