@20baht/export 0.1.2 → 0.1.3

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
@@ -111,7 +111,7 @@ const bytes = await xlsx([page, expenses]); // beside sheets of rows, a tab each
111
111
  ```
112
112
 
113
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
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
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,8 +40,8 @@ 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;
package/dist/xlsx.js CHANGED
@@ -36,16 +36,17 @@ 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 sidesOf = (border) => border === true
41
+ ? SIDES.join(',')
42
+ : SIDES.filter((side) => border !== undefined && border !== false && border.includes(side)).join(',');
42
43
  class Styles {
43
44
  #formats = new Map();
44
45
  #looks = new Map();
45
46
  #xfs = [];
46
47
  #fonts = new Map();
47
48
  #fills = new Map();
48
- #bordered = false;
49
+ #borders = new Map();
49
50
  of(format) {
50
51
  return this.look({ ...(format === undefined ? {} : { format }) });
51
52
  }
@@ -60,8 +61,9 @@ class Styles {
60
61
  look.color ?? '',
61
62
  look.fill ?? '',
62
63
  look.align ?? '',
64
+ look.border === true ? '' : sidesOf(look.border),
63
65
  ].join('|');
64
- const key = flags + (more === '||||' ? '' : `${more}|`) + (format ?? '');
66
+ const key = flags + (more === '|||||' ? '' : `${more}|`) + (format ?? '');
65
67
  if (key === '00000')
66
68
  return 0;
67
69
  if (key === '10000')
@@ -111,20 +113,31 @@ class Styles {
111
113
  }
112
114
  return at;
113
115
  }
116
+ #border(look) {
117
+ const sides = sidesOf(look.border);
118
+ if (sides === '')
119
+ return 0;
120
+ let at = this.#borders.get(sides);
121
+ if (at === undefined) {
122
+ at = this.#borders.size + 1;
123
+ this.#borders.set(sides, at);
124
+ }
125
+ return at;
126
+ }
114
127
  #xf(look, format) {
115
128
  const on = (flag, text) => (flag === true ? text : '');
116
- this.#bordered ||= look.border === true;
129
+ const border = this.#border(look);
117
130
  const font = this.#font(look);
118
131
  const fill = this.#fill(look);
119
132
  const across = look.align ?? (look.center === true ? 'center' : undefined);
120
133
  const upDown = look.wrap === true || look.center === true;
121
134
  const aligned = upDown || across !== undefined;
122
135
  const attrs = `numFmtId="${String(this.#numFmt(format))}" fontId="${String(font)}"` +
123
- ` fillId="${String(fill)}" borderId="${on(look.border, '1') || '0'}" xfId="0"` +
136
+ ` fillId="${String(fill)}" borderId="${String(border)}" xfId="0"` +
124
137
  on(format !== undefined, ' applyNumberFormat="1"') +
125
138
  on(font !== 0, ' applyFont="1"') +
126
139
  on(fill !== 0, ' applyFill="1"') +
127
- on(look.border, ' applyBorder="1"') +
140
+ on(border !== 0, ' applyBorder="1"') +
128
141
  on(aligned, ' applyAlignment="1"');
129
142
  if (!aligned)
130
143
  return `<xf ${attrs}/>`;
@@ -151,7 +164,13 @@ class Styles {
151
164
  ]
152
165
  .map((colour) => `<fill><patternFill patternType="solid"><fgColor rgb="FF${colour}"/><bgColor indexed="64"/></patternFill></fill>`)
153
166
  .join('')}</fills>` +
154
- `<borders count="${this.#bordered ? '2' : '1'}"><border><left/><right/><top/><bottom/><diagonal/></border>${this.#bordered ? `<border>${THIN}<diagonal/></border>` : ''}</borders>` +
167
+ `<borders count="${String(this.#borders.size + 1)}"><border><left/><right/><top/><bottom/><diagonal/></border>${[
168
+ ...this.#borders.keys(),
169
+ ]
170
+ .map((sides) => `<border>${SIDES.map((side) => sides.split(',').includes(side)
171
+ ? `<${side} style="thin"><color auto="1"/></${side}>`
172
+ : `<${side}/>`).join('')}<diagonal/></border>`)
173
+ .join('')}</borders>` +
155
174
  `<cellStyleXfs count="1"><xf numFmtId="0" fontId="0" fillId="0" borderId="0"/></cellStyleXfs>` +
156
175
  `<cellXfs count="${String(this.#xfs.length + 2)}"><xf numFmtId="0" fontId="0" fillId="0" borderId="0" xfId="0"/>` +
157
176
  `<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.3",
4
4
  "type": "module",
5
5
  "engines": {
6
6
  "node": ">=20.19.0"