From 6127e19c0225c3a584aa089d80e8c35fe5d73c48 Mon Sep 17 00:00:00 2001 From: Alex Baranov Date: Thu, 18 Jun 2020 14:34:04 -0700 Subject: [PATCH] feat(raw-numbers): optional raw numbers --- bits/90_utils.js | 4 ++-- types/index.d.ts | 6 ++++++ xlsx.flow.js | 4 ++-- xlsx.js | 4 ++-- xlsx.mini.flow.js | 4 ++-- xlsx.mini.js | 4 ++-- 6 files changed, 16 insertions(+), 10 deletions(-) diff --git a/bits/90_utils.js b/bits/90_utils.js index 3f329b8..cbf1fd8 100644 --- a/bits/90_utils.js +++ b/bits/90_utils.js @@ -33,7 +33,7 @@ function make_json_row(sheet/*:Worksheet*/, r/*:Range*/, R/*:number*/, cols/*:Ar else if(raw && v === null) row[hdr[C]] = null; else continue; } else { - row[hdr[C]] = raw ? v : format_cell(val,v,o); + row[hdr[C]] = raw || (o.rawNumbers && val.t == "n") ? v : format_cell(val,v,o); } if(v != null) isempty = false; } @@ -98,7 +98,7 @@ function make_csv_row(sheet/*:Worksheet*/, r/*:Range*/, R/*:number*/, cols/*:Arr if(val == null) txt = ""; else if(val.v != null) { isempty = false; - txt = ''+format_cell(val, null, o); + txt = ''+(o.rawNumbers && val.t == "n" ? val.v : format_cell(val, null, o)); for(var i = 0, cc = 0; i !== txt.length; ++i) if((cc = txt.charCodeAt(i)) === fs || cc === rs || cc === 34 || o.forceQuotes) {txt = "\"" + txt.replace(qreg, '""') + "\""; break; } if(txt == "ID") txt = '"ID"'; } else if(val.f != null && !val.F) { diff --git a/types/index.d.ts b/types/index.d.ts index 00b8849..5cfecf2 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -629,6 +629,9 @@ export interface Sheet2CSVOpts extends DateNFOption { /** Force quotes around fields */ forceQuotes?: boolean; + + /** if true, return raw numbers; if false, return formatted numbers */ + rawNumbers?: boolean; } export interface OriginOption { @@ -665,6 +668,9 @@ export interface Sheet2JSONOpts extends DateNFOption { /** if true, return raw data; if false, return formatted text */ raw?: boolean; + + /** if true, return raw numbers; if false, return formatted numbers */ + rawNumbers?: boolean; } export interface AOA2SheetOpts extends CommonOptions, DateNFOption { diff --git a/xlsx.flow.js b/xlsx.flow.js index feb52db..e8470bc 100644 --- a/xlsx.flow.js +++ b/xlsx.flow.js @@ -21286,7 +21286,7 @@ function make_json_row(sheet/*:Worksheet*/, r/*:Range*/, R/*:number*/, cols/*:Ar else if(raw && v === null) row[hdr[C]] = null; else continue; } else { - row[hdr[C]] = raw ? v : format_cell(val,v,o); + row[hdr[C]] = raw || (o.rawNumbers && val.t == "n") ? v : format_cell(val,v,o); } if(v != null) isempty = false; } @@ -21351,7 +21351,7 @@ function make_csv_row(sheet/*:Worksheet*/, r/*:Range*/, R/*:number*/, cols/*:Arr if(val == null) txt = ""; else if(val.v != null) { isempty = false; - txt = ''+format_cell(val, null, o); + txt = ''+(o.rawNumbers && val.t == "n" ? val.v : format_cell(val, null, o)); for(var i = 0, cc = 0; i !== txt.length; ++i) if((cc = txt.charCodeAt(i)) === fs || cc === rs || cc === 34 || o.forceQuotes) {txt = "\"" + txt.replace(qreg, '""') + "\""; break; } if(txt == "ID") txt = '"ID"'; } else if(val.f != null && !val.F) { diff --git a/xlsx.js b/xlsx.js index 92ff6e0..5d37e73 100644 --- a/xlsx.js +++ b/xlsx.js @@ -21155,7 +21155,7 @@ function make_json_row(sheet, r, R, cols, header, hdr, dense, o) { else if(raw && v === null) row[hdr[C]] = null; else continue; } else { - row[hdr[C]] = raw ? v : format_cell(val,v,o); + row[hdr[C]] = raw || (o.rawNumbers && val.t == "n") ? v : format_cell(val,v,o); } if(v != null) isempty = false; } @@ -21220,7 +21220,7 @@ function make_csv_row(sheet, r, R, cols, fs, rs, FS, o) { if(val == null) txt = ""; else if(val.v != null) { isempty = false; - txt = ''+format_cell(val, null, o); + txt = ''+(o.rawNumbers && val.t == "n" ? val.v : format_cell(val, null, o)); for(var i = 0, cc = 0; i !== txt.length; ++i) if((cc = txt.charCodeAt(i)) === fs || cc === rs || cc === 34 || o.forceQuotes) {txt = "\"" + txt.replace(qreg, '""') + "\""; break; } if(txt == "ID") txt = '"ID"'; } else if(val.f != null && !val.F) { diff --git a/xlsx.mini.flow.js b/xlsx.mini.flow.js index 7a4b877..462ae2e 100644 --- a/xlsx.mini.flow.js +++ b/xlsx.mini.flow.js @@ -9376,7 +9376,7 @@ function make_json_row(sheet/*:Worksheet*/, r/*:Range*/, R/*:number*/, cols/*:Ar else if(raw && v === null) row[hdr[C]] = null; else continue; } else { - row[hdr[C]] = raw ? v : format_cell(val,v,o); + row[hdr[C]] = raw || (o.rawNumbers && val.t == "n") ? v : format_cell(val,v,o); } if(v != null) isempty = false; } @@ -9441,7 +9441,7 @@ function make_csv_row(sheet/*:Worksheet*/, r/*:Range*/, R/*:number*/, cols/*:Arr if(val == null) txt = ""; else if(val.v != null) { isempty = false; - txt = ''+format_cell(val, null, o); + txt = ''+(o.rawNumbers && val.t == "n" ? val.v : format_cell(val, null, o)); for(var i = 0, cc = 0; i !== txt.length; ++i) if((cc = txt.charCodeAt(i)) === fs || cc === rs || cc === 34 || o.forceQuotes) {txt = "\"" + txt.replace(qreg, '""') + "\""; break; } if(txt == "ID") txt = '"ID"'; } else if(val.f != null && !val.F) { diff --git a/xlsx.mini.js b/xlsx.mini.js index 8e476de..57c528d 100644 --- a/xlsx.mini.js +++ b/xlsx.mini.js @@ -9271,7 +9271,7 @@ function make_json_row(sheet, r, R, cols, header, hdr, dense, o) { else if(raw && v === null) row[hdr[C]] = null; else continue; } else { - row[hdr[C]] = raw ? v : format_cell(val,v,o); + row[hdr[C]] = raw || (o.rawNumbers && val.t == "n") ? v : format_cell(val,v,o); } if(v != null) isempty = false; } @@ -9336,7 +9336,7 @@ function make_csv_row(sheet, r, R, cols, fs, rs, FS, o) { if(val == null) txt = ""; else if(val.v != null) { isempty = false; - txt = ''+format_cell(val, null, o); + txt = ''+(o.rawNumbers && val.t == "n" ? val.v : format_cell(val, null, o)); for(var i = 0, cc = 0; i !== txt.length; ++i) if((cc = txt.charCodeAt(i)) === fs || cc === rs || cc === 34 || o.forceQuotes) {txt = "\"" + txt.replace(qreg, '""') + "\""; break; } if(txt == "ID") txt = '"ID"'; } else if(val.f != null && !val.F) {