Commit ea006163 authored by Alexander.Trofimov's avatar Alexander.Trofimov

fix bug 30804

add support AVERAGEIFS function
parent 3a458862
......@@ -4099,9 +4099,8 @@ cSQRTPI.prototype.getInfo = function () {
}
}
var _sum = 0;
var arg0Matrix = arg0.getMatrix();
var i, j, arg1, arg2, valueForSearching, match, search, oper = null;
var i, j, arg1, arg2, valueForSearching, match, search, oper;
for (var k = 1; k < arg.length; k += 2) {
arg1 = arg[k];
arg2 = arg[k + 1];
......@@ -4156,6 +4155,7 @@ cSQRTPI.prototype.getInfo = function () {
}
}
var _sum = 0;
var valMatrix0;
for (i = 0; i < arg0Matrix.length; ++i) {
for (j = 0; j < arg0Matrix[i].length; ++j) {
......
......@@ -639,10 +639,104 @@
/** @constructor */
function cAVERAGEIFS() {
cBaseFunction.call(this, "AVERAGEIFS");
this.name = "AVERAGEIFS";
this.type = cElementType.func;
this.value = null;
this.argumentsMin = 3;
this.argumentsCurrent = 0;
this.argumentsMax = 255;
this.formatType = {
def: -1, //подразумевается формат первой ячейки входящей в формулу.
noneFormat: -2
};
this.numFormat = this.formatType.def;
}
cAVERAGEIFS.prototype = Object.create(cBaseFunction.prototype);
cAVERAGEIFS.prototype.Calculate = function (arg) {
var arg0 = arg[0];
if (cElementType.cell !== arg0.type && cElementType.cell3D !== arg0.type &&
cElementType.cellsRange !== arg0.type) {
return this.value = new cError(cErrorType.wrong_value_type);
}
var arg0Matrix = arg0.getMatrix();
var i, j, arg1, arg2, operators, match, search, oper, valueForSearching;
for (var k = 1; k < arg.length; k += 2) {
arg1 = arg[k];
arg2 = arg[k + 1];
if ((cElementType.cell !== arg1.type && cElementType.cell3D !== arg1.type &&
cElementType.cellsRange !== arg1.type)) {
return this.value = new cError(cErrorType.wrong_value_type);
}
if (cElementType.cellsRange === arg2.type || cElementType.cellsRange3D === arg2.type) {
arg2 = arg2.cross(arguments[1].bbox);
} else if (cElementType.array === arg2.type) {
arg2 = arg2.getElementRowCol(0, 0);
}
arg2 = arg2.tocString();
if (cElementType.string !== arg2.type) {
return this.value = new cError(cErrorType.wrong_value_type);
}
arg2 = arg2.toString();
operators = new RegExp("^ *[<=> ]+ *");
match = arg2.match(operators);
if (match) {
search = arg2.substr(match[0].length);
oper = match[0].replace(/\s/g, "");
} else {
search = arg2;
oper = null;
}
valueForSearching = AscCommonExcel.matchingValue(search);
var arg1Matrix = arg1.getMatrix();
if (arg0Matrix.length !== arg1Matrix.length) {
return this.value = new cError(cErrorType.wrong_value_type);
}
for (i = 0; i < arg1Matrix.length; ++i) {
if (arg0Matrix[i].length !== arg1Matrix[i].length) {
return this.value = new cError(cErrorType.wrong_value_type);
}
for (j = 0; j < arg1Matrix[i].length; ++j) {
if (arg0Matrix[i][j] && !AscCommonExcel.matching(arg1Matrix[i][j], valueForSearching, oper)) {
arg0Matrix[i][j] = null;
}
}
}
}
var _sum = 0, _count = 0;
var valMatrix0;
for (i = 0; i < arg0Matrix.length; ++i) {
for (j = 0; j < arg0Matrix[i].length; ++j) {
if ((valMatrix0 = arg0Matrix[i][j]) && cElementType.number === valMatrix0.type) {
_sum += valMatrix0.getValue();
++_count;
}
}
}
if (0 === _count) {
return new cError(cErrorType.division_by_zero);
} else {
return this.value = new cNumber(_sum / _count);
}
};
cAVERAGEIFS.prototype.checkArguments = function () {
return 1 === this.argumentsCurrent % 2 && cBaseFunction.prototype.checkArguments.apply(this, arguments);
};
cAVERAGEIFS.prototype.getInfo = function () {
return {
name: this.name, args: "(average_range, criteria_range1, criteria1, [criteria_range2, criteria2], ...)"
};
};
/** @constructor */
function cBETADIST() {/*Нет реализации в Google Docs*/
......@@ -1166,12 +1260,12 @@
arg1 = arg1.toString();
match = arg1.match(operators);
oper = null;
if (match) {
search = arg1.substr(match[0].length);
oper = match[0].replace(/\s/g, "");
} else {
search = arg1;
oper = null;
}
valueForSearching = AscCommonExcel.matchingValue(search);
arg1Matrix = arg0.getMatrix();
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment