Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11,098 changes: 5,565 additions & 5,533 deletions dist/formula.js

Large diffs are not rendered by default.

7,746 changes: 7,742 additions & 4 deletions dist/formula.min.js

Large diffs are not rendered by default.

12 changes: 12 additions & 0 deletions lib/database.js
Original file line number Diff line number Diff line change
Expand Up @@ -332,3 +332,15 @@ exports.DVARP = function(database, field, criteria) {
}
return stats.VAR.P(targetValues);
};

exports.VLOOKUP = function(value, table, col_index, range_lookup) {
if (range_lookup !== undefined) {
return error.value; // not implemented
}
for (row = 0; row < table[0].length; row++) {
if (table[0][row] === value) {
return table[col_index - 1][row];
}
}
return false;
};
32 changes: 24 additions & 8 deletions lib/math-trig.js
Original file line number Diff line number Diff line change
Expand Up @@ -927,14 +927,22 @@ exports.SUM = function() {
return result;
};

exports.SUMIF = function(range, criteria) {
range = utils.parseNumberArray(utils.flatten(range));
if (range instanceof Error) {
return range;
exports.SUMIF = function(range, criteria, sum_range) {
range = utils.flatten(range);
sum_range = sum_range || range;
sum_range = utils.parseNumberArray(utils.flatten(sum_range));
if (sum_range instanceof Error) {
return sum_range;
}

criteria = utils.parseCriteria(criteria)

var result = 0;
for (var i = 0; i < range.length; i++) {
result += (eval(range[i] + criteria)) ? range[i] : 0; // jshint ignore:line
if (typeof range[i] === 'string' &&! /^\".*\"$/.test(range[i])) {
range[i] = '"' + range[i] + '"';
}
result += (eval(range[i] + criteria)) ? sum_range[i] : 0; // jshint ignore:line
}
return result;
};
Expand All @@ -955,11 +963,14 @@ exports.SUMIFS = function() {
var el = range[i];
var condition = '';
for (var c = 0; c < n_criterias; c++) {
condition += el + criteria[c];
let currentCriteria = criteria[c]
currentCriteria = utils.parseCriteria(currentCriteria)
condition += el + currentCriteria;

if (c !== n_criterias - 1) {
condition += '&&';
}
}
}
if (eval(condition)) { // jshint ignore:line
result += el;
}
Expand All @@ -981,7 +992,12 @@ exports.SUMPRODUCT = function() {
if (!(arguments[0][i] instanceof Array)) {
product = 1;
for (k = 1; k < arrays; k++) {
_i = utils.parseNumber(arguments[k - 1][i]);
if (arguments[k - 1][i] === false) {
_i = false;
}
else {
_i = utils.parseNumber(arguments[k - 1][i]);
}
if (_i instanceof Error) {
return _i;
}
Expand Down
3 changes: 3 additions & 0 deletions lib/statistical.js
Original file line number Diff line number Diff line change
Expand Up @@ -1070,6 +1070,9 @@ exports.NORM.INV = function(probability, mean, sd) {
exports.NORM.S = {};

exports.NORM.S.DIST = function(z, cumulative) {
if (cumulative === undefined) {
cumulative = true;
}
z = utils.parseNumber(z);
if (z instanceof Error) {
return error.value;
Expand Down
20 changes: 19 additions & 1 deletion lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,4 +215,22 @@ exports.initial = function(array, idx) {
return array;
}
return array.slice(0, array.length - idx);
};
};

exports.parseCriteria = function (criteria) {

if (!/[<>=!]/.test(criteria)) {
criteria = '==' + criteria;
}
if (/=[^=]/.test(criteria)) {
criteria = '=' + criteria;
}
if (/=+(.*)/.test(criteria)) {
var elt = criteria.match(/=+(.*)/)[1];
if (this.parseNumber(elt) == error.value && !/"(.*)"/.test(criteria)) {
criteria = '=="' + elt + '"';
}
}

return criteria
}
4 changes: 4 additions & 0 deletions test/database.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,8 @@ suite('Database', function() {
database.DVARP([['Tree','Apple','Pear','Cherry','Apple','Pear','Apple'],['Height',18,12,13,14,9,12],['Age',20,12,14,16,8,11],['Yield',14,10,9,10,8,6]], undefined, [['Height','>10'],['Age','>14']]).should.equal(error.value);
database.DVARP([['Tree','Apple','Pear','Cherry','Apple','Pear','Apple'],['Height',18,12,13,14,9,12],['Age',20,12,14,16,8,11],['Yield',14,10,9,10,8,6]], 3, [['Height','>10'],['Age','>14']]).should.equal(4);
});

test('VLOOKUP', function() {
database.VLOOKUP("B", [["A","B","C"],[5,7,3]], 2);
});
});
9 changes: 9 additions & 0 deletions test/math-trig.js
Original file line number Diff line number Diff line change
Expand Up @@ -700,6 +700,10 @@ suite('Math & Trig', function() {

test("SUMIF", function() {
mathTrig.SUMIF([1, 2, 3], '>2').should.equal(3);
mathTrig.SUMIF([1, 2, 3], '=2').should.equal(2);
mathTrig.SUMIF([1, 2, 3], '>2', [4, 5, 6]).should.equal(6);
mathTrig.SUMIF(['"A"', '"B"', '"B"'], '"B"', [1, 2, 3]).should.equal(5);
mathTrig.SUMIF(['A', 'B', 'B'], '=B', [1, 2, 3]).should.equal(5);
mathTrig.SUMIF([
[1, 1],
[2, 2],
Expand Down Expand Up @@ -759,6 +763,11 @@ suite('Math & Trig', function() {
[5, 3]
]).should.equal(error.value);

mathTrig.SUMPRODUCT(
[1, 2, false],
[4, 5, false]
).should.equal(14);

mathTrig.SUMPRODUCT([8, 'invalid'], [5, 3]).should.equal(error.value);
mathTrig.SUMPRODUCT().should.equal(error.value);
});
Expand Down
1 change: 1 addition & 0 deletions test/statistical.js
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,7 @@ suite('Statistical', function() {

test('NORM.S.DIST', function() {
statistical.NORM.S.DIST(1, true).should.approximately(0.8413447460685429, 1e-9);
statistical.NORM.S.DIST(1).should.approximately(0.8413447460685429, 1e-9);
statistical.NORM.S.DIST(1, false).should.approximately(0.24197072451914337, 1e-9);
statistical.NORM.S.DIST('invalid', false).should.equal(error.value);
});
Expand Down