Skip to content

Commit 888dcc8

Browse files
Merge pull request #11 from zurmokeeper/bugfix/cell_style_and_datavalidation_bug
Bugfix/cell style and datavalidation bug
2 parents c1e348c + b664b89 commit 888dcc8

File tree

11 files changed

+186
-7
lines changed

11 files changed

+186
-7
lines changed

.github/workflows/tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ jobs:
7676
- uses: actions/checkout@v2
7777
- uses: actions/setup-node@v2
7878
with:
79-
node-version: 12.x
79+
node-version: 14.x
8080
- name: Create the npm cache directory
8181
run: mkdir npm-cache && npm config set cache ./npm-cache --global
8282
- name: Cache node modules

lib/doc/cell.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ class Cell {
349349
}
350350

351351
if (value.style) {
352-
this.style = value.style;
352+
this.style = _.cloneDeep(value.style);
353353
} else {
354354
this.style = {};
355355
}
@@ -862,8 +862,7 @@ class FormulaValue {
862862
if (!this._translatedFormula && this.model.sharedFormula) {
863863
const {worksheet} = this.cell;
864864
const master = worksheet.findCell(this.model.sharedFormula);
865-
this._translatedFormula =
866-
master && slideFormula(master.formula, master.address, this.model.address);
865+
this._translatedFormula = master && slideFormula(master.formula, master.address, this.model.address);
867866
}
868867
return this._translatedFormula;
869868
}

lib/utils/under-dash.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,34 @@ const _ = {
179179
}
180180
return target;
181181
},
182+
183+
cloneDeep: function cloneDeep(value, preserveUndefined) {
184+
if (preserveUndefined === undefined) {
185+
preserveUndefined = true;
186+
}
187+
let clone;
188+
if (value === null) {
189+
return null;
190+
}
191+
if (value instanceof Date) {
192+
return value;
193+
}
194+
if (value instanceof Array) {
195+
clone = [];
196+
} else if (typeof value === 'object') {
197+
clone = {};
198+
} else {
199+
return value;
200+
}
201+
_.each(value, (_value, name) => {
202+
if (_value !== undefined) {
203+
clone[name] = cloneDeep(_value, preserveUndefined);
204+
} else if (preserveUndefined) {
205+
clone[name] = undefined;
206+
}
207+
});
208+
return clone;
209+
},
182210
};
183211

184212
module.exports = _;

lib/xlsx/xform/sheet/data-validations-xform.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,14 @@ class DataValidationsXform extends BaseXform {
143143
if (value.type === 'date') {
144144
xmlStream.writeText(utils.dateToExcel(new Date(formula)));
145145
} else {
146+
if (value.type === 'list') {
147+
const pattern = /^".*"$/; // Match "XX, XXX1"
148+
if (pattern.test(formula) && formula.length - '""'.length > 255) {
149+
throw new Error(
150+
'The input cannot be larger than 255 characters. Please check the value of dataValidation.formulae'
151+
);
152+
}
153+
}
146154
xmlStream.writeText(formula);
147155
}
148156
xmlStream.closeNode();

lib/xlsx/xlsx.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -677,6 +677,10 @@ class XLSX {
677677
resolve();
678678
});
679679
stream.on('error', error => {
680+
// TODO: Now the error is still generated, consider how to deal with it, manually delete the file?
681+
// fs.unlink(filename, err =>{
682+
// reject(err);
683+
// });
680684
reject(error);
681685
});
682686

@@ -685,6 +689,7 @@ class XLSX {
685689
stream.end();
686690
})
687691
.catch(err => {
692+
// stream.emit('error', err);
688693
reject(err);
689694
});
690695
});

spec/end-to-end/express-encryption.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ describe('Express test workbook.xlsx.write encryption', () => {
3030
});
3131

3232
it('downloads a workbook, workbook.xlsx.write encryption successful', async function() {
33-
this.timeout(5000);
33+
this.timeout(50000);
3434
const res = got.stream('http://127.0.0.1:3003/workbook', {
3535
decompress: false,
3636
});
18.8 MB
Binary file not shown.
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
const ExcelJS = verquire('exceljs');
2+
3+
describe('github issues', () => {
4+
describe('issue 10 - (dataValidation) The exported table inserts a drop-down menu for the cell. The cell cannot display drop-down options', () => {
5+
it('issue 10 - (dataValidation) Input content is 266 characters, more than 255 characters, throw error', async () => {
6+
async function test() {
7+
const workbook = new ExcelJS.Workbook();
8+
const worksheet = workbook.addWorksheet('sheet');
9+
const columns = [
10+
{
11+
header: 'Test1',
12+
key: 'test1',
13+
width: 25,
14+
},
15+
];
16+
worksheet.columns = columns;
17+
const cell = worksheet.getCell('A2');
18+
const value =
19+
'"1234 Main St, Anytown 56789,5678 Elm Ave, Smallville 23456,9012 Oak Rd, Big City 34567,3456 Pine Dr, Tinytown 67890,7890 Maple Ln, Mediumtown 12345,2345 Hickory St, Suburbia 45678,6789 Cedar Cir, Urbanville 89012,0123 Birch Blvd, Countryside 23456,4567 Wal"';
20+
cell.dataValidation = {
21+
type: 'list',
22+
allowBlank: true,
23+
formulae: [value],
24+
};
25+
await workbook.xlsx.writeFile(
26+
'./spec/integration/data/test-new-issue-10-1.xlsx'
27+
);
28+
}
29+
let error;
30+
try {
31+
await test();
32+
} catch (err) {
33+
error = err;
34+
}
35+
expect(error)
36+
.to.be.an('error')
37+
.with.property(
38+
'message',
39+
'The input cannot be larger than 255 characters. Please check the value of dataValidation.formulae'
40+
);
41+
});
42+
43+
it('issue 2256 - (dataValidation) Input content is 255 characters, normal display', async () => {
44+
async function test() {
45+
const workbook = new ExcelJS.Workbook();
46+
const worksheet = workbook.addWorksheet('sheet');
47+
const columns = [
48+
{
49+
header: 'Test1',
50+
key: 'test1',
51+
width: 25,
52+
},
53+
];
54+
worksheet.columns = columns;
55+
const cell = worksheet.getCell('A2');
56+
const value =
57+
'"1234 Main St, Anytown 56789,5678 Elm Ave, Smallville 23456,9012 Oak Rd, Big City 34567,3456 Pine Dr, Tinytown 67890,7890 Maple Ln, Mediumtown 12345,2345 Hickory St, Suburbia 45678,6789 Cedar Cir, Urbanville 89012,0123 Birch Blvd, Countryside 23456,4567 Wa"';
58+
cell.dataValidation = {
59+
type: 'list',
60+
allowBlank: true,
61+
formulae: [value],
62+
};
63+
await workbook.xlsx.writeFile(
64+
'./spec/integration/data/test-new-issue-10-2.xlsx'
65+
);
66+
}
67+
await test();
68+
});
69+
});
70+
});

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,24 @@ describe('WorkbookReader', () => {
130130
{maxRows: 20}
131131
);
132132
});
133+
134+
// describe('Big file support', () => {
135+
// it('should read large file correctly', function(done) {
136+
// this.timeout(20000);
137+
// const workbook = new ExcelJS.Workbook();
138+
// workbook.xlsx
139+
// .read(
140+
// fs.createReadStream('./spec/integration/data/extra-large.xlsx')
141+
// )
142+
// .then(result => {
143+
// expect(result._worksheets[1]._rows.length).to.equal(512781);
144+
// expect(
145+
// result._worksheets[1]._rows[500000]._cells[4]._value.value
146+
// ).to.equal(11);
147+
// done();
148+
// });
149+
// });
150+
// });
133151
});
134152
});
135153

spec/integration/workbook/workbook.spec.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -639,9 +639,13 @@ describe('Workbook', () => {
639639
const ws2 = wb2.getWorksheet('duplicateTest');
640640

641641
expect(ws2.getCell('A2').value).to.equal('OneInfo');
642-
expect(ws2.getCell('A2').style).to.equal(ws2.getCell('A1').style);
642+
expect(ws2.getCell('A2').style).to.deep.equal(
643+
ws2.getCell('A1').style
644+
);
643645
expect(ws2.getCell('A3').value).to.equal('OneInfo');
644-
expect(ws2.getCell('A3').style).to.equal(ws2.getCell('A1').style);
646+
expect(ws2.getCell('A3').style).to.deep.equal(
647+
ws2.getCell('A1').style
648+
);
645649
expect(ws2.getCell('A4').value).to.be.null();
646650
});
647651
});

0 commit comments

Comments
 (0)