From ad0fb7766b3e484e695db5585bf56bbcb5bae714 Mon Sep 17 00:00:00 2001 From: SheetJS Date: Tue, 17 Oct 2023 04:53:00 -0400 Subject: [PATCH] import and export blank worksheets --- bits/40_harb.js | 13 ++++++++----- bits/41_lotus.js | 3 ++- bits/79_html.js | 4 ++-- bits/81_writeods.js | 1 + bits/83_numbers.js | 2 ++ modules/83_numbers.js | 2 ++ modules/83_numbers.ts | 1 + 7 files changed, 18 insertions(+), 8 deletions(-) diff --git a/bits/40_harb.js b/bits/40_harb.js index 8e55348..84fef48 100644 --- a/bits/40_harb.js +++ b/bits/40_harb.js @@ -190,7 +190,7 @@ function dbf_to_aoa(buf, opts)/*:AOA*/ { case 'L': switch(s.trim().toUpperCase()) { case 'Y': case 'T': out[R][C] = true; break; case 'N': case 'F': out[R][C] = false; break; - case '': case '?': break; + case '': case '\x00': case '?': break; default: throw new Error("DBF Unrecognized L:|" + s + "|"); } break; case 'M': /* TODO: handle memo files */ @@ -252,6 +252,7 @@ function dbf_to_workbook(buf, opts)/*:Workbook*/ { var _RLEN = { 'B': 8, 'C': 250, 'L': 1, 'D': 8, '?': 0, '': 0 }; function sheet_to_dbf(ws/*:Worksheet*/, opts/*:WriteOpts*/) { + if(!ws["!ref"]) throw new Error("Cannot export empty sheet to DBF"); var o = opts || {}; var old_cp = current_codepage; if(+o.codepage >= 0) set_cp(+o.codepage); @@ -635,7 +636,7 @@ var SYLK = /*#__PURE__*/(function() { if(!opts) opts = {}; opts._formats = ["General"]; /* TODO: codepage */ var preamble/*:Array*/ = ["ID;PSheetJS;N;E"], o/*:Array*/ = []; - var r = safe_decode_range(ws['!ref']), cell/*:Cell*/; + var r = safe_decode_range(ws['!ref']||"A1"), cell/*:Cell*/; var dense = ws["!data"] != null; var RS = "\r\n"; var d1904 = (((wb||{}).Workbook||{}).WBProps||{}).date1904; @@ -644,7 +645,7 @@ var SYLK = /*#__PURE__*/(function() { preamble.push("P;PGeneral"); /* Excel has been inconsistent in comment placement */ var R = r.s.r, C = r.s.c, p = []; - for(R = r.s.r; R <= r.e.r; ++R) { + if(ws["!ref"]) for(R = r.s.r; R <= r.e.r; ++R) { if(dense && !ws["!data"][R]) continue; p = []; for(C = r.s.c; C <= r.e.c; ++C) { @@ -654,7 +655,7 @@ var SYLK = /*#__PURE__*/(function() { } if(p.length) o.push(p.join(RS)); } - for(R = r.s.r; R <= r.e.r; ++R) { + if(ws["!ref"]) for(R = r.s.r; R <= r.e.r; ++R) { if(dense && !ws["!data"][R]) continue; p = []; for(C = r.s.c; C <= r.e.c; ++C) { @@ -674,7 +675,7 @@ var SYLK = /*#__PURE__*/(function() { if(ws['!cols']) write_ws_cols_sylk(preamble, ws['!cols']); if(ws['!rows']) write_ws_rows_sylk(preamble, ws['!rows']); - preamble.push("B;Y" + (r.e.r - r.s.r + 1) + ";X" + (r.e.c - r.s.c + 1) + ";D" + [r.s.c,r.s.r,r.e.c,r.e.r].join(" ")); + if(ws["!ref"]) preamble.push("B;Y" + (r.e.r - r.s.r + 1) + ";X" + (r.e.c - r.s.c + 1) + ";D" + [r.s.c,r.s.r,r.e.c,r.e.r].join(" ")); preamble.push("O;L;D;B" + (d1904 ? ";V4" : "") + ";K47;G100 0.001"); delete opts._formats; @@ -747,6 +748,7 @@ var DIF = /*#__PURE__*/(function() { function make_value_str(s/*:string*/)/*:string*/ { return "1,0\r\n\"" + s.replace(/"/g,'""') + '"'; } function sheet_to_dif(ws/*:Worksheet*//*::, opts:?any*/)/*:string*/ { var _DIF_XL = DIF_XL; + if(!ws["!ref"]) throw new Error("Cannot export empty sheet to DIF"); var r = safe_decode_range(ws['!ref']); var dense = ws["!data"] != null; var o/*:Array*/ = [ @@ -1084,6 +1086,7 @@ var PRN = /*#__PURE__*/(function() { function sheet_to_prn(ws/*:Worksheet*//*::, opts:?any*/)/*:string*/ { var o/*:Array*/ = []; + if(!ws["!ref"]) return ""; var r = safe_decode_range(ws['!ref']), cell/*:Cell*/; var dense = ws["!data"] != null; for(var R = r.s.r; R <= r.e.r; ++R) { diff --git a/bits/41_lotus.js b/bits/41_lotus.js index f39c464..fdbea35 100644 --- a/bits/41_lotus.js +++ b/bits/41_lotus.js @@ -184,6 +184,7 @@ var WK_ = /*#__PURE__*/(function() { if(+o.codepage >= 0) set_cp(+o.codepage); if(o.type == "string") throw new Error("Cannot write WK1 to JS string"); var ba = buf_array(); + if(!ws["!ref"]) throw new Error("Cannot export empty sheet to WK1"); var range = safe_decode_range(ws["!ref"]); var dense = ws["!data"] != null; var cols = []; @@ -977,7 +978,7 @@ var WK_ = /*#__PURE__*/(function() { /*::[*/0x0E/*::*/: "dd-mmm-yyyy", /*::[*/0x0F/*::*/: "mmm-yyyy", - /* It is suspected that the the low nybble specifies decimal places + /* It is suspected that the the low nybble specifies decimal places */ /*::[*/0x0022/*::*/: "0.00", /*::[*/0x0032/*::*/: "0.00;[Red]0.00", /*::[*/0x0042/*::*/: "0.00;\(0.00\)", diff --git a/bits/79_html.js b/bits/79_html.js index bb6f2e6..129276f 100644 --- a/bits/79_html.js +++ b/bits/79_html.js @@ -122,9 +122,9 @@ function sheet_to_html(ws/*:Worksheet*/, opts/*:?Sheet2HTMLOpts*//*, wb:?Workboo var header = o.header != null ? o.header : HTML_BEGIN; var footer = o.footer != null ? o.footer : HTML_END; var out/*:Array*/ = [header]; - var r = decode_range(ws['!ref']); + var r = decode_range(ws['!ref'] || "A1"); out.push(make_html_preamble(ws, r, o)); - for(var R = r.s.r; R <= r.e.r; ++R) out.push(make_html_row(ws, r, R, o)); + if(ws["!ref"]) for(var R = r.s.r; R <= r.e.r; ++R) out.push(make_html_row(ws, r, R, o)); out.push("" + footer); return out.join(""); } diff --git a/bits/81_writeods.js b/bits/81_writeods.js index d9fd6f3..eb8d5e9 100644 --- a/bits/81_writeods.js +++ b/bits/81_writeods.js @@ -364,6 +364,7 @@ var write_content_ods/*:{(wb:any, opts:any):string}*/ = /* @__PURE__ */(function wb.SheetNames.map(function(n) { return wb.Sheets[n]; }).forEach(function(ws) { if(!ws) return; var dense = (ws["!data"] != null); + if(!ws["!ref"]) return; var range = decode_range(ws["!ref"]); for(var R = 0; R <= range.e.r; ++R) for(var C = 0; C <= range.e.c; ++C) { var c = dense ? (ws["!data"][R]||[])[C] : ws[encode_cell({r:R,c:C})]; diff --git a/bits/83_numbers.js b/bits/83_numbers.js index 7d02754..b6a5a8f 100644 --- a/bits/83_numbers.js +++ b/bits/83_numbers.js @@ -1846,6 +1846,8 @@ function write_numbers_ws(cfb, deps, ws, wsname, sheetidx, rootref) { } var USE_WIDE_ROWS = true; function write_numbers_tma(cfb, deps, ws, tmaroot, tmafile, tmaref) { + if (!ws["!ref"]) + throw new Error("Cannot export empty sheet to NUMBERS"); var range = decode_range(ws["!ref"]); range.s.r = range.s.c = 0; var trunc = false; diff --git a/modules/83_numbers.js b/modules/83_numbers.js index 7d02754..b6a5a8f 100644 --- a/modules/83_numbers.js +++ b/modules/83_numbers.js @@ -1846,6 +1846,8 @@ function write_numbers_ws(cfb, deps, ws, wsname, sheetidx, rootref) { } var USE_WIDE_ROWS = true; function write_numbers_tma(cfb, deps, ws, tmaroot, tmafile, tmaref) { + if (!ws["!ref"]) + throw new Error("Cannot export empty sheet to NUMBERS"); var range = decode_range(ws["!ref"]); range.s.r = range.s.c = 0; var trunc = false; diff --git a/modules/83_numbers.ts b/modules/83_numbers.ts index fe1b18d..58472cb 100644 --- a/modules/83_numbers.ts +++ b/modules/83_numbers.ts @@ -1641,6 +1641,7 @@ var USE_WIDE_ROWS = true; /** Write .TST.TableModelArchive */ function write_numbers_tma(cfb: CFB$Container, deps: Dependents, ws: WorkSheet, tmaroot: IWAArchiveInfo, tmafile: IWAArchiveInfo[], tmaref: number) { + if(!ws["!ref"]) throw new Error("Cannot export empty sheet to NUMBERS"); var range = decode_range(ws["!ref"] as string); range.s.r = range.s.c = 0;