Skip to content

Commit

Permalink
fix: average #93
Browse files Browse the repository at this point in the history
  • Loading branch information
fabiooshiro committed Nov 2, 2022
1 parent 0d4c46f commit ae64d1f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
6 changes: 5 additions & 1 deletion src/formulas.js
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,11 @@ function EXP(n) {
}

function avg() {
return sum.apply(this, arguments) / counta.apply(this, arguments);
var aux = counta.apply(this, arguments);
if (aux == 0) {
throw Error('#DIV/0!');
}
return sum.apply(this, arguments) / aux;
}

function stDeviation() {
Expand Down
15 changes: 13 additions & 2 deletions test/1-basic-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ const errorValues = {

describe('XLSX_CALC', function() {
let workbook;
beforeEach(function() {
workbook = {
function create_workbook() {
return {
Sheets: {
Sheet1: {
A1: {},
Expand All @@ -38,6 +38,9 @@ describe('XLSX_CALC', function() {
}
}
};
}
beforeEach(function() {
workbook = create_workbook();
});

describe('ROUND', () => {
Expand Down Expand Up @@ -1023,6 +1026,14 @@ describe('XLSX_CALC', function() {
XLSX_CALC(workbook);
assert.equal(workbook.Sheets.Sheet1.A7.v, 0.25);
});
it('should calc AVERAGE of empty cells as div by zero', function() {
workbook.Sheets.Sheet1 = {};
workbook.Sheets.Sheet1.B1 = {f: 'AVERAGE(A1:A6)'};
XLSX_CALC(workbook);
assert.equal(workbook.Sheets.Sheet1.B1.v, errorValues["#DIV/0!"]);
assert.equal(workbook.Sheets.Sheet1.B1.w, '#DIV/0!');
assert.equal(workbook.Sheets.Sheet1.B1.t, 'e');
});
});
describe('IRR', function() {
it('calcs IRR', function() {
Expand Down

0 comments on commit ae64d1f

Please sign in to comment.