Skip to content
This repository has been archived by the owner on Jul 22, 2022. It is now read-only.

Commit

Permalink
Merge pull request #228 from natergj/issue218-null-string
Browse files Browse the repository at this point in the history
fix issue 228 
#228
  • Loading branch information
natergj authored Sep 9, 2018
2 parents 9b63171 + f732832 commit 8d1ee19
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 18 deletions.
27 changes: 13 additions & 14 deletions source/lib/cell/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,21 @@ const util = require('util');

function stringSetter(val) {
let logger = this.ws.wb.logger;

if (typeof (val) !== 'string') {
logger.warn('Value sent to String function of cells %s was not a string, it has type of %s',
JSON.stringify(this.excelRefs),
typeof (val));
val = '';
}

let chars, chr;
chars = /[\u0000-\u0008\u000B-\u000C\u000E-\u001F\uD800-\uDFFF\uFFFE-\uFFFF]/;
chr = val.match(chars);
if (chr) {
logger.warn('Invalid Character for XML "' + chr + '" in string "' + val + '"');
val = val.replace(chr, '');
}

if (typeof(val) !== 'string') {
logger.warn('Value sent to String function of cells %s was not a string, it has type of %s',
JSON.stringify(this.excelRefs),
typeof(val));
val = '';
}

val = val.toString();
// Remove Control characters, they aren't understood by xmlbuilder
val = val.replace(/[\u0000-\u0008\u000B-\u000C\u000E-\u001F\uD800-\uDFFF\uFFFE-\uFFFF]/, '');

Expand Down Expand Up @@ -54,7 +53,7 @@ function numberSetter(val) {
if (val === undefined || parseFloat(val) !== val) {
throw new TypeError(util.format('Value sent to Number function of cells %s was not a number, it has type of %s and value of %s',
JSON.stringify(this.excelRefs),
typeof(val),
typeof (val),
val
));
}
Expand All @@ -68,14 +67,14 @@ function numberSetter(val) {
var c = this.cells[0];
c.number(val);
}
return this;
return this;
}

function booleanSetter(val) {
if (val === undefined || typeof (val.toString().toLowerCase() === 'true' || ((val.toString().toLowerCase() === 'false') ? false : val)) !== 'boolean') {
throw new TypeError(util.format('Value sent to Bool function of cells %s was not a bool, it has type of %s and value of %s',
JSON.stringify(this.excelRefs),
typeof(val),
typeof (val),
val
));
}
Expand All @@ -93,8 +92,8 @@ function booleanSetter(val) {
}

function formulaSetter(val) {
if (typeof(val) !== 'string') {
throw new TypeError(util.format('Value sent to Formula function of cells %s was not a string, it has type of %s', JSON.stringify(this.excelRefs), typeof(val)));
if (typeof (val) !== 'string') {
throw new TypeError(util.format('Value sent to Formula function of cells %s was not a string, it has type of %s', JSON.stringify(this.excelRefs), typeof (val)));
}
if (this.merged !== true) {
this.cells.forEach((c, i) => {
Expand Down
19 changes: 15 additions & 4 deletions tests/cell.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,29 @@ test('Add String to cell', (t) => {
let cell = ws.cell(1, 1).string('my test string');
let thisCell = ws.cells[cell.excelRefs[0]];
t.ok(thisCell.t === 's', 'cellType set to sharedString');
t.ok(typeof(thisCell.v) === 'number', 'cell Value is a number');
t.ok(typeof (thisCell.v) === 'number', 'cell Value is a number');
t.ok(wb.sharedStrings[thisCell.v] === 'my test string', 'Cell sharedString value is correct');
});

test('Replace null or undefined value with empty string', (t) => {
t.plan(3);
let wb = new xl.Workbook();
let ws = wb.addWorksheet('test');
let cell = ws.cell(1, 1).string(null);
let thisCell = ws.cells[cell.excelRefs[0]];
t.ok(thisCell.t === 's', 'cellType set to sharedString');
t.ok(typeof (thisCell.v) === 'number', 'cell Value is a number');
t.ok(wb.sharedStrings[thisCell.v] === '', 'Cell is empty string');
});

test('Add Number to cell', (t) => {
t.plan(3);
let wb = new xl.Workbook();
let ws = wb.addWorksheet('test');
let cell = ws.cell(1, 1).number(10);
let thisCell = ws.cells[cell.excelRefs[0]];
t.ok(thisCell.t === 'n', 'cellType set to number');
t.ok(typeof(thisCell.v) === 'number', 'cell Value is a number');
t.ok(typeof (thisCell.v) === 'number', 'cell Value is a number');
t.ok(thisCell.v === 10, 'Cell value value is correct');
});

Expand All @@ -46,7 +57,7 @@ test('Add Boolean to cell', (t) => {
let cell = ws.cell(1, 1).bool(true);
let thisCell = ws.cells[cell.excelRefs[0]];
t.ok(thisCell.t === 'b', 'cellType set to boolean');
t.ok(typeof(thisCell.v) === 'string', 'cell Value is a string');
t.ok(typeof (thisCell.v) === 'string', 'cell Value is a string');
t.ok(thisCell.v === 'true' || thisCell.v === 'false', 'Cell value value is correct');
});

Expand All @@ -58,6 +69,6 @@ test('Add Formula to cell', (t) => {
let thisCell = ws.cells[cell.excelRefs[0]];
t.ok(thisCell.t === null, 'cellType is not set');
t.ok(thisCell.v === null, 'cellValue is not set');
t.ok(typeof(thisCell.f) === 'string', 'cell Formula is a string');
t.ok(typeof (thisCell.f) === 'string', 'cell Formula is a string');
t.ok(thisCell.f === 'SUM(A1:A10)', 'Cell value value is correct');
});

0 comments on commit 8d1ee19

Please sign in to comment.