xls-reader 0.7.3 → 0.8.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 +12 -0
- package/README.md +20 -5
- package/README.pt-BR.md +21 -5
- package/dist/cli.mjs +87 -40
- package/dist/index.cjs +88 -40
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +16 -1
- package/dist/index.d.mts +16 -1
- package/dist/index.mjs +88 -41
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,17 @@
|
|
|
1
1
|
# xls-reader
|
|
2
2
|
|
|
3
|
+
## 0.8.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- [#69](https://github.com/zanlucathiago/xls-reader/pull/69) [`7aa4e3c`](https://github.com/zanlucathiago/xls-reader/commit/7aa4e3c25f1623e2866915ef453fe2a87853b7fb) Thanks [@zanlucathiago](https://github.com/zanlucathiago)! - Export `cellToString(cell, { dateFormat })`, the per-cell text rendering that `sheetToCsv` already used, so importers that build their own rows don't have to reimplement it. `dateFormat: "compact"` writes a midnight date as `2024-04-02` and drops milliseconds otherwise; `sheetToCsv` accepts the same option. The default (`"iso"`) keeps today's output.
|
|
8
|
+
|
|
9
|
+
## 0.7.4
|
|
10
|
+
|
|
11
|
+
### Patch Changes
|
|
12
|
+
|
|
13
|
+
- [#66](https://github.com/zanlucathiago/xls-reader/pull/66) [`e84f053`](https://github.com/zanlucathiago/xls-reader/commit/e84f053ddd5de5d80516a00ab976a42b7ba9fd5a) Thanks [@zanlucathiago](https://github.com/zanlucathiago)! - Security: bound OLE2 sector chains and stream buffers by the size of the input. A crafted `.xls` could describe a sector chain far longer than the file and make the parser allocate about 1000× its size (hundreds of MB from a ~400 KB file), crashing the process. Chains now stop at the last sector present in the file, the FAT is capped the same way, and buffers are allocated at most at their declared size. See GHSA-29jh-wp5p-965w.
|
|
14
|
+
|
|
3
15
|
## 0.7.3
|
|
4
16
|
|
|
5
17
|
### Patch Changes
|
package/README.md
CHANGED
|
@@ -162,12 +162,11 @@ The first worksheet, for the single-sheet case.
|
|
|
162
162
|
Turns a sheet's rows into objects keyed by a header row (the first by default).
|
|
163
163
|
Blank-header columns are skipped and short rows are padded with `null`.
|
|
164
164
|
|
|
165
|
-
### `sheetToCsv(sheet: Sheet, options?: { delimiter?: string; eol?: string }): string`
|
|
165
|
+
### `sheetToCsv(sheet: Sheet, options?: { delimiter?: string; eol?: string; dateFormat?: "iso" | "compact" }): string`
|
|
166
166
|
|
|
167
|
-
Serializes a sheet to a CSV string with RFC-4180 quoting.
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
`\r\n` for Excel-style output).
|
|
167
|
+
Serializes a sheet to a CSV string with RFC-4180 quoting. Each cell is rendered
|
|
168
|
+
with `cellToString` (below), so `dateFormat` works the same way. `delimiter`
|
|
169
|
+
defaults to `,` and `eol` to `\n` (pass `\r\n` for Excel-style output).
|
|
171
170
|
|
|
172
171
|
```ts
|
|
173
172
|
import { readFirstSheet, sheetToCsv } from "xls-reader";
|
|
@@ -177,6 +176,22 @@ const csv = sheet ? sheetToCsv(sheet) : "";
|
|
|
177
176
|
// "Name,Age\nAda,36\nAlan,41"
|
|
178
177
|
```
|
|
179
178
|
|
|
179
|
+
### `cellToString(cell: Cell, options?: { dateFormat?: "iso" | "compact" }): string`
|
|
180
|
+
|
|
181
|
+
One cell as plain text, the same way `sheetToCsv` writes it: a blank cell becomes
|
|
182
|
+
`""`, a `CellError` becomes its code (e.g. `#DIV/0!`), and a `Date` becomes UTC
|
|
183
|
+
text. `dateFormat: "iso"` (the default) is `Date.toISOString()`; `"compact"`
|
|
184
|
+
gives `2024-04-02` for a date at UTC midnight and `2024-04-02T09:30:15Z`
|
|
185
|
+
otherwise.
|
|
186
|
+
|
|
187
|
+
```ts
|
|
188
|
+
import { cellToString, readFirstSheet } from "xls-reader";
|
|
189
|
+
|
|
190
|
+
const rows = readFirstSheet(bytes)?.rows ?? [];
|
|
191
|
+
const text = rows.map((row) => row.map((cell) => cellToString(cell, { dateFormat: "compact" })));
|
|
192
|
+
// [["Name", "Born"], ["Ada", "1815-12-10"]]
|
|
193
|
+
```
|
|
194
|
+
|
|
180
195
|
### `excelSerialToDate(serial: number, date1904: boolean): Date`
|
|
181
196
|
|
|
182
197
|
The raw serial-number → `Date` (UTC) conversion, if you need it directly.
|
package/README.pt-BR.md
CHANGED
|
@@ -169,12 +169,12 @@ Transforma as linhas de uma planilha em objetos com chaves vindas de uma linha
|
|
|
169
169
|
de cabeçalho (a primeira, por padrão). Colunas com cabeçalho vazio são ignoradas
|
|
170
170
|
e linhas curtas são preenchidas com `null`.
|
|
171
171
|
|
|
172
|
-
### `sheetToCsv(sheet: Sheet, options?: { delimiter?: string; eol?: string }): string`
|
|
172
|
+
### `sheetToCsv(sheet: Sheet, options?: { delimiter?: string; eol?: string; dateFormat?: "iso" | "compact" }): string`
|
|
173
173
|
|
|
174
|
-
Serializa uma planilha para uma string CSV com aspas no padrão RFC-4180.
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
174
|
+
Serializa uma planilha para uma string CSV com aspas no padrão RFC-4180. Cada
|
|
175
|
+
célula é convertida com `cellToString` (abaixo), então `dateFormat` funciona do
|
|
176
|
+
mesmo jeito. `delimiter` é `,` por padrão e `eol` é `\n` (use `\r\n` para o
|
|
177
|
+
formato do Excel).
|
|
178
178
|
|
|
179
179
|
```ts
|
|
180
180
|
import { readFirstSheet, sheetToCsv } from "xls-reader";
|
|
@@ -184,6 +184,22 @@ const csv = sheet ? sheetToCsv(sheet) : "";
|
|
|
184
184
|
// "Name,Age\nAda,36\nAlan,41"
|
|
185
185
|
```
|
|
186
186
|
|
|
187
|
+
### `cellToString(cell: Cell, options?: { dateFormat?: "iso" | "compact" }): string`
|
|
188
|
+
|
|
189
|
+
Uma célula como texto, do mesmo jeito que o `sheetToCsv` escreve: uma célula em
|
|
190
|
+
branco vira `""`, um `CellError` vira o seu código (ex.: `#DIV/0!`) e um `Date`
|
|
191
|
+
vira texto em UTC. `dateFormat: "iso"` (o padrão) é o `Date.toISOString()`;
|
|
192
|
+
`"compact"` dá `2024-04-02` para uma data à meia-noite UTC e
|
|
193
|
+
`2024-04-02T09:30:15Z` nos outros casos.
|
|
194
|
+
|
|
195
|
+
```ts
|
|
196
|
+
import { cellToString, readFirstSheet } from "xls-reader";
|
|
197
|
+
|
|
198
|
+
const rows = readFirstSheet(bytes)?.rows ?? [];
|
|
199
|
+
const text = rows.map((row) => row.map((cell) => cellToString(cell, { dateFormat: "compact" })));
|
|
200
|
+
// [["Name", "Born"], ["Ada", "1815-12-10"]]
|
|
201
|
+
```
|
|
202
|
+
|
|
187
203
|
### `excelSerialToDate(serial: number, date1904: boolean): Date`
|
|
188
204
|
|
|
189
205
|
A conversão bruta de número serial → `Date` (UTC), caso você precise dela
|
package/dist/cli.mjs
CHANGED
|
@@ -718,8 +718,20 @@ const FREESECT = 0xffffffff;
|
|
|
718
718
|
function sectorOffset(id, sectorSize) {
|
|
719
719
|
return (id + 1) * sectorSize;
|
|
720
720
|
}
|
|
721
|
+
/**
|
|
722
|
+
* How many whole-or-partial sectors follow the header in the file: valid sector
|
|
723
|
+
* ids are 0..count-1. Every chain and the FAT itself are capped by this, because
|
|
724
|
+
* the FAT is attacker-controlled and can describe sectors the file doesn't have
|
|
725
|
+
* — sizing buffers by those claims let a ~400 KB file allocate hundreds of MB
|
|
726
|
+
* (GHSA-29jh-wp5p-965w).
|
|
727
|
+
*
|
|
728
|
+
* @example fileSectorCount(bytes, 512) // 3 for a 2048-byte v3 file
|
|
729
|
+
*/
|
|
730
|
+
function fileSectorCount(bytes, sectorSize) {
|
|
731
|
+
return Math.max(0, Math.ceil(bytes.length / sectorSize) - 1);
|
|
732
|
+
}
|
|
721
733
|
function buildFat(bytes, header) {
|
|
722
|
-
const fatSectorIds = collectFatSectorIds(bytes, header);
|
|
734
|
+
const fatSectorIds = collectFatSectorIds(bytes, header).slice(0, fileSectorCount(bytes, header.sectorSize));
|
|
723
735
|
const fat = [];
|
|
724
736
|
const perSector = header.sectorSize / 4;
|
|
725
737
|
for (const id of fatSectorIds) {
|
|
@@ -744,38 +756,53 @@ function collectFatSectorIds(bytes, header) {
|
|
|
744
756
|
}
|
|
745
757
|
return ids;
|
|
746
758
|
}
|
|
747
|
-
function followChain(fat, start) {
|
|
759
|
+
function followChain(fat, start, sectorLimit) {
|
|
748
760
|
const chain = [];
|
|
749
761
|
const seen = /* @__PURE__ */ new Set();
|
|
762
|
+
const limit = Math.min(fat.length, sectorLimit);
|
|
750
763
|
let current = start;
|
|
751
|
-
while (current !== ENDOFCHAIN && current <
|
|
764
|
+
while (current !== ENDOFCHAIN && current < limit && !seen.has(current)) {
|
|
752
765
|
seen.add(current);
|
|
753
766
|
chain.push(current);
|
|
754
767
|
current = fat[current] ?? ENDOFCHAIN;
|
|
755
768
|
}
|
|
756
769
|
return chain;
|
|
757
770
|
}
|
|
758
|
-
|
|
759
|
-
|
|
771
|
+
/**
|
|
772
|
+
* Concatenates the bytes of a sector chain, up to `size`. Used for the
|
|
773
|
+
* directory, the mini-FAT and regular (non-mini) streams; also for mini streams
|
|
774
|
+
* with the mini-stream container as `bytes` and `firstSectorAt = 0`. Allocates
|
|
775
|
+
* at most `min(size, chain length × sectorSize)` — never the declared size alone
|
|
776
|
+
* (a u32 from the file) nor a full chain past `size`.
|
|
777
|
+
*
|
|
778
|
+
* @example readChainBytes(bytes, [3, 4], 512, 700) // 700 bytes from sectors 3, 4
|
|
779
|
+
*/
|
|
780
|
+
function readChainBytes(bytes, chain, sectorSize, size, firstSectorAt = sectorSize) {
|
|
781
|
+
const out = new Uint8Array(Math.max(0, Math.min(size, chain.length * sectorSize)));
|
|
760
782
|
chain.forEach((id, index) => {
|
|
761
|
-
const
|
|
762
|
-
|
|
783
|
+
const at = index * sectorSize;
|
|
784
|
+
if (at >= out.length) return;
|
|
785
|
+
const start = firstSectorAt + id * sectorSize;
|
|
786
|
+
out.set(bytes.subarray(start, start + Math.min(sectorSize, out.length - at)), at);
|
|
763
787
|
});
|
|
764
|
-
return out
|
|
788
|
+
return out;
|
|
765
789
|
}
|
|
766
790
|
|
|
767
791
|
// src/cfb/compound-file.ts
|
|
768
792
|
function openCompoundFile(bytes) {
|
|
769
793
|
const header = parseCfbHeader(bytes);
|
|
770
|
-
const
|
|
771
|
-
const directory = parseDirectory(directoryBytes(bytes, header, fat));
|
|
772
|
-
const file = {
|
|
794
|
+
const base = {
|
|
773
795
|
bytes,
|
|
774
796
|
header,
|
|
775
|
-
fat,
|
|
797
|
+
fat: buildFat(bytes, header),
|
|
798
|
+
sectorLimit: fileSectorCount(bytes, header.sectorSize)
|
|
799
|
+
};
|
|
800
|
+
const directory = parseDirectory(directoryBytes(base));
|
|
801
|
+
const file = {
|
|
802
|
+
...base,
|
|
776
803
|
directory,
|
|
777
|
-
miniStream: readMiniStreamContainer(
|
|
778
|
-
miniFat: buildMiniFat(
|
|
804
|
+
miniStream: readMiniStreamContainer(base, directory),
|
|
805
|
+
miniFat: buildMiniFat(base)
|
|
779
806
|
};
|
|
780
807
|
return { readStream(name) {
|
|
781
808
|
const entry = directory.find((e) => e.name === name);
|
|
@@ -783,33 +810,32 @@ function openCompoundFile(bytes) {
|
|
|
783
810
|
return entry.size < header.miniStreamCutoff ? readMiniStream(file, entry) : readRegularStream(file, entry);
|
|
784
811
|
} };
|
|
785
812
|
}
|
|
786
|
-
function
|
|
787
|
-
const chain = followChain(fat,
|
|
788
|
-
|
|
813
|
+
function wholeChainBytes(base, start) {
|
|
814
|
+
const chain = followChain(base.fat, start, base.sectorLimit);
|
|
815
|
+
const { sectorSize } = base.header;
|
|
816
|
+
return readChainBytes(base.bytes, chain, sectorSize, chain.length * sectorSize);
|
|
817
|
+
}
|
|
818
|
+
function directoryBytes(base) {
|
|
819
|
+
return wholeChainBytes(base, base.header.firstDirSector);
|
|
789
820
|
}
|
|
790
821
|
function readRegularStream(file, entry) {
|
|
791
|
-
const chain = followChain(file.fat, entry.startSector);
|
|
822
|
+
const chain = followChain(file.fat, entry.startSector, file.sectorLimit);
|
|
792
823
|
return readChainBytes(file.bytes, chain, file.header.sectorSize, entry.size);
|
|
793
824
|
}
|
|
794
|
-
function readMiniStreamContainer(
|
|
825
|
+
function readMiniStreamContainer(base, directory) {
|
|
795
826
|
const root = findRoot(directory);
|
|
796
827
|
if (!root) return /* @__PURE__ */ new Uint8Array(0);
|
|
797
|
-
const chain = followChain(fat, root.startSector);
|
|
798
|
-
return readChainBytes(bytes, chain, header.sectorSize, root.size);
|
|
828
|
+
const chain = followChain(base.fat, root.startSector, base.sectorLimit);
|
|
829
|
+
return readChainBytes(base.bytes, chain, base.header.sectorSize, root.size);
|
|
799
830
|
}
|
|
800
|
-
function buildMiniFat(
|
|
801
|
-
|
|
802
|
-
return toU32Array(readChainBytes(bytes, chain, header.sectorSize, chain.length * header.sectorSize));
|
|
831
|
+
function buildMiniFat(base) {
|
|
832
|
+
return toU32Array(wholeChainBytes(base, base.header.firstMiniFatSector));
|
|
803
833
|
}
|
|
804
834
|
function readMiniStream(file, entry) {
|
|
805
835
|
const { miniStream, miniFat, header } = file;
|
|
806
|
-
const
|
|
807
|
-
const
|
|
808
|
-
chain.
|
|
809
|
-
const start = id * header.miniSectorSize;
|
|
810
|
-
out.set(miniStream.subarray(start, start + header.miniSectorSize), index * header.miniSectorSize);
|
|
811
|
-
});
|
|
812
|
-
return out.subarray(0, entry.size);
|
|
836
|
+
const miniSize = header.miniSectorSize;
|
|
837
|
+
const chain = followChain(miniFat, entry.startSector, Math.ceil(miniStream.length / miniSize));
|
|
838
|
+
return readChainBytes(miniStream, chain, miniSize, entry.size, 0);
|
|
813
839
|
}
|
|
814
840
|
function toU32Array(raw) {
|
|
815
841
|
const reader = new ByteReader(raw);
|
|
@@ -913,21 +939,42 @@ Examples:
|
|
|
913
939
|
npx xls-reader report.xls --visible-only > report.json
|
|
914
940
|
`;
|
|
915
941
|
|
|
942
|
+
// src/cell-to-string.ts
|
|
943
|
+
/**
|
|
944
|
+
* Renders one cell as plain text: blank → `""`, `Date` → UTC text in the chosen
|
|
945
|
+
* `dateFormat`, `CellError` → its code (e.g. `#DIV/0!`), anything else via its
|
|
946
|
+
* JS string form. This is the exact rendering `sheetToCsv` uses per field, so
|
|
947
|
+
* importers that build their own rows don't have to re-derive it.
|
|
948
|
+
*
|
|
949
|
+
* @example
|
|
950
|
+
* cellToString(new Date(Date.UTC(2024, 3, 2)), { dateFormat: "compact" }) // "2024-04-02"
|
|
951
|
+
*/
|
|
952
|
+
function cellToString(cell, options = {}) {
|
|
953
|
+
if (cell === null) return "";
|
|
954
|
+
if (cell instanceof Date) return dateToText(cell, options.dateFormat ?? "iso");
|
|
955
|
+
if (cell instanceof CellError) return cell.code;
|
|
956
|
+
return String(cell);
|
|
957
|
+
}
|
|
958
|
+
function dateToText(date, format) {
|
|
959
|
+
if (format === "iso") return date.toISOString();
|
|
960
|
+
if (format === "compact") return compactDate(date);
|
|
961
|
+
throw new XlsError(`dateFormat must be "iso" or "compact", got ${JSON.stringify(format)}`);
|
|
962
|
+
}
|
|
963
|
+
function compactDate(date) {
|
|
964
|
+
const iso = date.toISOString();
|
|
965
|
+
return iso.endsWith("T00:00:00.000Z") ? iso.slice(0, 10) : `${iso.slice(0, 19)}Z`;
|
|
966
|
+
}
|
|
967
|
+
|
|
916
968
|
// src/sheet-to-csv.ts
|
|
917
969
|
function sheetToCsv(sheet, options = {}) {
|
|
918
970
|
const delimiter = options.delimiter ?? ",";
|
|
919
971
|
const eol = options.eol ?? "\n";
|
|
972
|
+
const dateFormat = options.dateFormat ?? "iso";
|
|
920
973
|
assertDelimiter(delimiter);
|
|
921
|
-
return sheet.rows.map((row) => encodeRow(row, delimiter)).join(eol);
|
|
922
|
-
}
|
|
923
|
-
function encodeRow(row, delimiter) {
|
|
924
|
-
return row.map((cell) => encodeField(cellToText(cell), delimiter)).join(delimiter);
|
|
974
|
+
return sheet.rows.map((row) => encodeRow(row, delimiter, dateFormat)).join(eol);
|
|
925
975
|
}
|
|
926
|
-
function
|
|
927
|
-
|
|
928
|
-
if (cell instanceof Date) return cell.toISOString();
|
|
929
|
-
if (cell instanceof CellError) return cell.code;
|
|
930
|
-
return String(cell);
|
|
976
|
+
function encodeRow(row, delimiter, dateFormat) {
|
|
977
|
+
return row.map((cell) => encodeField(cellToString(cell, { dateFormat }), delimiter)).join(delimiter);
|
|
931
978
|
}
|
|
932
979
|
function encodeField(text, delimiter) {
|
|
933
980
|
if (!text.includes(delimiter) && !/["\r\n]/.test(text)) return text;
|
package/dist/index.cjs
CHANGED
|
@@ -715,8 +715,20 @@ const FREESECT = 0xffffffff;
|
|
|
715
715
|
function sectorOffset(id, sectorSize) {
|
|
716
716
|
return (id + 1) * sectorSize;
|
|
717
717
|
}
|
|
718
|
+
/**
|
|
719
|
+
* How many whole-or-partial sectors follow the header in the file: valid sector
|
|
720
|
+
* ids are 0..count-1. Every chain and the FAT itself are capped by this, because
|
|
721
|
+
* the FAT is attacker-controlled and can describe sectors the file doesn't have
|
|
722
|
+
* — sizing buffers by those claims let a ~400 KB file allocate hundreds of MB
|
|
723
|
+
* (GHSA-29jh-wp5p-965w).
|
|
724
|
+
*
|
|
725
|
+
* @example fileSectorCount(bytes, 512) // 3 for a 2048-byte v3 file
|
|
726
|
+
*/
|
|
727
|
+
function fileSectorCount(bytes, sectorSize) {
|
|
728
|
+
return Math.max(0, Math.ceil(bytes.length / sectorSize) - 1);
|
|
729
|
+
}
|
|
718
730
|
function buildFat(bytes, header) {
|
|
719
|
-
const fatSectorIds = collectFatSectorIds(bytes, header);
|
|
731
|
+
const fatSectorIds = collectFatSectorIds(bytes, header).slice(0, fileSectorCount(bytes, header.sectorSize));
|
|
720
732
|
const fat = [];
|
|
721
733
|
const perSector = header.sectorSize / 4;
|
|
722
734
|
for (const id of fatSectorIds) {
|
|
@@ -741,38 +753,53 @@ function collectFatSectorIds(bytes, header) {
|
|
|
741
753
|
}
|
|
742
754
|
return ids;
|
|
743
755
|
}
|
|
744
|
-
function followChain(fat, start) {
|
|
756
|
+
function followChain(fat, start, sectorLimit) {
|
|
745
757
|
const chain = [];
|
|
746
758
|
const seen = /* @__PURE__ */ new Set();
|
|
759
|
+
const limit = Math.min(fat.length, sectorLimit);
|
|
747
760
|
let current = start;
|
|
748
|
-
while (current !== ENDOFCHAIN && current <
|
|
761
|
+
while (current !== ENDOFCHAIN && current < limit && !seen.has(current)) {
|
|
749
762
|
seen.add(current);
|
|
750
763
|
chain.push(current);
|
|
751
764
|
current = fat[current] ?? ENDOFCHAIN;
|
|
752
765
|
}
|
|
753
766
|
return chain;
|
|
754
767
|
}
|
|
755
|
-
|
|
756
|
-
|
|
768
|
+
/**
|
|
769
|
+
* Concatenates the bytes of a sector chain, up to `size`. Used for the
|
|
770
|
+
* directory, the mini-FAT and regular (non-mini) streams; also for mini streams
|
|
771
|
+
* with the mini-stream container as `bytes` and `firstSectorAt = 0`. Allocates
|
|
772
|
+
* at most `min(size, chain length × sectorSize)` — never the declared size alone
|
|
773
|
+
* (a u32 from the file) nor a full chain past `size`.
|
|
774
|
+
*
|
|
775
|
+
* @example readChainBytes(bytes, [3, 4], 512, 700) // 700 bytes from sectors 3, 4
|
|
776
|
+
*/
|
|
777
|
+
function readChainBytes(bytes, chain, sectorSize, size, firstSectorAt = sectorSize) {
|
|
778
|
+
const out = new Uint8Array(Math.max(0, Math.min(size, chain.length * sectorSize)));
|
|
757
779
|
chain.forEach((id, index) => {
|
|
758
|
-
const
|
|
759
|
-
|
|
780
|
+
const at = index * sectorSize;
|
|
781
|
+
if (at >= out.length) return;
|
|
782
|
+
const start = firstSectorAt + id * sectorSize;
|
|
783
|
+
out.set(bytes.subarray(start, start + Math.min(sectorSize, out.length - at)), at);
|
|
760
784
|
});
|
|
761
|
-
return out
|
|
785
|
+
return out;
|
|
762
786
|
}
|
|
763
787
|
|
|
764
788
|
// src/cfb/compound-file.ts
|
|
765
789
|
function openCompoundFile(bytes) {
|
|
766
790
|
const header = parseCfbHeader(bytes);
|
|
767
|
-
const
|
|
768
|
-
const directory = parseDirectory(directoryBytes(bytes, header, fat));
|
|
769
|
-
const file = {
|
|
791
|
+
const base = {
|
|
770
792
|
bytes,
|
|
771
793
|
header,
|
|
772
|
-
fat,
|
|
794
|
+
fat: buildFat(bytes, header),
|
|
795
|
+
sectorLimit: fileSectorCount(bytes, header.sectorSize)
|
|
796
|
+
};
|
|
797
|
+
const directory = parseDirectory(directoryBytes(base));
|
|
798
|
+
const file = {
|
|
799
|
+
...base,
|
|
773
800
|
directory,
|
|
774
|
-
miniStream: readMiniStreamContainer(
|
|
775
|
-
miniFat: buildMiniFat(
|
|
801
|
+
miniStream: readMiniStreamContainer(base, directory),
|
|
802
|
+
miniFat: buildMiniFat(base)
|
|
776
803
|
};
|
|
777
804
|
return { readStream(name) {
|
|
778
805
|
const entry = directory.find((e) => e.name === name);
|
|
@@ -780,33 +807,32 @@ function openCompoundFile(bytes) {
|
|
|
780
807
|
return entry.size < header.miniStreamCutoff ? readMiniStream(file, entry) : readRegularStream(file, entry);
|
|
781
808
|
} };
|
|
782
809
|
}
|
|
783
|
-
function
|
|
784
|
-
const chain = followChain(fat,
|
|
785
|
-
|
|
810
|
+
function wholeChainBytes(base, start) {
|
|
811
|
+
const chain = followChain(base.fat, start, base.sectorLimit);
|
|
812
|
+
const { sectorSize } = base.header;
|
|
813
|
+
return readChainBytes(base.bytes, chain, sectorSize, chain.length * sectorSize);
|
|
814
|
+
}
|
|
815
|
+
function directoryBytes(base) {
|
|
816
|
+
return wholeChainBytes(base, base.header.firstDirSector);
|
|
786
817
|
}
|
|
787
818
|
function readRegularStream(file, entry) {
|
|
788
|
-
const chain = followChain(file.fat, entry.startSector);
|
|
819
|
+
const chain = followChain(file.fat, entry.startSector, file.sectorLimit);
|
|
789
820
|
return readChainBytes(file.bytes, chain, file.header.sectorSize, entry.size);
|
|
790
821
|
}
|
|
791
|
-
function readMiniStreamContainer(
|
|
822
|
+
function readMiniStreamContainer(base, directory) {
|
|
792
823
|
const root = findRoot(directory);
|
|
793
824
|
if (!root) return /* @__PURE__ */ new Uint8Array(0);
|
|
794
|
-
const chain = followChain(fat, root.startSector);
|
|
795
|
-
return readChainBytes(bytes, chain, header.sectorSize, root.size);
|
|
825
|
+
const chain = followChain(base.fat, root.startSector, base.sectorLimit);
|
|
826
|
+
return readChainBytes(base.bytes, chain, base.header.sectorSize, root.size);
|
|
796
827
|
}
|
|
797
|
-
function buildMiniFat(
|
|
798
|
-
|
|
799
|
-
return toU32Array(readChainBytes(bytes, chain, header.sectorSize, chain.length * header.sectorSize));
|
|
828
|
+
function buildMiniFat(base) {
|
|
829
|
+
return toU32Array(wholeChainBytes(base, base.header.firstMiniFatSector));
|
|
800
830
|
}
|
|
801
831
|
function readMiniStream(file, entry) {
|
|
802
832
|
const { miniStream, miniFat, header } = file;
|
|
803
|
-
const
|
|
804
|
-
const
|
|
805
|
-
chain.
|
|
806
|
-
const start = id * header.miniSectorSize;
|
|
807
|
-
out.set(miniStream.subarray(start, start + header.miniSectorSize), index * header.miniSectorSize);
|
|
808
|
-
});
|
|
809
|
-
return out.subarray(0, entry.size);
|
|
833
|
+
const miniSize = header.miniSectorSize;
|
|
834
|
+
const chain = followChain(miniFat, entry.startSector, Math.ceil(miniStream.length / miniSize));
|
|
835
|
+
return readChainBytes(miniStream, chain, miniSize, entry.size, 0);
|
|
810
836
|
}
|
|
811
837
|
function toU32Array(raw) {
|
|
812
838
|
const reader = new ByteReader(raw);
|
|
@@ -860,21 +886,42 @@ function assertHeaderRow(headerRow) {
|
|
|
860
886
|
throw new XlsError(`headerRow must be a non-negative integer, got ${JSON.stringify(headerRow)}`);
|
|
861
887
|
}
|
|
862
888
|
|
|
889
|
+
// src/cell-to-string.ts
|
|
890
|
+
/**
|
|
891
|
+
* Renders one cell as plain text: blank → `""`, `Date` → UTC text in the chosen
|
|
892
|
+
* `dateFormat`, `CellError` → its code (e.g. `#DIV/0!`), anything else via its
|
|
893
|
+
* JS string form. This is the exact rendering `sheetToCsv` uses per field, so
|
|
894
|
+
* importers that build their own rows don't have to re-derive it.
|
|
895
|
+
*
|
|
896
|
+
* @example
|
|
897
|
+
* cellToString(new Date(Date.UTC(2024, 3, 2)), { dateFormat: "compact" }) // "2024-04-02"
|
|
898
|
+
*/
|
|
899
|
+
function cellToString(cell, options = {}) {
|
|
900
|
+
if (cell === null) return "";
|
|
901
|
+
if (cell instanceof Date) return dateToText(cell, options.dateFormat ?? "iso");
|
|
902
|
+
if (cell instanceof CellError) return cell.code;
|
|
903
|
+
return String(cell);
|
|
904
|
+
}
|
|
905
|
+
function dateToText(date, format) {
|
|
906
|
+
if (format === "iso") return date.toISOString();
|
|
907
|
+
if (format === "compact") return compactDate(date);
|
|
908
|
+
throw new XlsError(`dateFormat must be "iso" or "compact", got ${JSON.stringify(format)}`);
|
|
909
|
+
}
|
|
910
|
+
function compactDate(date) {
|
|
911
|
+
const iso = date.toISOString();
|
|
912
|
+
return iso.endsWith("T00:00:00.000Z") ? iso.slice(0, 10) : `${iso.slice(0, 19)}Z`;
|
|
913
|
+
}
|
|
914
|
+
|
|
863
915
|
// src/sheet-to-csv.ts
|
|
864
916
|
function sheetToCsv(sheet, options = {}) {
|
|
865
917
|
const delimiter = options.delimiter ?? ",";
|
|
866
918
|
const eol = options.eol ?? "\n";
|
|
919
|
+
const dateFormat = options.dateFormat ?? "iso";
|
|
867
920
|
assertDelimiter(delimiter);
|
|
868
|
-
return sheet.rows.map((row) => encodeRow(row, delimiter)).join(eol);
|
|
869
|
-
}
|
|
870
|
-
function encodeRow(row, delimiter) {
|
|
871
|
-
return row.map((cell) => encodeField(cellToText(cell), delimiter)).join(delimiter);
|
|
921
|
+
return sheet.rows.map((row) => encodeRow(row, delimiter, dateFormat)).join(eol);
|
|
872
922
|
}
|
|
873
|
-
function
|
|
874
|
-
|
|
875
|
-
if (cell instanceof Date) return cell.toISOString();
|
|
876
|
-
if (cell instanceof CellError) return cell.code;
|
|
877
|
-
return String(cell);
|
|
923
|
+
function encodeRow(row, delimiter, dateFormat) {
|
|
924
|
+
return row.map((cell) => encodeField(cellToString(cell, { dateFormat }), delimiter)).join(delimiter);
|
|
878
925
|
}
|
|
879
926
|
function encodeField(text, delimiter) {
|
|
880
927
|
if (!text.includes(delimiter) && !/["\r\n]/.test(text)) return text;
|
|
@@ -887,6 +934,7 @@ function assertDelimiter(delimiter) {
|
|
|
887
934
|
|
|
888
935
|
exports.CellError = CellError;
|
|
889
936
|
exports.XlsError = XlsError;
|
|
937
|
+
exports.cellToString = cellToString;
|
|
890
938
|
exports.excelSerialToDate = excelSerialToDate;
|
|
891
939
|
exports.readFirstSheet = readFirstSheet;
|
|
892
940
|
exports.readXls = readXls;
|