diff --git a/src/xlsx.js b/src/xlsx.js index 43b658c1..98da375c 100644 --- a/src/xlsx.js +++ b/src/xlsx.js @@ -45,6 +45,7 @@ function extract(sheet, {range, headers}) { function valueOf(cell) { if (!cell) return; const {value} = cell; + if (value && value instanceof Date) return value; if (value && typeof value === "object") { if (value.formula) return value.result; if (value.richText) return value.richText.map((d) => d.text).join(""); @@ -61,8 +62,8 @@ function parseRange(specifier = [], {columnCount, rowCount}) { if (typeof specifier === "string") { const [ [c0 = 0, r0 = 0], - [c1 = columnCount - 1, r1 = rowCount - 1] = [], - ] = specifier.split(":").map(NN); + [c1 = columnCount - 1, r1 = rowCount - 1], + ] = NN2(specifier); return [ [c0, r0], [c1, r1], @@ -100,3 +101,10 @@ function NN(s) { } return [c ? c - 1 : undefined, sr ? +sr - 1 : undefined]; } + +// "A" is the "A:A" column; "1" is the "1:1" row, "A1" is the "A1:A1" cell +function NN2(s) { + s = s.split(":").map(NN); + if (s.length === 1) s[1] = s[0]; + return s; +} \ No newline at end of file diff --git a/test/xlsx-test.js b/test/xlsx-test.js index 0a9f8781..f4b34e20 100644 --- a/test/xlsx-test.js +++ b/test/xlsx-test.js @@ -163,12 +163,11 @@ test("FileAttachment.xlsx reads sheet ranges", (t) => { ]); // "B2" - // [[1,1]] t.same(workbook.sheet(0, {range: "B2"}), [ - {B: 11, C: 12, D: 13, E: 14, F: 15, G: 16, H: 17, I: 18, J: 19}, - {B: 21, C: 22, D: 23, E: 24, F: 25, G: 26, H: 27, I: 28, J: 29}, - {B: 31, C: 32, D: 33, E: 34, F: 35, G: 36, H: 37, I: 38, J: 39}, + {B: 11}, ]); + + // [[1,1]] t.same(workbook.sheet(0, {range: [[1, 1]]}), [ {B: 11, C: 12, D: 13, E: 14, F: 15, G: 16, H: 17, I: 18, J: 19}, {B: 21, C: 22, D: 23, E: 24, F: 25, G: 26, H: 27, I: 28, J: 29}, @@ -176,13 +175,14 @@ test("FileAttachment.xlsx reads sheet ranges", (t) => { ]); // "H" - // [[7]] t.same(workbook.sheet(0, {range: "H"}), [ - {H: 7, I: 8, J: 9}, - {H: 17, I: 18, J: 19}, - {H: 27, I: 28, J: 29}, - {H: 37, I: 38, J: 39}, + {H: 7}, + {H: 17}, + {H: 27}, + {H: 37}, ]); + + // [[7]] t.same(workbook.sheet(0, {range: [[7]]}), [ {H: 7, I: 8, J: 9}, {H: 17, I: 18, J: 19}, @@ -190,23 +190,24 @@ test("FileAttachment.xlsx reads sheet ranges", (t) => { {H: 37, I: 38, J: 39}, ]); - // ":I" - // [,[1,]] + // "I:" + // [[8,],] const sheetJ = [ {I: 8, J: 9}, {I: 18, J: 19}, {I: 28, J: 29}, {I: 38, J: 39}, ]; - t.same(workbook.sheet(0, {range: "I"}), sheetJ); + t.same(workbook.sheet(0, {range: "I:"}), sheetJ); t.same(workbook.sheet(0, {range: [[8, undefined], undefined]}), sheetJ); // ":ZZ" (doesn't cause extra column fields) t.same(workbook.sheet(0, {range: ":ZZ"}), entireSheet); // "2" + t.same(workbook.sheet(0, {range: "2"}), entireSheet.slice(1,2)); + // [[,1]] - t.same(workbook.sheet(0, {range: "2"}), entireSheet.slice(1)); t.same(workbook.sheet(0, {range: [[undefined, 1]]}), entireSheet.slice(1)); // ":2"