Fix rawNumber support inside sheet_to_json

This commit is contained in:
Andrew Lessels 2022-03-20 16:29:24 +11:00
parent 69bb1e79a3
commit a5b387716c
11 changed files with 1102 additions and 1283 deletions

View File

@ -34,7 +34,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 || (o.rawNumbers && val.t == "n") ? v : format_cell(val,v,o);
row[hdr[C]] = raw && (val.t !== "n" || (val.t === "n" && o.rawNumbers !== false)) ? v : format_cell(val,v,o);
}
if(v != null) isempty = false;
}

16
test.js
View File

@ -144,6 +144,8 @@ var paths = {
dtxlsx: dir + 'xlsx-stream-d-date-cell.xlsx',
dtxlsb: dir + 'xlsx-stream-d-date-cell.xlsb',
dtfxlsx: dir + 'DataTypesFormats.xlsx',
fstxls: dir + 'formula_stress_test.xls',
fstxml: dir + 'formula_stress_test.xls.xml',
fstxlsx: dir + 'formula_stress_test.xlsx',
@ -1381,6 +1383,20 @@ describe('parse features', function() {
});
});
});
describe('data types formats', function() {[
['xlsx', paths.dtfxlsx],
].forEach(function(m) { it(m[0], function() {
var wb = X.read(fs.readFileSync(m[1]), {type: TYPE, cellDates: true});
var ws = wb.Sheets[wb.SheetNames[0]];
var data = X.utils.sheet_to_json(ws, { header: 1, raw: true, rawNumbers: false });
assert(data[0][1] instanceof Date);
assert(data[1][1] instanceof Date);
assert.equal(data[2][1], '$123.00');
assert.equal(data[3][1], '98.76%');
assert.equal(data[4][1], '456.00');
assert.equal(data[5][1], '7,890');
}); }); });
});
describe('write features', function() {

16
test.mjs generated
View File

@ -145,6 +145,8 @@ var paths = {
dtxlsx: dir + 'xlsx-stream-d-date-cell.xlsx',
dtxlsb: dir + 'xlsx-stream-d-date-cell.xlsb',
dtfxlsx: dir + 'DataTypesFormats.xlsx',
fstxls: dir + 'formula_stress_test.xls',
fstxml: dir + 'formula_stress_test.xls.xml',
fstxlsx: dir + 'formula_stress_test.xlsx',
@ -1371,6 +1373,20 @@ describe('parse features', function() {
});
});
});
describe('data types formats', function() {[
['xlsx', paths.dtfxlsx]
].forEach(function(m) { it(m[0], function() {
var wb = X.read(fs.readFileSync(m[1]), {type: TYPE, cellDates: true});
var ws = wb.Sheets[wb.SheetNames[0]];
var data = X.utils.sheet_to_json(ws, { header: 1, raw: true, rawNumbers: false });
assert.ok(data[0][1] instanceof Date);
assert.ok(data[1][1] instanceof Date);
assert.equal(data[2][1], '$123.00');
assert.equal(data[3][1], '98.76%');
assert.equal(data[4][1], '456.00');
assert.equal(data[5][1], '7,890');
}); }); });
});
describe('write features', function() {

18
test.ts
View File

@ -165,6 +165,8 @@ var paths: any = {
dtxlsx: dir + 'xlsx-stream-d-date-cell.xlsx',
dtxlsb: dir + 'xlsx-stream-d-date-cell.xlsb',
dtfxlsx: dir + 'DataTypesFormats.xlsx',
fstxls: dir + 'formula_stress_test.xls',
fstxml: dir + 'formula_stress_test.xls.xml',
fstxlsx: dir + 'formula_stress_test.xlsx',
@ -951,7 +953,7 @@ Deno.test('parse features', async function(t) {
X.read(fs.readFileSync(paths.cpxml), {type:TYPE, WTF:true})
];
var s1 = ['XLSX', 'XLSB', 'XLS', 'XML']; for(var i = 0; i < s1.length; ++i) { let x = s1[i];
var s1 = ['XLSX', 'XLSB', 'XLS', 'XML']; for(var i = 0; i < s1.length; ++i) { let x = s1[i];
await t.step(x + ' should parse core properties', async function(t) { var P = wbs?.[i]?.Props; if(typeof P == "undefined") throw "missing props"; coreprop(P); });
await t.step(x + ' should parse custom properties', async function(t) { custprop(wbs?.[i]?.Custprops); });
}
@ -1335,6 +1337,20 @@ Deno.test('parse features', async function(t) {
});
});
});
await t.step('data types formats', async function(t) {var dtf = [
['xlsx', paths.dtfxlsx]
]; for(var j = 0; j < dtf.length; ++j) { var m = dtf[j]; await t.step(m[0], async function(t) {
var wb = X.read(fs.readFileSync(m[1]), {type: TYPE, cellDates: true});
var ws = wb.Sheets[wb.SheetNames[0]];
var data = X.utils.sheet_to_json<any>(ws, { header: 1, raw: true, rawNumbers: false });
assert.assert(data[0][1] instanceof Date);
assert.assert(data[1][1] instanceof Date);
assert.equal(data[2][1], '$123.00');
assert.equal(data[3][1], '98.76%');
assert.equal(data[4][1], '456.00');
assert.equal(data[5][1], '7,890');
}); } });
});
Deno.test('write features', async function(t) {

View File

@ -45,6 +45,7 @@ text_and_numbers.xlsb
time_stress_test_1.xlsb.pending
xlsx-stream-d-date-cell.xlsb
AutoFilter.xlsx
DataTypesFormats.xlsx
ErrorTypes.xlsx
LONumbers-2010.xlsx
LONumbers-2011.xlsx

16
tests/core.js generated
View File

@ -144,6 +144,8 @@ var paths = {
dtxlsx: dir + 'xlsx-stream-d-date-cell.xlsx',
dtxlsb: dir + 'xlsx-stream-d-date-cell.xlsb',
dtfxlsx: dir + 'DataTypesFormats.xlsx',
fstxls: dir + 'formula_stress_test.xls',
fstxml: dir + 'formula_stress_test.xls.xml',
fstxlsx: dir + 'formula_stress_test.xlsx',
@ -1381,6 +1383,20 @@ describe('parse features', function() {
});
});
});
describe('data types formats', function() {[
['xlsx', paths.dtfxlsx],
].forEach(function(m) { it(m[0], function() {
var wb = X.read(fs.readFileSync(m[1]), {type: TYPE, cellDates: true});
var ws = wb.Sheets[wb.SheetNames[0]];
var data = X.utils.sheet_to_json(ws, { header: 1, raw: true, rawNumbers: false });
assert(data[0][1] instanceof Date);
assert(data[1][1] instanceof Date);
assert.equal(data[2][1], '$123.00');
assert.equal(data[3][1], '98.76%');
assert.equal(data[4][1], '456.00');
assert.equal(data[5][1], '7,890');
}); }); });
});
describe('write features', function() {

1
tests/fixtures.js generated

File diff suppressed because one or more lines are too long

View File

@ -57,6 +57,7 @@
./test_files/column_width.xlsb
./test_files/column_width.slk
./test_files/cross-sheet_formula_names.xlsb
./test_files/DataTypesFormats.xlsx
./test_files/defined_names_simple.xls
./test_files/defined_names_simple.xml
./test_files/defined_names_simple.xlsx

File diff suppressed because it is too large Load Diff

1142
xlsx.js generated

File diff suppressed because it is too large Load Diff

2
xlsx.mjs generated
View File

@ -23553,7 +23553,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 || (o.rawNumbers && val.t == "n") ? v : format_cell(val,v,o);
row[hdr[C]] = raw && (val.t !== "n" || (val.t === "n" && o.rawNumbers !== false)) ? v : format_cell(val,v,o);
}
if(v != null) isempty = false;
}