xls-reader 0.6.0 → 0.7.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/CHANGELOG.md +15 -0
- package/README.md +15 -11
- package/README.pt-BR.md +10 -5
- package/dist/cli.mjs +47 -3
- package/package.json +2 -2
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,20 @@
|
|
|
1
1
|
# xls-reader
|
|
2
2
|
|
|
3
|
+
## 0.7.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- [#48](https://github.com/zanlucathiago/xls-reader/pull/48) [`0399e14`](https://github.com/zanlucathiago/xls-reader/commit/0399e14e6f55f23e9cc229a010910f2d5b51f574) Thanks [@zanlucathiago](https://github.com/zanlucathiago)! - Add `sheetToCsv(sheet, options?)`, a CSV counterpart to `sheetToObjects`. It
|
|
8
|
+
serializes a sheet's grid to a CSV string with RFC-4180 quoting: a `Date` becomes
|
|
9
|
+
a UTC ISO-8601 string, a `CellError` becomes its code (e.g. `#DIV/0!`), and a
|
|
10
|
+
blank cell becomes an empty field. `delimiter` defaults to `,` and `eol` to `\n`
|
|
11
|
+
(pass `\r\n` for Excel-style output). Exports the new `SheetToCsvOptions` type.
|
|
12
|
+
|
|
13
|
+
The `xls-reader` CLI gains a `--csv` flag that prints the selected sheet as CSV
|
|
14
|
+
instead of JSON. CSV is a single flat table, so `--csv` needs the selection to
|
|
15
|
+
resolve to exactly one sheet — pair it with `--sheet <name|index>` on a
|
|
16
|
+
multi-sheet workbook — and it can't be combined with `--objects`.
|
|
17
|
+
|
|
3
18
|
## 0.6.0
|
|
4
19
|
|
|
5
20
|
### Minor Changes
|
package/README.md
CHANGED
|
@@ -126,22 +126,26 @@ try {
|
|
|
126
126
|
## Command line
|
|
127
127
|
|
|
128
128
|
No code needed for a quick look or a shell pipeline — the package ships an
|
|
129
|
-
`xls-reader` bin that prints a workbook's cells as JSON to stdout:
|
|
129
|
+
`xls-reader` bin that prints a workbook's cells as JSON (or CSV) to stdout:
|
|
130
130
|
|
|
131
131
|
```bash
|
|
132
|
-
npx xls-reader report.xls
|
|
133
|
-
npx xls-reader report.xls --objects
|
|
132
|
+
npx xls-reader report.xls # every sheet, pretty-printed
|
|
133
|
+
npx xls-reader report.xls --objects # rows keyed by the header row
|
|
134
134
|
npx xls-reader report.xls --sheet 0 --compact > sheet0.json
|
|
135
|
+
npx xls-reader report.xls --csv --sheet 0 > sheet0.csv
|
|
135
136
|
```
|
|
136
137
|
|
|
137
|
-
| Flag | Effect
|
|
138
|
-
| ------------------- |
|
|
139
|
-
| `--objects` | Each row as an object keyed by the header row
|
|
140
|
-
| `--
|
|
141
|
-
| `--
|
|
142
|
-
| `--
|
|
143
|
-
|
|
144
|
-
|
|
138
|
+
| Flag | Effect |
|
|
139
|
+
| ------------------- | ------------------------------------------------ |
|
|
140
|
+
| `--objects` | Each row as an object keyed by the header row |
|
|
141
|
+
| `--csv` | The selected sheet as CSV (needs a single sheet) |
|
|
142
|
+
| `--sheet <name\|n>` | Only the sheet with this name or 0-based index |
|
|
143
|
+
| `--visible-only` | Skip hidden and very-hidden sheets |
|
|
144
|
+
| `--compact` | Single-line JSON (default is pretty-printed) |
|
|
145
|
+
|
|
146
|
+
Output goes to stdout; errors go to stderr with a non-zero exit code. `--csv`
|
|
147
|
+
serializes a single sheet, so pair it with `--sheet` when the workbook has more
|
|
148
|
+
than one — otherwise it exits with an error listing the available sheets.
|
|
145
149
|
|
|
146
150
|
## API
|
|
147
151
|
|
package/README.pt-BR.md
CHANGED
|
@@ -129,23 +129,28 @@ try {
|
|
|
129
129
|
## Linha de comando
|
|
130
130
|
|
|
131
131
|
Sem escrever código, para uma olhada rápida ou um pipeline no shell — o pacote
|
|
132
|
-
inclui um bin `xls-reader` que imprime as células de um workbook como JSON
|
|
133
|
-
stdout:
|
|
132
|
+
inclui um bin `xls-reader` que imprime as células de um workbook como JSON (ou
|
|
133
|
+
CSV) no stdout:
|
|
134
134
|
|
|
135
135
|
```bash
|
|
136
|
-
npx xls-reader relatorio.xls
|
|
137
|
-
npx xls-reader relatorio.xls --objects
|
|
136
|
+
npx xls-reader relatorio.xls # todas as planilhas, formatado
|
|
137
|
+
npx xls-reader relatorio.xls --objects # linhas com chaves do cabeçalho
|
|
138
138
|
npx xls-reader relatorio.xls --sheet 0 --compact > planilha0.json
|
|
139
|
+
npx xls-reader relatorio.xls --csv --sheet 0 > planilha0.csv
|
|
139
140
|
```
|
|
140
141
|
|
|
141
142
|
| Flag | Efeito |
|
|
142
143
|
| ------------------- | ------------------------------------------------------- |
|
|
143
144
|
| `--objects` | Cada linha como objeto com chaves da linha de cabeçalho |
|
|
145
|
+
| `--csv` | A planilha selecionada como CSV (exige uma única aba) |
|
|
144
146
|
| `--sheet <nome\|n>` | Só a planilha com este nome ou índice (base 0) |
|
|
145
147
|
| `--visible-only` | Ignora planilhas `hidden` e `very-hidden` |
|
|
146
148
|
| `--compact` | JSON em uma linha (o padrão é formatado) |
|
|
147
149
|
|
|
148
|
-
|
|
150
|
+
A saída vai para o stdout; erros vão para o stderr com código de saída diferente
|
|
151
|
+
de zero. O `--csv` serializa uma única planilha, então combine com `--sheet`
|
|
152
|
+
quando o workbook tiver mais de uma — do contrário ele encerra com erro listando
|
|
153
|
+
as planilhas disponíveis.
|
|
149
154
|
|
|
150
155
|
## API
|
|
151
156
|
|
package/dist/cli.mjs
CHANGED
|
@@ -830,6 +830,10 @@ function parseArgs(argv) {
|
|
|
830
830
|
kind: "error",
|
|
831
831
|
message: "missing .xls file argument (try --help)"
|
|
832
832
|
};
|
|
833
|
+
if (acc.format === "csv" && acc.asObjects) return {
|
|
834
|
+
kind: "error",
|
|
835
|
+
message: "--csv cannot be combined with --objects (try --help)"
|
|
836
|
+
};
|
|
833
837
|
return {
|
|
834
838
|
kind: "run",
|
|
835
839
|
options: {
|
|
@@ -844,7 +848,8 @@ function createAccumulator() {
|
|
|
844
848
|
sheet: void 0,
|
|
845
849
|
asObjects: false,
|
|
846
850
|
visibleOnly: false,
|
|
847
|
-
compact: false
|
|
851
|
+
compact: false,
|
|
852
|
+
format: "json"
|
|
848
853
|
};
|
|
849
854
|
}
|
|
850
855
|
function hasFlag(argv, ...names) {
|
|
@@ -856,6 +861,7 @@ function applyToken(token, acc) {
|
|
|
856
861
|
return null;
|
|
857
862
|
}
|
|
858
863
|
if (token === "--objects") acc.asObjects = true;
|
|
864
|
+
else if (token === "--csv") acc.format = "csv";
|
|
859
865
|
else if (token === "--visible-only") acc.visibleOnly = true;
|
|
860
866
|
else if (token === "--compact") acc.compact = true;
|
|
861
867
|
else if (token.startsWith("-")) return `unknown option "${token}" (try --help)`;
|
|
@@ -866,13 +872,14 @@ function applyToken(token, acc) {
|
|
|
866
872
|
|
|
867
873
|
//#endregion
|
|
868
874
|
//#region src/cli/help.ts
|
|
869
|
-
const HELP_TEXT = `xls-reader — read a legacy .xls (BIFF8 / Excel 97-2003) and print its cells as JSON
|
|
875
|
+
const HELP_TEXT = `xls-reader — read a legacy .xls (BIFF8 / Excel 97-2003) and print its cells as JSON or CSV
|
|
870
876
|
|
|
871
877
|
Usage:
|
|
872
878
|
xls-reader <file.xls> [options]
|
|
873
879
|
|
|
874
880
|
Options:
|
|
875
881
|
--objects Emit each row as an object keyed by the header row
|
|
882
|
+
--csv Emit the selected sheet as CSV (needs a single sheet)
|
|
876
883
|
--sheet <name|n> Only the sheet with this name or 0-based index
|
|
877
884
|
--visible-only Skip hidden and very-hidden sheets
|
|
878
885
|
--compact Single-line JSON (default is pretty-printed)
|
|
@@ -882,9 +889,36 @@ Options:
|
|
|
882
889
|
Examples:
|
|
883
890
|
xls-reader report.xls
|
|
884
891
|
xls-reader report.xls --objects --sheet 0
|
|
892
|
+
xls-reader report.xls --csv --sheet 0 > report.csv
|
|
885
893
|
npx xls-reader report.xls --visible-only > report.json
|
|
886
894
|
`;
|
|
887
895
|
|
|
896
|
+
//#endregion
|
|
897
|
+
//#region src/sheet-to-csv.ts
|
|
898
|
+
function sheetToCsv(sheet, options = {}) {
|
|
899
|
+
const delimiter = options.delimiter ?? ",";
|
|
900
|
+
const eol = options.eol ?? "\n";
|
|
901
|
+
assertDelimiter(delimiter);
|
|
902
|
+
return sheet.rows.map((row) => encodeRow(row, delimiter)).join(eol);
|
|
903
|
+
}
|
|
904
|
+
function encodeRow(row, delimiter) {
|
|
905
|
+
return row.map((cell) => encodeField(cellToText(cell), delimiter)).join(delimiter);
|
|
906
|
+
}
|
|
907
|
+
function cellToText(cell) {
|
|
908
|
+
if (cell === null) return "";
|
|
909
|
+
if (cell instanceof Date) return cell.toISOString();
|
|
910
|
+
if (cell instanceof CellError) return cell.code;
|
|
911
|
+
return String(cell);
|
|
912
|
+
}
|
|
913
|
+
function encodeField(text, delimiter) {
|
|
914
|
+
if (!text.includes(delimiter) && !/["\r\n]/.test(text)) return text;
|
|
915
|
+
return `"${text.replace(/"/g, "\"\"")}"`;
|
|
916
|
+
}
|
|
917
|
+
function assertDelimiter(delimiter) {
|
|
918
|
+
if (delimiter.length === 1 && !/["\r\n]/.test(delimiter)) return;
|
|
919
|
+
throw new XlsError(`delimiter must be a single character other than '"', CR, or LF, got ${JSON.stringify(delimiter)}`);
|
|
920
|
+
}
|
|
921
|
+
|
|
888
922
|
//#endregion
|
|
889
923
|
//#region src/sheet-to-objects.ts
|
|
890
924
|
function sheetToObjects(sheet, options = {}) {
|
|
@@ -919,9 +953,19 @@ function assertHeaderRow(headerRow) {
|
|
|
919
953
|
//#endregion
|
|
920
954
|
//#region src/cli/render.ts
|
|
921
955
|
function renderWorkbook(workbook, options) {
|
|
922
|
-
const
|
|
956
|
+
const sheets = selectSheets(workbook.sheets, options);
|
|
957
|
+
if (options.format === "csv") return renderCsv(sheets);
|
|
958
|
+
const payload = sheets.map((sheet) => renderSheet(sheet, options));
|
|
923
959
|
return JSON.stringify(payload, null, options.compact ? void 0 : 2);
|
|
924
960
|
}
|
|
961
|
+
function renderCsv(sheets) {
|
|
962
|
+
const only = sheets[0];
|
|
963
|
+
if (sheets.length !== 1 || only === void 0) {
|
|
964
|
+
const names = sheets.map((s) => s.name).join(", ") || "(none)";
|
|
965
|
+
throw new XlsError(`--csv needs exactly one sheet, but ${sheets.length} are selected (${names}) — narrow it with --sheet <name|index>`);
|
|
966
|
+
}
|
|
967
|
+
return sheetToCsv(only);
|
|
968
|
+
}
|
|
925
969
|
function renderSheet(sheet, options) {
|
|
926
970
|
const rows = options.asObjects ? sheetToObjects(sheet) : sheet.rows;
|
|
927
971
|
return {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "xls-reader",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.7.0",
|
|
4
4
|
"description": "Zero-dependency .xls (BIFF8 / Excel 97-2003) reader for Node and the browser — a lightweight SheetJS alternative that returns each worksheet as typed cells.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"xls",
|
|
@@ -74,7 +74,7 @@
|
|
|
74
74
|
"@vitest/coverage-v8": "^4.1.9",
|
|
75
75
|
"eslint": "^10.6.0",
|
|
76
76
|
"prettier": "^3.4.2",
|
|
77
|
-
"tsdown": "0.22.
|
|
77
|
+
"tsdown": "0.22.7",
|
|
78
78
|
"typescript": "^6.0.3",
|
|
79
79
|
"typescript-eslint": "^8.18.1",
|
|
80
80
|
"vite": "^8.0.16",
|