Writing dense xlsx does not work "undefined is not an object (evaluating 'data_R[C]')" #3261

Open
opened 2024-11-28 14:14:27 +00:00 by mbornstein · 1 comment

Hey,

using bun this example file can be read but not be written

// extract_tables.ts

import * as fs from "fs"
import * as xlsx from "xlsx"

xlsx.set_fs(fs)

const file_path =
  "/[...]/example.xlsx"

const workbook = xlsx.readFile(file_path, {
  cellDates: true,
  dense: true,
  cellNF: true,
})

xlsx.writeFile(workbook, "/[...]/example_out.xlsx")

bun extract_tables.ts

fails with

16381 |                 r = [];
16382 |                 rr = encode_row(R);
16383 |                 var data_R = dense ? data[R] : [];
16384 |                 for(C = range.s.c; C <= range.e.c; ++C) {
16385 |                         ref = cols[C] + rr;
16386 |                         var _cell = dense ? data_R[C] : ws[ref];
                               ^
TypeError: undefined is not an object (evaluating 'data_R[C]')
      at write_ws_xml_data (/[...]/node_modules/xlsx/xlsx.mjs:16386:24)
      at write_ws_xml (/[...]/node_modules/xlsx/xlsx.mjs:16458:11)
      at write_zip_xlsx (/[...]/node_modules/xlsx/xlsx.mjs:27056:25)
      at write_zip_type (/[...]/node_modules/xlsx/xlsx.mjs:27301:10)
      at /[...]/extract_tables.ts:16:6

bun version: 1.1.34
xlsx: "https://cdn.sheetjs.com/xlsx-0.20.3/xlsx-0.20.3.tgz",

Hey, using `bun` this example file can be read but not be written ``` // extract_tables.ts import * as fs from "fs" import * as xlsx from "xlsx" xlsx.set_fs(fs) const file_path = "/[...]/example.xlsx" const workbook = xlsx.readFile(file_path, { cellDates: true, dense: true, cellNF: true, }) xlsx.writeFile(workbook, "/[...]/example_out.xlsx") ``` `bun extract_tables.ts` fails with ``` 16381 | r = []; 16382 | rr = encode_row(R); 16383 | var data_R = dense ? data[R] : []; 16384 | for(C = range.s.c; C <= range.e.c; ++C) { 16385 | ref = cols[C] + rr; 16386 | var _cell = dense ? data_R[C] : ws[ref]; ^ TypeError: undefined is not an object (evaluating 'data_R[C]') at write_ws_xml_data (/[...]/node_modules/xlsx/xlsx.mjs:16386:24) at write_ws_xml (/[...]/node_modules/xlsx/xlsx.mjs:16458:11) at write_zip_xlsx (/[...]/node_modules/xlsx/xlsx.mjs:27056:25) at write_zip_type (/[...]/node_modules/xlsx/xlsx.mjs:27301:10) at /[...]/extract_tables.ts:16:6 ``` --- bun version: 1.1.34 xlsx: "https://cdn.sheetjs.com/xlsx-0.20.3/xlsx-0.20.3.tgz",
Owner

Thanks for reporting! There is a missing null check:

diff --git a/bits/67_wsxml.js b/bits/67_wsxml.js
index 7c78e98..46a69a1 100644
--- a/bits/67_wsxml.js
+++ b/bits/67_wsxml.js
@@ -529,7 +529,7 @@ function write_ws_xml_data(ws/*:Worksheet*/, opts, idx/*:number*/, wb/*:Workbook
                r = [];
                rr = encode_row(R);
                var data_R = dense ? data[R] : [];
-               for(C = range.s.c; C <= range.e.c; ++C) {
+               if(data_R) for(C = range.s.c; C <= range.e.c; ++C) {
                        ref = cols[C] + rr;
                        var _cell = dense ? data_R[C] : ws[ref];
                        if(_cell === undefined) continue;
Thanks for reporting! There is a missing `null` check: ```patch diff --git a/bits/67_wsxml.js b/bits/67_wsxml.js index 7c78e98..46a69a1 100644 --- a/bits/67_wsxml.js +++ b/bits/67_wsxml.js @@ -529,7 +529,7 @@ function write_ws_xml_data(ws/*:Worksheet*/, opts, idx/*:number*/, wb/*:Workbook r = []; rr = encode_row(R); var data_R = dense ? data[R] : []; - for(C = range.s.c; C <= range.e.c; ++C) { + if(data_R) for(C = range.s.c; C <= range.e.c; ++C) { ref = cols[C] + rr; var _cell = dense ? data_R[C] : ws[ref]; if(_cell === undefined) continue; ```
Sign in to join this conversation.
No Milestone
No Assignees
2 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: sheetjs/sheetjs#3261
No description provided.