Skip to content

Commit

Permalink
Merge pull request #62 from pineapplemachine/bool-fix-2
Browse files Browse the repository at this point in the history
Use more standard representation of boolean values in cells
  • Loading branch information
jrohland authored May 2, 2023
2 parents e442907 + e117f97 commit f66bc70
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
24 changes: 15 additions & 9 deletions source/lib/cell/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,22 +84,28 @@ function numberSetter(val) {
}

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),
val
));
if (val !== true && val !== false) {
let valString = val.toString().toLowerCase();
if (valString === "true") {
val = true;
} else if (valString === "false") {
val = false;
} else {
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),
val
));
}
}
val = val.toString().toLowerCase() === 'true';

if (!this.merged) {
this.cells.forEach((c, i) => {
c.bool(val.toString());
c.bool(val ? '1' : '0');
});
} else {
var c = this.cells[0];
c.bool(val.toString());
c.bool(val ? '1' : '0');
}
return this;
}
Expand Down
2 changes: 1 addition & 1 deletion tests/cell.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ test('Add Boolean to cell', (t) => {
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(thisCell.v === 'true' || thisCell.v === 'false', 'Cell value value is correct');
t.ok(thisCell.v === '1' || thisCell.v === '0', 'Cell value value is correct');
});

test('Add Formula to cell', (t) => {
Expand Down

0 comments on commit f66bc70

Please sign in to comment.