@20baht/spreadsheet 0.0.0-stage → 0.1.0

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/dist/tsv.js ADDED
@@ -0,0 +1,23 @@
1
+ const quote = (cell) => /["\t\n\r]/.test(cell) ? `"${cell.replaceAll('"', '""')}"` : cell;
2
+ export function toTsv(cells) {
3
+ return cells.map((row) => row.map(quote).join('\t')).join('\n');
4
+ }
5
+ const CELL = /"((?:[^"]|"")*)"?([^\t\n]*)|([^\t\n]*)/y;
6
+ export function fromTsv(text) {
7
+ const source = text.replace(/\r\n?/g, '\n').replace(/\n$/, '');
8
+ if (source === '')
9
+ return [];
10
+ const rows = [[]];
11
+ CELL.lastIndex = 0;
12
+ for (;;) {
13
+ const match = CELL.exec(source);
14
+ const quoted = match[1];
15
+ rows.at(-1).push(quoted === undefined ? (match[3] ?? '') : quoted.replaceAll('""', '"') + (match[2] ?? ''));
16
+ const next = source[CELL.lastIndex];
17
+ if (next === undefined)
18
+ return rows;
19
+ if (next === '\n')
20
+ rows.push([]);
21
+ CELL.lastIndex++;
22
+ }
23
+ }
@@ -0,0 +1,36 @@
1
+ import { type Book, type CellAlign } from './book.js';
2
+ export interface WorkbookCell {
3
+ /** What the cell holds; for a formula, what it works out to here (a file's reader works it out again). */
4
+ value: string | number | boolean | null;
5
+ /** The formula, without its `=`. */
6
+ formula?: string;
7
+ bold?: boolean;
8
+ italic?: boolean;
9
+ underline?: boolean;
10
+ /** `#rrggbb`. */
11
+ color?: string;
12
+ fill?: string;
13
+ align?: CellAlign;
14
+ /** An Excel number format. */
15
+ format?: string;
16
+ }
17
+ export interface WorkbookSheet {
18
+ name: string;
19
+ cells: WorkbookCell[][];
20
+ merges: {
21
+ row: number;
22
+ column: number;
23
+ rows: number;
24
+ columns: number;
25
+ }[];
26
+ /** In characters. */
27
+ widths: number[];
28
+ /** A data sheet keeps its first row in view. */
29
+ frozen: number;
30
+ }
31
+ /**
32
+ * The book's sheets, each laid out for a workbook: every cell with its look, a formula as the
33
+ * formula and the value it has here, the merged ranges, the column widths. Hand the result to
34
+ * `@20baht/export`'s `xlsx()`.
35
+ */
36
+ export declare function bookWorkbook(book: Book): WorkbookSheet[];
@@ -0,0 +1,40 @@
1
+ import { addressOf, columnName, DEFAULT_WIDTH, extentOf, mergesOf, } from './book.js';
2
+ import { computeBook } from './compute.js';
3
+ import { isError } from './formula.js';
4
+ const FORMATS = {
5
+ number: '#,##0.00',
6
+ integer: '#,##0',
7
+ percent: '0.00%',
8
+ };
9
+ const PIXELS_PER_CHARACTER = 7;
10
+ const held = (value) => (isError(value) ? value.error : value);
11
+ function laid(sheet, at) {
12
+ const { rows, columns } = extentOf(sheet);
13
+ const cells = Array.from({ length: rows }, (_, row) => Array.from({ length: columns }, (_unused, column) => {
14
+ const address = addressOf({ row, column });
15
+ const cell = Object.hasOwn(sheet.cells, address) ? sheet.cells[address] : undefined;
16
+ const { format, ...look } = cell?.style ?? {};
17
+ return {
18
+ value: held(at(row, column)),
19
+ ...(cell?.formula === undefined ? {} : { formula: cell.formula }),
20
+ ...look,
21
+ ...(format ? { format: FORMATS[format] } : {}),
22
+ };
23
+ }));
24
+ return {
25
+ name: sheet.name,
26
+ cells,
27
+ merges: mergesOf(sheet).map((range) => ({
28
+ row: range.top,
29
+ column: range.left,
30
+ rows: range.bottom - range.top + 1,
31
+ columns: range.right - range.left + 1,
32
+ })),
33
+ widths: Array.from({ length: columns }, (_, column) => Math.round((sheet.widths?.[columnName(column)] ?? DEFAULT_WIDTH) / PIXELS_PER_CHARACTER)),
34
+ frozen: sheet.source ? 1 : 0,
35
+ };
36
+ }
37
+ export function bookWorkbook(book) {
38
+ const values = computeBook(book);
39
+ return book.sheets.map((sheet) => laid(sheet, (row, column) => values.at(sheet.key, row, column)));
40
+ }
package/package.json CHANGED
@@ -1,6 +1,77 @@
1
1
  {
2
2
  "name": "@20baht/spreadsheet",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.0",
4
+ "type": "module",
5
+ "engines": {
6
+ "node": ">=20.19.0"
7
+ },
8
+ "description": "A book of free cells from one JSON document: values and formulas (SUM, COUNT, IF, COUNTIF, …), bold / italic / colours, merged ranges, and data sheets a host fills from its own records. Headless: the authoring gate, the formulas, the grid session, the .xlsx layout. /element: <baht-spreadsheet>, a grid worked like Excel.",
9
+ "license": "MIT",
10
+ "author": "Nattapong Promthong",
11
+ "repository": {
12
+ "type": "git",
13
+ "url": "git+https://github.com/bank20baht/20baht.git",
14
+ "directory": "packages/core/spreadsheet"
15
+ },
16
+ "homepage": "https://github.com/bank20baht/20baht/tree/main/packages/core/spreadsheet#readme",
17
+ "bugs": {
18
+ "url": "https://github.com/bank20baht/20baht/issues"
19
+ },
20
+ "keywords": [
21
+ "spreadsheet",
22
+ "formulas",
23
+ "excel",
24
+ "xlsx",
25
+ "datagrid",
26
+ "json",
27
+ "web-components",
28
+ "lit",
29
+ "bahtui"
30
+ ],
31
+ "publishConfig": {
32
+ "access": "public"
33
+ },
34
+ "files": [
35
+ "dist"
36
+ ],
37
+ "exports": {
38
+ ".": {
39
+ "types": "./dist/index.d.ts",
40
+ "import": "./dist/index.js",
41
+ "default": "./dist/index.js"
42
+ },
43
+ "./element": {
44
+ "types": "./dist/element.d.ts",
45
+ "import": "./dist/element.js",
46
+ "default": "./dist/element.js"
47
+ },
48
+ "./spreadsheet.css": "./dist/spreadsheet.css",
49
+ "./custom-elements.json": "./dist/custom-elements.json",
50
+ "./package.json": "./package.json"
51
+ },
52
+ "scripts": {
53
+ "build": "rm -rf dist && tsc -p tsconfig.json --removeComments && tsc -p tsconfig.json --emitDeclarationOnly && cp src/spreadsheet.css dist/spreadsheet.css && bun run analyze",
54
+ "analyze": "custom-elements-manifest analyze",
55
+ "test": "vitest run",
56
+ "test:watch": "vitest",
57
+ "test:coverage": "vitest run --coverage"
58
+ },
59
+ "dependencies": {
60
+ "@20baht/element": ">=0.1.0 <0.2.0",
61
+ "lit": "^3.3.0"
62
+ },
63
+ "devDependencies": {
64
+ "@20baht/export": "workspace:*",
65
+ "@custom-elements-manifest/analyzer": "^0.11.0",
66
+ "happy-dom": "^21.0.4",
67
+ "typescript": "^5.9.3",
68
+ "vitest": "^3.2.4"
69
+ },
70
+ "main": "./dist/index.js",
71
+ "types": "./dist/index.d.ts",
72
+ "sideEffects": [
73
+ "./dist/element.js",
74
+ "./dist/elements/baht-*.js"
75
+ ],
76
+ "customElements": "dist/custom-elements.json"
77
+ }