Skip to content

Fil/xlsx #251

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions src/xlsx.js
Original file line number Diff line number Diff line change
Expand Up @@ -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("");
Expand All @@ -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],
Expand Down Expand Up @@ -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;
}
27 changes: 14 additions & 13 deletions test/xlsx-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,50 +163,51 @@ 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},
{B: 31, C: 32, D: 33, E: 34, F: 35, G: 36, H: 37, I: 38, J: 39},
]);

// "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},
{H: 27, I: 28, J: 29},
{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"
Expand Down