xtos throws TypeError when first rows have no columns #2656

Closed
opened 2022-04-13 21:39:29 +00:00 by M15terHyde · 3 comments
M15terHyde commented 2022-04-13 21:39:29 +00:00 (Migrated from github.com)

In file sheetjs/demos/xspreadsheet/xlsxspread.js function xtos line 137 will throw an error on minCoord.r when the spreadsheet passed in has the first couple rows empty (no columns) and below these empty rows has real data.
This is caused by xtos line 81 setting minCoord and maxCoord so their initial value is undefined then line 82 runs a forEach loop on all the columns in that row. The values of minCoord and maxCoord should be set in the forEach loop. However, if there are no columns in that row minCoord and maxCoord's values are never set and so calling minCoord.r on line 137 fails and produces an error.

In file sheetjs/demos/xspreadsheet/xlsxspread.js function xtos line 137 will throw an error on minCoord.r when the spreadsheet passed in has the first couple rows empty (no columns) and below these empty rows has real data. This is caused by xtos line 81 setting minCoord and maxCoord so their initial value is undefined then line 82 runs a forEach loop on all the columns in that row. The values of minCoord and maxCoord should be set in the forEach loop. However, if there are no columns in that row minCoord and maxCoord's values are never set and so calling minCoord.r on line 137 fails and produces an error.
SheetJSDev commented 2022-04-15 21:25:21 +00:00 (Migrated from github.com)

This can be simplified by anchoring the starting point to A1. If it works please feel free to send a PR

@@ -74,28 +74,18 @@ function xtos(sdata) {
   sdata.forEach(function (xws) {
     var ws = {};
     var rowobj = xws.rows;
+    var minCoord = { r: 0, c: 0 }, maxCoord = { r: 0, c: 0 };
     for (var ri = 0; ri < rowobj.len; ++ri) {
       var row = rowobj[ri];
       if (!row) continue;
 
-      var minCoord, maxCoord;
       Object.keys(row.cells).forEach(function (k) {
         var idx = +k;
         if (isNaN(idx)) return;
 
         var lastRef = XLSX.utils.encode_cell({ r: ri, c: idx });
-        if (minCoord == null) {
-          minCoord = { r: ri, c: idx };
-        } else {
-          if (ri < minCoord.r) minCoord.r = ri;
-          if (idx < minCoord.c) minCoord.c = idx;
-        }
-        if (maxCoord == undefined) {
-          maxCoord = { r: ri, c: idx };
-        } else {
-          if (ri > maxCoord.r) maxCoord.r = ri;
-          if (idx > maxCoord.c) maxCoord.c = idx;
-        }
+        if (ri > maxCoord.r) maxCoord.r = ri;
+        if (idx > maxCoord.c) maxCoord.c = idx;
 
         var cellText = row.cells[k].text, type = "s";
         if (!cellText) {
@@ -127,12 +117,11 @@ function xtos(sdata) {
           });
         }
       });
-
-      ws["!ref"] = XLSX.utils.encode_range({
-        s: { r: minCoord.r, c: minCoord.c },
-        e: { r: maxCoord.r, c: maxCoord.c }
-      });
     }
+    ws["!ref"] = minCoord ? XLSX.utils.encode_range({
+      s: minCoord,
+      e: maxCoord
+    }) : "A1";
 
     XLSX.utils.book_append_sheet(out, ws, xws.name);
   });
This can be simplified by anchoring the starting point to A1. If it works please feel free to send a PR ```diff @@ -74,28 +74,18 @@ function xtos(sdata) { sdata.forEach(function (xws) { var ws = {}; var rowobj = xws.rows; + var minCoord = { r: 0, c: 0 }, maxCoord = { r: 0, c: 0 }; for (var ri = 0; ri < rowobj.len; ++ri) { var row = rowobj[ri]; if (!row) continue; - var minCoord, maxCoord; Object.keys(row.cells).forEach(function (k) { var idx = +k; if (isNaN(idx)) return; var lastRef = XLSX.utils.encode_cell({ r: ri, c: idx }); - if (minCoord == null) { - minCoord = { r: ri, c: idx }; - } else { - if (ri < minCoord.r) minCoord.r = ri; - if (idx < minCoord.c) minCoord.c = idx; - } - if (maxCoord == undefined) { - maxCoord = { r: ri, c: idx }; - } else { - if (ri > maxCoord.r) maxCoord.r = ri; - if (idx > maxCoord.c) maxCoord.c = idx; - } + if (ri > maxCoord.r) maxCoord.r = ri; + if (idx > maxCoord.c) maxCoord.c = idx; var cellText = row.cells[k].text, type = "s"; if (!cellText) { @@ -127,12 +117,11 @@ function xtos(sdata) { }); } }); - - ws["!ref"] = XLSX.utils.encode_range({ - s: { r: minCoord.r, c: minCoord.c }, - e: { r: maxCoord.r, c: maxCoord.c } - }); } + ws["!ref"] = minCoord ? XLSX.utils.encode_range({ + s: minCoord, + e: maxCoord + }) : "A1"; XLSX.utils.book_append_sheet(out, ws, xws.name); }); ```
M15terHyde commented 2022-04-15 22:50:38 +00:00 (Migrated from github.com)

This is approximately what I did to fix it on my local machine but I can't submit a PR because of my employer's policy on outside work. I would have to get it approved first.

This is approximately what I did to fix it on my local machine but I can't submit a PR because of my employer's policy on outside work. I would have to get it approved first.
SheetJSDev commented 2022-04-15 23:19:45 +00:00 (Migrated from github.com)
Updated the public demo https://oss.sheetjs.com/sheetjs/x-spreadsheet.html
Sign in to join this conversation.
No Milestone
No Assignees
1 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#2656
No description provided.