@20baht/export 0.1.2 → 0.1.4

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`). 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/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  export { csv, csvStream, CSV_TYPE, type CsvOptions } from './csv.js';
2
2
  export { contentDisposition, contentTypeOf, download } from './download.js';
3
- export type { Cell, CellLook, CellMerge, Column, LaidCell, LaidSheet, Sheet, SyncSheet, } from './sheet.js';
3
+ export type { Cell, BorderSide, CellLook, CellMerge, Column, LaidCell, LaidSheet, Sheet, SyncSheet, } from './sheet.js';
4
4
  export { tableColumns, type TableColumnLike, type TableColumnsOptions, type TableLike, } from './table.js';
5
5
  export { columnName, xlsx, XLSX_TYPE, type XlsxOptions } from './xlsx.js';
package/dist/sheet.d.ts CHANGED
@@ -28,6 +28,8 @@ export interface Sheet<Row> {
28
28
  rows: Iterable<Row> | AsyncIterable<Row>;
29
29
  }
30
30
  /** How a laid-out cell looks in a workbook. */
31
+ /** A side of a cell a line can be drawn on. */
32
+ export type BorderSide = 'left' | 'right' | 'top' | 'bottom';
31
33
  export interface CellLook {
32
34
  bold?: boolean;
33
35
  italic?: boolean;
@@ -38,13 +40,15 @@ export interface CellLook {
38
40
  shade?: boolean;
39
41
  /** A fill of this colour, `#rrggbb`; it wins over `shade`. */
40
42
  fill?: string;
41
- /** A thin line on every side. */
42
- border?: boolean;
43
+ /** A thin line on every side, or on the sides named. */
44
+ border?: boolean | readonly BorderSide[];
43
45
  /** Long text breaks onto more lines instead of running over the next cell. */
44
46
  wrap?: boolean;
45
47
  center?: boolean;
46
48
  /** Across only: left, centre or right. It wins over `center`'s across; up and down stays as it is. */
47
49
  align?: 'left' | 'center' | 'right';
50
+ /** Up and down only: top, middle or bottom. It wins over `center`'s and `wrap`'s up and down. */
51
+ valign?: 'top' | 'middle' | 'bottom';
48
52
  /** An Excel number format for a number or a date: `'#,##0.00'`, `'dd/mm/yyyy hh:mm'`. */
49
53
  format?: string;
50
54
  }
package/dist/xlsx.js CHANGED
@@ -36,16 +36,22 @@ const rgb = (colour) => {
36
36
  throw new RangeError(`a colour is written #rrggbb, not ${colour}`);
37
37
  return colour.slice(1).toUpperCase();
38
38
  };
39
- const THIN = ['left', 'right', 'top', 'bottom']
40
- .map((side) => `<${side} style="thin"><color auto="1"/></${side}>`)
41
- .join('');
39
+ const SIDES = ['left', 'right', 'top', 'bottom'];
40
+ const VERTICAL = {
41
+ top: 'top',
42
+ middle: 'center',
43
+ bottom: 'bottom',
44
+ };
45
+ const sidesOf = (border) => border === true
46
+ ? SIDES.join(',')
47
+ : SIDES.filter((side) => border !== undefined && border !== false && border.includes(side)).join(',');
42
48
  class Styles {
43
49
  #formats = new Map();
44
50
  #looks = new Map();
45
51
  #xfs = [];
46
52
  #fonts = new Map();
47
53
  #fills = new Map();
48
- #bordered = false;
54
+ #borders = new Map();
49
55
  of(format) {
50
56
  return this.look({ ...(format === undefined ? {} : { format }) });
51
57
  }
@@ -60,8 +66,10 @@ class Styles {
60
66
  look.color ?? '',
61
67
  look.fill ?? '',
62
68
  look.align ?? '',
69
+ look.border === true ? '' : sidesOf(look.border),
70
+ look.valign ?? '',
63
71
  ].join('|');
64
- const key = flags + (more === '||||' ? '' : `${more}|`) + (format ?? '');
72
+ const key = flags + (more === '||||||' ? '' : `${more}|`) + (format ?? '');
65
73
  if (key === '00000')
66
74
  return 0;
67
75
  if (key === '10000')
@@ -111,25 +119,37 @@ class Styles {
111
119
  }
112
120
  return at;
113
121
  }
122
+ #border(look) {
123
+ const sides = sidesOf(look.border);
124
+ if (sides === '')
125
+ return 0;
126
+ let at = this.#borders.get(sides);
127
+ if (at === undefined) {
128
+ at = this.#borders.size + 1;
129
+ this.#borders.set(sides, at);
130
+ }
131
+ return at;
132
+ }
114
133
  #xf(look, format) {
115
134
  const on = (flag, text) => (flag === true ? text : '');
116
- this.#bordered ||= look.border === true;
135
+ const border = this.#border(look);
117
136
  const font = this.#font(look);
118
137
  const fill = this.#fill(look);
119
138
  const across = look.align ?? (look.center === true ? 'center' : undefined);
120
- const upDown = look.wrap === true || look.center === true;
121
- const aligned = upDown || across !== undefined;
139
+ const middle = look.center === true ? 'center' : undefined;
140
+ const upDown = VERTICAL[look.valign ?? ''] ?? middle ?? (look.wrap === true ? 'top' : undefined);
141
+ const aligned = upDown !== undefined || across !== undefined;
122
142
  const attrs = `numFmtId="${String(this.#numFmt(format))}" fontId="${String(font)}"` +
123
- ` fillId="${String(fill)}" borderId="${on(look.border, '1') || '0'}" xfId="0"` +
143
+ ` fillId="${String(fill)}" borderId="${String(border)}" xfId="0"` +
124
144
  on(format !== undefined, ' applyNumberFormat="1"') +
125
145
  on(font !== 0, ' applyFont="1"') +
126
146
  on(fill !== 0, ' applyFill="1"') +
127
- on(look.border, ' applyBorder="1"') +
147
+ on(border !== 0, ' applyBorder="1"') +
128
148
  on(aligned, ' applyAlignment="1"');
129
149
  if (!aligned)
130
150
  return `<xf ${attrs}/>`;
131
151
  const alignment = (across === undefined ? '' : ` horizontal="${across}"`) +
132
- on(upDown, ` vertical="${on(look.center, 'center') || 'top'}"`) +
152
+ (upDown === undefined ? '' : ` vertical="${upDown}"`) +
133
153
  on(look.wrap, ' wrapText="1"');
134
154
  return `<xf ${attrs}><alignment${alignment}/></xf>`;
135
155
  }
@@ -151,7 +171,13 @@ class Styles {
151
171
  ]
152
172
  .map((colour) => `<fill><patternFill patternType="solid"><fgColor rgb="FF${colour}"/><bgColor indexed="64"/></patternFill></fill>`)
153
173
  .join('')}</fills>` +
154
- `<borders count="${this.#bordered ? '2' : '1'}"><border><left/><right/><top/><bottom/><diagonal/></border>${this.#bordered ? `<border>${THIN}<diagonal/></border>` : ''}</borders>` +
174
+ `<borders count="${String(this.#borders.size + 1)}"><border><left/><right/><top/><bottom/><diagonal/></border>${[
175
+ ...this.#borders.keys(),
176
+ ]
177
+ .map((sides) => `<border>${SIDES.map((side) => sides.split(',').includes(side)
178
+ ? `<${side} style="thin"><color auto="1"/></${side}>`
179
+ : `<${side}/>`).join('')}<diagonal/></border>`)
180
+ .join('')}</borders>` +
155
181
  `<cellStyleXfs count="1"><xf numFmtId="0" fontId="0" fillId="0" borderId="0"/></cellStyleXfs>` +
156
182
  `<cellXfs count="${String(this.#xfs.length + 2)}"><xf numFmtId="0" fontId="0" fillId="0" borderId="0" xfId="0"/>` +
157
183
  `<xf numFmtId="0" fontId="1" fillId="0" borderId="0" xfId="0" applyFont="1"/>${this.#xfs.join('')}</cellXfs>` +
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@20baht/export",
3
- "version": "0.1.2",
3
+ "version": "0.1.4",
4
4
  "type": "module",
5
5
  "engines": {
6
6
  "node": ">=20.19.0"