Skip to content

Commit

Permalink
Merge pull request #122 from laucheukhim/fix/error-in-range
Browse files Browse the repository at this point in the history
Return error object instead of state in range
  • Loading branch information
fabiooshiro authored Aug 16, 2024
2 parents 1f6b1d3 + d05716c commit d425a55
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 10 deletions.
4 changes: 2 additions & 2 deletions src/Range.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,15 @@ module.exports = function Range(str_expression, formula) {
formula.exec_formula(formula_ref);
}
if (sheet[cell_name].t === 'e') {
row.push(sheet[cell_name]);
row.push(new Error(sheet[cell_name].w));
}
else {
row.push(sheet[cell_name].v);
}
}
else if (sheet[cell_name]) {
if (sheet[cell_name].t === 'e') {
row.push(sheet[cell_name]);
row.push(new Error(sheet[cell_name].w));
}
else {
row.push(sheet[cell_name].v);
Expand Down
12 changes: 6 additions & 6 deletions src/formulas.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,23 +170,23 @@ function sumproduct() {
if (Array.isArray(col)) {
for (var k = 0; k < col.length; k++) {
var cell = col[k];
if (cell && typeof cell === 'object' && cell.t === 'e') {
throw Error(cell.w);
if (cell instanceof Error) {
throw cell;
}
}
}
else {
var cell = col;
if (cell && typeof cell === 'object' && cell.t === 'e') {
throw Error(cell.w);
if (cell instanceof Error) {
throw cell;
}
}
}
}
else {
var cell = row;
if (cell && typeof cell === 'object' && cell.t === 'e') {
throw Error(cell.w);
if (cell instanceof Error) {
throw cell;
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions test/1-basic-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -777,7 +777,7 @@ describe('XLSX_CALC', function() {
assert.equal(workbook.Sheets.Sheet1.B3.t, 'n');
assert.equal(workbook.Sheets.Sheet1.B3.v, 2);
});
it('should preserve error state when a cell is in error', function () {
it('should contain error when a cell is in error', function () {
workbook.Sheets.Sheet1.A1 = { t: "e", v: 42, w: "#N/A" };
workbook.Sheets.Sheet1.A2 = { t: 'n', v: 1 };
workbook.Sheets.Sheet1.A3 = { t: 'n', v: 2 };
Expand All @@ -787,7 +787,7 @@ describe('XLSX_CALC', function() {
var formula = find_all_cells_with_formulas(workbook, exec_formula)[0];
var range = exec_formula.build_expression(formula).args[0].calc();
var expected = [
[{ t: "e", v: 42, w: "#N/A" }],
[new Error('#N/A')],
[1],
[2]
];
Expand Down
10 changes: 10 additions & 0 deletions test/5-formulajs-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,15 @@ describe('formulajs integration', function() {
XLSX_CALC(workbook);
assert.strictEqual(workbook.Sheets.Sheet1.A2.w, '#DIV/0!');
});
it('handles range that contains error', function () {
XLSX_CALC.import_functions(formulajs);
var workbook = {};
workbook.Sheets = {};
workbook.Sheets.Sheet1 = {};
workbook.Sheets.Sheet1.A2 = {f: 'AVERAGE(A1)'};
workbook.Sheets.Sheet1.A3 = {f: 'INDEX(A1:A2, 2, 0)'};
XLSX_CALC(workbook);
assert.strictEqual(workbook.Sheets.Sheet1.A3.w, '#DIV/0!');
});
})
});

0 comments on commit d425a55

Please sign in to comment.