Skip to content

Commit 43ef243

Browse files
Siemienikalubbe
authored andcommitted
Properly assigning styles by deep copy
* property assigning styles by deep copy + var to const/let refactoring * reduce async
1 parent 13621bd commit 43ef243

File tree

3 files changed

+69
-46
lines changed

3 files changed

+69
-46
lines changed

lib/doc/row.js

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
'use strict';
22

3-
var _ = require('../utils/under-dash');
3+
const _ = require('../utils/under-dash');
44

5-
var Enums = require('./enums');
6-
var colCache = require('./../utils/col-cache');
7-
var Cell = require('./cell');
5+
const Enums = require('./enums');
6+
const colCache = require('./../utils/col-cache');
7+
const Cell = require('./cell');
88

9-
var Row = module.exports = function(worksheet, number) {
9+
const Row = module.exports = function (worksheet, number) {
1010
this._worksheet = worksheet;
1111
this._number = number;
1212
this._cells = [];
@@ -43,9 +43,9 @@ Row.prototype = {
4343

4444
// given {address, row, col}, find or create new cell
4545
getCellEx: function(address) {
46-
var cell = this._cells[address.col - 1];
46+
let cell = this._cells[address.col - 1];
4747
if (!cell) {
48-
var column = this._worksheet.getColumn(address.col);
48+
const column = this._worksheet.getColumn(address.col);
4949
cell = new Cell(this, column, address.address);
5050
this._cells[address.col - 1] = cell;
5151
}
@@ -56,7 +56,7 @@ Row.prototype = {
5656
getCell: function(col) {
5757
if (typeof col === 'string') {
5858
// is it a key?
59-
var column = this._worksheet.getColumnKey(col);
59+
const column = this._worksheet.getColumnKey(col);
6060
if (column) {
6161
col = column.number;
6262
} else {
@@ -73,11 +73,11 @@ Row.prototype = {
7373

7474
// remove cell(s) and shift all higher cells down by count
7575
splice: function(start, count) {
76-
var inserts = Array.prototype.slice.call(arguments, 2);
77-
var nKeep = start + count;
78-
var nExpand = inserts.length - count;
79-
var nEnd = this._cells.length;
80-
var i, cSrc, cDst;
76+
const inserts = Array.prototype.slice.call(arguments, 2);
77+
const nKeep = start + count;
78+
const nExpand = inserts.length - count;
79+
const nEnd = this._cells.length;
80+
let i, cSrc, cDst;
8181

8282
if (nExpand < 0) {
8383
// remove cells
@@ -115,8 +115,8 @@ Row.prototype = {
115115
options = null;
116116
}
117117
if (options && options.includeEmpty) {
118-
var n = this._cells.length;
119-
for (var i = 1; i <= n; i++) {
118+
const n = this._cells.length;
119+
for (let i = 1; i <= n; i++) {
120120
iteratee(this.getCell(i), i);
121121
}
122122
} else {
@@ -146,7 +146,7 @@ Row.prototype = {
146146

147147
// return a sparse array of cell values
148148
get values() {
149-
var values = [];
149+
const values = [];
150150
this._cells.forEach(function(cell) {
151151
if (cell && (cell.type !== Enums.ValueType.Null)) {
152152
values[cell.col] = cell.value;
@@ -162,7 +162,7 @@ Row.prototype = {
162162
if (!value) {
163163
// empty row
164164
} else if (value instanceof Array) {
165-
var offset = 0;
165+
let offset = 0;
166166
if (value.hasOwnProperty('0')) {
167167
// contiguous array - start at column 1
168168
offset = 1;
@@ -201,7 +201,7 @@ Row.prototype = {
201201
return this._cells.length;
202202
},
203203
get actualCellCount() {
204-
var count = 0;
204+
let count = 0;
205205
this.eachCell(function() {
206206
count++;
207207
});
@@ -210,8 +210,8 @@ Row.prototype = {
210210

211211
// get the min and max column number for the non-null cells in this row or null
212212
get dimensions() {
213-
var min = 0;
214-
var max = 0;
213+
let min = 0;
214+
let max = 0;
215215
this._cells.forEach(function(cell) {
216216
if (cell && (cell.type !== Enums.ValueType.Null)) {
217217
if (!min || (min > cell.col)) {
@@ -288,12 +288,12 @@ Row.prototype = {
288288

289289
// =========================================================================
290290
get model() {
291-
var cells = [];
292-
var min = 0;
293-
var max = 0;
291+
const cells = [];
292+
let min = 0;
293+
let max = 0;
294294
this._cells.forEach(function(cell) {
295295
if (cell) {
296-
var cellModel = cell.model;
296+
const cellModel = cell.model;
297297
if (cellModel) {
298298
if (!min || (min > cell.col)) {
299299
min = cell.col;
@@ -323,21 +323,21 @@ Row.prototype = {
323323
throw new Error('Invalid row number in model');
324324
}
325325
this._cells = [];
326-
var previousAddress;
326+
let previousAddress;
327327
value.cells.forEach(cellModel => {
328328
switch (cellModel.type) {
329329
case Cell.Types.Merge:
330330
// special case - don't add this types
331331
break;
332332
default:
333-
var address;
333+
let address;
334334
if (cellModel.address) {
335335
address = colCache.decodeAddress(cellModel.address);
336336
} else if (previousAddress) {
337337
// This is a <c> element without an r attribute
338338
// Assume that it's the cell for the next column
339-
var row = previousAddress.row;
340-
var col = previousAddress.col + 1;
339+
const row = previousAddress.row;
340+
const col = previousAddress.col + 1;
341341
address = {
342342
row: row,
343343
col: col,
@@ -346,7 +346,7 @@ Row.prototype = {
346346
};
347347
}
348348
previousAddress = address;
349-
var cell = this.getCellEx(address);
349+
const cell = this.getCellEx(address);
350350
cell.model = cellModel;
351351
break;
352352
}
@@ -361,6 +361,6 @@ Row.prototype = {
361361
this.hidden = value.hidden;
362362
this.outlineLevel = value.outlineLevel || 0;
363363

364-
this.style = value.style || {};
364+
this.style = value.style && JSON.parse(JSON.stringify(value.style)) || {};
365365
}
366366
};
9.06 KB
Binary file not shown.

spec/integration/workbook-xlsx-reader.spec.js

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
'use strict';
22

3-
var verquire = require('../utils/verquire');
4-
var testutils = require('../utils/index');
5-
var fs = require('fs');
6-
var expect = require('chai').expect;
3+
const verquire = require('../utils/verquire');
4+
const testutils = require('../utils/index');
5+
const fs = require('fs');
6+
const expect = require('chai').expect;
77

8-
var Excel = verquire('excel');
8+
const Excel = verquire('excel');
99

10-
var TEST_FILE_NAME = './spec/out/wb.test.xlsx';
10+
const TEST_FILE_NAME = './spec/out/wb.test.xlsx';
1111

1212
// need some architectural changes to make stream read work properly
1313
// because of: shared strings, sheet names, etc are not read in guaranteed order
1414
describe('WorkbookReader', function() {
1515
describe('Serialise', function() {
1616
it('xlsx file', function() {
1717
this.timeout(10000);
18-
var wb = testutils.createTestBook(new Excel.Workbook(), 'xlsx');
18+
const wb = testutils.createTestBook(new Excel.Workbook(), 'xlsx');
1919

2020
return wb.xlsx.writeFile(TEST_FILE_NAME)
2121
.then(function() {
@@ -27,7 +27,7 @@ describe('WorkbookReader', function() {
2727
describe('#readFile', function() {
2828
describe('Row limit', function() {
2929
it('should bail out if the file contains more rows than the limit', function() {
30-
var workbook = new Excel.Workbook();
30+
const workbook = new Excel.Workbook();
3131
// The Fibonacci sheet has 19 rows
3232
return workbook.xlsx.readFile('./spec/integration/data/fibonacci.xlsx', {maxRows: 10})
3333
.then(function() {
@@ -39,7 +39,7 @@ describe('WorkbookReader', function() {
3939

4040
it('should fail fast on a huge file', function() {
4141
this.timeout(20000);
42-
var workbook = new Excel.Workbook();
42+
const workbook = new Excel.Workbook();
4343
return workbook.xlsx.readFile('./spec/integration/data/huge.xlsx', {maxRows: 100})
4444
.then(function() {
4545
throw new Error('Promise unexpectedly fulfilled');
@@ -49,7 +49,7 @@ describe('WorkbookReader', function() {
4949
});
5050

5151
it('should parse fine if the limit is not exceeded', function() {
52-
var workbook = new Excel.Workbook();
52+
const workbook = new Excel.Workbook();
5353
return workbook.xlsx.readFile('./spec/integration/data/fibonacci.xlsx', {maxRows: 20});
5454
});
5555
});
@@ -58,7 +58,7 @@ describe('WorkbookReader', function() {
5858
describe('#read', function() {
5959
describe('Row limit', function() {
6060
it('should bail out if the file contains more rows than the limit', function() {
61-
var workbook = new Excel.Workbook();
61+
const workbook = new Excel.Workbook();
6262
// The Fibonacci sheet has 19 rows
6363
return workbook.xlsx.read(fs.createReadStream('./spec/integration/data/fibonacci.xlsx'), {maxRows: 10})
6464
.then(function() {
@@ -69,16 +69,39 @@ describe('WorkbookReader', function() {
6969
});
7070

7171
it('should parse fine if the limit is not exceeded', function() {
72-
var workbook = new Excel.Workbook();
72+
const workbook = new Excel.Workbook();
7373
return workbook.xlsx.read(fs.createReadStream('./spec/integration/data/fibonacci.xlsx'), {maxRows: 20});
7474
});
7575
});
7676
});
77+
78+
describe('edit styles in existing file', function(){
79+
beforeEach(function(){
80+
this.wb = new Excel.Workbook();
81+
return this.wb.xlsx.readFile('./spec/integration/data/test-row-styles.xlsx');
82+
});
83+
84+
it('edit styles of single row instead of all', function () {
85+
const ws = this.wb.getWorksheet(1);
86+
87+
ws.eachRow((row, rowNo) => {
88+
rowNo % 5 === 0 && (row.font = {color: {argb: '00ff00'}})
89+
});
90+
91+
expect(ws.getRow(3).font.color.argb).to.be.equal(ws.getRow(6).font.color.argb);
92+
expect(ws.getRow(6).font.color.argb).to.be.equal(ws.getRow(9).font.color.argb);
93+
expect(ws.getRow(9).font.color.argb).to.be.equal(ws.getRow(12).font.color.argb);
94+
expect(ws.getRow(12).font.color.argb).not.to.be.equal(ws.getRow(15).font.color.argb);
95+
expect(ws.getRow(15).font.color.argb).not.to.be.equal(ws.getRow(18).font.color.argb);
96+
expect(ws.getRow(15).font.color.argb).to.be.equal(ws.getRow(10).font.color.argb);
97+
expect(ws.getRow(10).font.color.argb).to.be.equal(ws.getRow(5).font.color.argb);
98+
})
99+
});
77100

78101
describe('with a spreadsheet that contains formulas', function() {
79102
before(function() {
80-
var testContext = this;
81-
var workbook = new Excel.Workbook();
103+
const testContext = this;
104+
const workbook = new Excel.Workbook();
82105
return workbook.xlsx.read(fs.createReadStream('./spec/integration/data/formulas.xlsx'))
83106
.then(function() {
84107
testContext.worksheet = workbook.getWorksheet();
@@ -129,8 +152,8 @@ describe('WorkbookReader', function() {
129152

130153
describe('with a spreadsheet that contains a shared string with an escaped underscore', function() {
131154
before(function() {
132-
var testContext = this;
133-
var workbook = new Excel.Workbook();
155+
const testContext = this;
156+
const workbook = new Excel.Workbook();
134157
return workbook.xlsx.read(fs.createReadStream('./spec/integration/data/shared_string_with_escape.xlsx'))
135158
.then(function() {
136159
testContext.worksheet = workbook.getWorksheet();

0 commit comments

Comments
 (0)