Skip to content

Commit

Permalink
fix: error when sheet doesnt contain anything
Browse files Browse the repository at this point in the history
  • Loading branch information
iyxan23 committed Oct 23, 2024
1 parent a1ffba8 commit 8182878
Showing 1 changed file with 37 additions and 13 deletions.
50 changes: 37 additions & 13 deletions src/xlsx/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,30 @@ class XlsxTemplater {
): Promise<any> {
const rows: Record<number, { c: any[] | any } & any> = {};

const { rowBound, colBound } = sheetData.getBounds();
const { rowBound: sheetRowBound, colBound: sheetColBound } =
sheetData.getBounds();

const rowInfoMax = Object.keys(rowInfo).reduce<number | null>(
(a, b) => Math.max(a ?? 0, parseInt(b ?? "0")),
null,
);
const colInfoMax = Object.keys(colInfo).reduce<number | null>(
(a, b) => Math.max(a ?? 0, parseInt(b ?? "0")),
null,
);

const rowBound = rowInfoMax
? sheetRowBound < rowInfoMax
? rowInfoMax
: sheetRowBound
: sheetRowBound;

const colBound = colInfoMax
? sheetColBound < colInfoMax
? colInfoMax
: sheetColBound
: sheetColBound;

sheetData.optimizeSheet({ rowBound, colBound });

for (let r = 0; r <= rowBound; r++) {
Expand All @@ -342,10 +365,9 @@ class XlsxTemplater {
after: {
dimension: [
() => {
const bounds = sheetData.getBounds();
const dimension = createAddressNumber(
bounds.colBound,
bounds.rowBound,
Math.max(colBound, 0),
Math.max(rowBound, 0),
);

return {
Expand Down Expand Up @@ -508,10 +530,10 @@ class XlsxTemplater {
newObj:
obj["@_t"] === "s" && isNumeric(obj["v"])
? {
...obj,
v: this.sharedStrings[parseInt(obj["v"])],
"@_t": "str",
}
...obj,
v: this.sharedStrings[parseInt(obj["v"])],
"@_t": "str",
}
: { ...obj },
};
}
Expand All @@ -522,10 +544,10 @@ class XlsxTemplater {
newObj: obj.map((o) =>
o["@_t"] === "s" && isNumeric(o["v"])
? {
...o,
v: this.sharedStrings[parseInt(o["v"])],
"@_t": "str",
}
...o,
v: this.sharedStrings[parseInt(o["v"])],
"@_t": "str",
}
: { ...o },
),
};
Expand Down Expand Up @@ -557,7 +579,9 @@ function parseAddressNumber(address: string): { row: number; col: number } {

function createAddressNumber(col: number, row: number): `${string}${number}` {
if (col < 0 || row < 0) {
throw new Error("Column and row must be non-negative.");
throw new Error(
`Column and row must be non-negative; col: ${col}, row: ${row}`,
);
}

const oneIndexedRow = row + 1;
Expand Down

0 comments on commit 8182878

Please sign in to comment.