From c6f96c3df767cdb8ffe3d3f6fbbd4cd8db53d912 Mon Sep 17 00:00:00 2001
From: Patrick Lehmhecker
Date: Wed, 26 Apr 2017 17:32:42 +0200
Subject: [PATCH] converting to json with a selected range and header: 1 now
lets the array start at zero
---
bits/90_utils.js | 2 +-
test.js | 9 +++++++++
xlsx.flow.js | 2 +-
xlsx.js | 2 +-
4 files changed, 12 insertions(+), 3 deletions(-)
diff --git a/bits/90_utils.js b/bits/90_utils.js
index eb2f869..4dcdbea 100644
--- a/bits/90_utils.js
+++ b/bits/90_utils.js
@@ -26,7 +26,7 @@ function sheet_to_json(sheet/*:Worksheet*/, opts/*:?Sheet2JSONOpts*/){
cols[C] = encode_col(C);
val = dense ? sheet[R][C] : sheet[cols[C] + rr];
switch(header) {
- case 1: hdr[C] = C; break;
+ case 1: hdr[C] = C - r.s.c; break;
case 2: hdr[C] = cols[C]; break;
case 3: hdr[C] = o.header[C - r.s.c]; break;
default:
diff --git a/test.js b/test.js
index 4883ba5..dcb9da7 100644
--- a/test.js
+++ b/test.js
@@ -1516,6 +1516,15 @@ describe('json output', function() {
assert.equal(json5.length, 2); // = 2 records
assert.equal(json6.length, 3); // = 4 sheet rows - 1 blank row
});
+ it('should have an index that starts with zero when selecting range', function() {
+ var _data = [["S","h","e","e","t","J","S"],[1,2,3,4,5,6,7],[7,6,5,4,3,2,1],[2,3,4,5,6,7,8]];
+ var _ws = X.utils.aoa_to_sheet(_data);
+ var json1 = X.utils.sheet_to_json(_ws, { header:1, raw: true, range: "B1:F3" });
+ assert.equal(json1[0][3], "t");
+ assert.equal(json1[1][0], 2);
+ assert.equal(json1[2][1], 5);
+ assert.equal(json1[2][3], 3);
+ });
});
describe('csv output', function() {
diff --git a/xlsx.flow.js b/xlsx.flow.js
index e4e47d9..d95a144 100644
--- a/xlsx.flow.js
+++ b/xlsx.flow.js
@@ -16562,7 +16562,7 @@ function sheet_to_json(sheet/*:Worksheet*/, opts/*:?Sheet2JSONOpts*/){
cols[C] = encode_col(C);
val = dense ? sheet[R][C] : sheet[cols[C] + rr];
switch(header) {
- case 1: hdr[C] = C; break;
+ case 1: hdr[C] = C - r.s.c; break;
case 2: hdr[C] = cols[C]; break;
case 3: hdr[C] = o.header[C - r.s.c]; break;
default:
diff --git a/xlsx.js b/xlsx.js
index 08a9047..de5700f 100644
--- a/xlsx.js
+++ b/xlsx.js
@@ -16492,7 +16492,7 @@ function sheet_to_json(sheet, opts){
cols[C] = encode_col(C);
val = dense ? sheet[R][C] : sheet[cols[C] + rr];
switch(header) {
- case 1: hdr[C] = C; break;
+ case 1: hdr[C] = C - r.s.c; break;
case 2: hdr[C] = cols[C]; break;
case 3: hdr[C] = o.header[C - r.s.c]; break;
default: