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

move matchInfo to matchingValue function

parent cdb75f73
...@@ -3996,13 +3996,11 @@ cSQRTPI.prototype.getInfo = function () { ...@@ -3996,13 +3996,11 @@ cSQRTPI.prototype.getInfo = function () {
noneFormat: -2 noneFormat: -2
}; };
this.numFormat = this.formatType.def; this.numFormat = this.formatType.def;
this.operatorRE = new RegExp("^ *[<=> ]+ *");
this.spaceRE = /\s/g;
} }
cSUMIF.prototype = Object.create(cBaseFunction.prototype); cSUMIF.prototype = Object.create(cBaseFunction.prototype);
cSUMIF.prototype.Calculate = function (arg) { cSUMIF.prototype.Calculate = function (arg) {
var arg0 = arg[0], arg1 = arg[1], arg2 = arg[2] ? arg[2] : arg[0], _sum = 0, valueForSearching; var arg0 = arg[0], arg1 = arg[1], arg2 = arg[2] ? arg[2] : arg[0], _sum = 0, matchingInfo;
if (cElementType.cell !== arg0.type && cElementType.cell3D !== arg0.type && cElementType.cellsRange !== arg0.type) { if (cElementType.cell !== arg0.type && cElementType.cell3D !== arg0.type && cElementType.cellsRange !== arg0.type) {
if (cElementType.cellsRange3D === arg0.type) { if (cElementType.cellsRange3D === arg0.type) {
arg0 = arg0.tocArea(); arg0 = arg0.tocArea();
...@@ -4037,21 +4035,13 @@ cSQRTPI.prototype.getInfo = function () { ...@@ -4037,21 +4035,13 @@ cSQRTPI.prototype.getInfo = function () {
return this.value = new cError(cErrorType.wrong_value_type); return this.value = new cError(cErrorType.wrong_value_type);
} }
arg1 = arg1.toString(); matchingInfo = AscCommonExcel.matchingValue(arg1.toString());
var match = arg1.match(this.operatorRE), search, oper;
if (match) {
search = arg1.substr(match[0].length);
oper = match[0].replace(this.spaceRE, "");
} else {
search = arg1;
}
valueForSearching = AscCommonExcel.matchingValue(search);
if (cElementType.cellsRange === arg0.type) { if (cElementType.cellsRange === arg0.type) {
var arg0Matrix = arg0.getMatrix(), arg2Matrix = arg2.getMatrix(), valMatrix2; var arg0Matrix = arg0.getMatrix(), arg2Matrix = arg2.getMatrix(), valMatrix2;
for (var i = 0; i < arg0Matrix.length; i++) { for (var i = 0; i < arg0Matrix.length; i++) {
for (var j = 0; j < arg0Matrix[i].length; j++) { for (var j = 0; j < arg0Matrix[i].length; j++) {
if (arg2Matrix[i] && (valMatrix2 = arg2Matrix[i][j]) && cElementType.number === valMatrix2.type && if (arg2Matrix[i] && (valMatrix2 = arg2Matrix[i][j]) && cElementType.number === valMatrix2.type &&
AscCommonExcel.matching(arg0Matrix[i][j], valueForSearching, oper)) { AscCommonExcel.matching(arg0Matrix[i][j], matchingInfo)) {
_sum += valMatrix2.getValue(); _sum += valMatrix2.getValue();
} }
} }
...@@ -4081,8 +4071,6 @@ cSQRTPI.prototype.getInfo = function () { ...@@ -4081,8 +4071,6 @@ cSQRTPI.prototype.getInfo = function () {
noneFormat: -2 noneFormat: -2
}; };
this.numFormat = this.formatType.def; this.numFormat = this.formatType.def;
this.operatorRE = new RegExp("^ *[<=> ]+ *");
this.spaceRE = /\s/g;
} }
cSUMIFS.prototype = Object.create(cBaseFunction.prototype); cSUMIFS.prototype = Object.create(cBaseFunction.prototype);
...@@ -4100,7 +4088,7 @@ cSQRTPI.prototype.getInfo = function () { ...@@ -4100,7 +4088,7 @@ cSQRTPI.prototype.getInfo = function () {
} }
var arg0Matrix = arg0.getMatrix(); var arg0Matrix = arg0.getMatrix();
var i, j, arg1, arg2, valueForSearching, match, search, oper; var i, j, arg1, arg2, matchingInfo;
for (var k = 1; k < arg.length; k += 2) { for (var k = 1; k < arg.length; k += 2) {
arg1 = arg[k]; arg1 = arg[k];
arg2 = arg[k + 1]; arg2 = arg[k + 1];
...@@ -4128,16 +4116,7 @@ cSQRTPI.prototype.getInfo = function () { ...@@ -4128,16 +4116,7 @@ cSQRTPI.prototype.getInfo = function () {
return this.value = new cError(cErrorType.wrong_value_type); return this.value = new cError(cErrorType.wrong_value_type);
} }
arg2 = arg2.toString(); matchingInfo = AscCommonExcel.matchingValue(arg2.toString());
match = arg2.match(this.operatorRE);
if (match) {
search = arg2.substr(match[0].length);
oper = match[0].replace(this.spaceRE, "");
} else {
search = arg2;
oper = null;
}
valueForSearching = AscCommonExcel.matchingValue(search);
var arg1Matrix = arg1.getMatrix(); var arg1Matrix = arg1.getMatrix();
if (arg0Matrix.length !== arg1Matrix.length) { if (arg0Matrix.length !== arg1Matrix.length) {
...@@ -4148,7 +4127,7 @@ cSQRTPI.prototype.getInfo = function () { ...@@ -4148,7 +4127,7 @@ cSQRTPI.prototype.getInfo = function () {
return this.value = new cError(cErrorType.wrong_value_type); return this.value = new cError(cErrorType.wrong_value_type);
} }
for (j = 0; j < arg1Matrix[i].length; ++j) { for (j = 0; j < arg1Matrix[i].length; ++j) {
if (arg0Matrix[i][j] && !AscCommonExcel.matching(arg1Matrix[i][j], valueForSearching, oper)) { if (arg0Matrix[i][j] && !AscCommonExcel.matching(arg1Matrix[i][j], matchingInfo)) {
arg0Matrix[i][j] = null; arg0Matrix[i][j] = null;
} }
} }
......
...@@ -5364,11 +5364,25 @@ function parseNum( str ) { ...@@ -5364,11 +5364,25 @@ function parseNum( str ) {
return !isNaN( str ); return !isNaN( str );
} }
function matchingValue(search) { var matchingOperators = new RegExp("^ *[<=> ]+ *");
function matchingValue(val) {
var search, op;
var match = val.match(matchingOperators);
if (match) {
search = val.substr(match[0].length);
op = match[0].replace(/\s/g, "");
} else {
search = val;
op = null;
}
var parseRes = AscCommon.g_oFormatParser.parse(search); var parseRes = AscCommon.g_oFormatParser.parse(search);
return parseRes ? new cNumber(parseRes.value) : new cString(search); return {val: parseRes ? new cNumber(parseRes.value) : new cString(search), op: op};
} }
function matching(x, y, operator) { function matching(x, matchingInfo) {
var y = matchingInfo.val;
var operator = matchingInfo.op;
var res = false, rS; var res = false, rS;
if (cElementType.string === y.type) { if (cElementType.string === y.type) {
if ('<' === operator || '>' === operator || '<=' === operator || '>=' === operator) { if ('<' === operator || '>' === operator || '<=' === operator || '>=' === operator) {
......
...@@ -560,7 +560,7 @@ ...@@ -560,7 +560,7 @@
cAVERAGEIF.prototype = Object.create(cBaseFunction.prototype); cAVERAGEIF.prototype = Object.create(cBaseFunction.prototype);
cAVERAGEIF.prototype.Calculate = function (arg) { cAVERAGEIF.prototype.Calculate = function (arg) {
var arg0 = arg[0], arg1 = arg[1], arg2 = arg[2] ? arg[2] : arg[0], _sum = 0, _count = 0, valueForSearching, ws; var arg0 = arg[0], arg1 = arg[1], arg2 = arg[2] ? arg[2] : arg[0], _sum = 0, _count = 0, matchingInfo, ws;
if ((cElementType.cell !== arg0.type && cElementType.cell3D !== arg0.type && if ((cElementType.cell !== arg0.type && cElementType.cell3D !== arg0.type &&
cElementType.cellsRange !== arg0.type) || cElementType.cellsRange !== arg0.type) ||
(cElementType.cell !== arg2.type && cElementType.cell3D !== arg2.type && (cElementType.cell !== arg2.type && cElementType.cell3D !== arg2.type &&
...@@ -581,19 +581,13 @@ ...@@ -581,19 +581,13 @@
} }
arg1 = arg1.toString(); arg1 = arg1.toString();
var operators = new RegExp("^ *[<=> ]+ *"), match = arg1.match(operators), search, oper, val; var val;
if (match) { matchingInfo = AscCommonExcel.matchingValue(arg1.toString());
search = arg1.substr(match[0].length);
oper = match[0].replace(/\s/g, "");
} else {
search = arg1;
}
valueForSearching = AscCommonExcel.matchingValue(search);
if (cElementType.cellsRange === arg0.type) { if (cElementType.cellsRange === arg0.type) {
ws = arg0.getWS(); ws = arg0.getWS();
var tmpCellArg0 = arg0.getRange().getCells()[0], tmpCellArg2 = arg2.getRange(), offset, bbox, r2; var tmpCellArg0 = arg0.getRange().getCells()[0], tmpCellArg2 = arg2.getRange(), offset, bbox, r2;
arg0.foreach2(function (v, cell) { arg0.foreach2(function (v, cell) {
if (matching(v, valueForSearching, oper)) { if (matching(v, matchingInfo)) {
offset = cell.getOffset(tmpCellArg0); offset = cell.getOffset(tmpCellArg0);
tmpCellArg2 = arg2.getRange(); tmpCellArg2 = arg2.getRange();
tmpCellArg2.setOffset(offset); tmpCellArg2.setOffset(offset);
...@@ -613,7 +607,7 @@ ...@@ -613,7 +607,7 @@
}) })
} else { } else {
val = arg0.getValue(); val = arg0.getValue();
if (matching(val, valueForSearching, oper)) { if (matching(val, matchingInfo)) {
var r = arg0.getRange(); var r = arg0.getRange();
ws = arg0.getWS(); ws = arg0.getWS();
var r1 = r.bbox.r1, c1 = arg2.getRange().bbox.c1; var r1 = r.bbox.r1, c1 = arg2.getRange().bbox.c1;
...@@ -661,7 +655,7 @@ ...@@ -661,7 +655,7 @@
} }
var arg0Matrix = arg0.getMatrix(); var arg0Matrix = arg0.getMatrix();
var i, j, arg1, arg2, operators, match, search, oper, valueForSearching; var i, j, arg1, arg2, matchingInfo;
for (var k = 1; k < arg.length; k += 2) { for (var k = 1; k < arg.length; k += 2) {
arg1 = arg[k]; arg1 = arg[k];
arg2 = arg[k + 1]; arg2 = arg[k + 1];
...@@ -681,18 +675,7 @@ ...@@ -681,18 +675,7 @@
if (cElementType.string !== arg2.type) { if (cElementType.string !== arg2.type) {
return this.value = new cError(cErrorType.wrong_value_type); return this.value = new cError(cErrorType.wrong_value_type);
} }
matchingInfo = AscCommonExcel.matchingValue(arg2.toString());
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(); var arg1Matrix = arg1.getMatrix();
if (arg0Matrix.length !== arg1Matrix.length) { if (arg0Matrix.length !== arg1Matrix.length) {
...@@ -704,7 +687,7 @@ ...@@ -704,7 +687,7 @@
return this.value = new cError(cErrorType.wrong_value_type); return this.value = new cError(cErrorType.wrong_value_type);
} }
for (j = 0; j < arg1Matrix[i].length; ++j) { for (j = 0; j < arg1Matrix[i].length; ++j) {
if (arg0Matrix[i][j] && !AscCommonExcel.matching(arg1Matrix[i][j], valueForSearching, oper)) { if (arg0Matrix[i][j] && !AscCommonExcel.matching(arg1Matrix[i][j], matchingInfo)) {
arg0Matrix[i][j] = null; arg0Matrix[i][j] = null;
} }
} }
...@@ -1172,7 +1155,7 @@ ...@@ -1172,7 +1155,7 @@
cCOUNTIF.prototype = Object.create(cBaseFunction.prototype); cCOUNTIF.prototype = Object.create(cBaseFunction.prototype);
cCOUNTIF.prototype.Calculate = function (arg) { cCOUNTIF.prototype.Calculate = function (arg) {
var arg0 = arg[0], arg1 = arg[1], _count = 0, valueForSearching; var arg0 = arg[0], arg1 = arg[1], _count = 0, matchingInfo;
if (cElementType.cell !== arg0.type && cElementType.cell3D !== arg0.type && cElementType.cellsRange !== arg0.type && cElementType.cellsRange3D !== arg0.type) { if (cElementType.cell !== arg0.type && cElementType.cell3D !== arg0.type && cElementType.cellsRange !== arg0.type && cElementType.cellsRange3D !== arg0.type) {
return this.value = new cError(cErrorType.wrong_value_type); return this.value = new cError(cErrorType.wrong_value_type);
} }
...@@ -1189,28 +1172,20 @@ ...@@ -1189,28 +1172,20 @@
return this.value = new cError(cErrorType.wrong_value_type); return this.value = new cError(cErrorType.wrong_value_type);
} }
arg1 = arg1.toString(); var val;
var operators = new RegExp("^ *[<=> ]+ *"), search, oper, val, match = arg1.match(operators); matchingInfo = AscCommonExcel.matchingValue(arg1.toString());
if (match) {
search = arg1.substr(match[0].length);
oper = match[0].replace(/\s/g, "");
} else {
search = arg1;
}
valueForSearching = AscCommonExcel.matchingValue(search);
if (cElementType.cellsRange === arg0.type) { if (cElementType.cellsRange === arg0.type) {
arg0.foreach2(function (_val) { arg0.foreach2(function (_val) {
_count += matching(_val, valueForSearching, oper); _count += matching(_val, matchingInfo);
}) })
} else if (cElementType.cellsRange3D === arg0.type) { } else if (cElementType.cellsRange3D === arg0.type) {
val = arg0.getValue(); val = arg0.getValue();
for (var i = 0; i < val.length; i++) { for (var i = 0; i < val.length; i++) {
_count += matching(val[i], valueForSearching, oper); _count += matching(val[i], matchingInfo);
} }
} else { } else {
val = arg0.getValue(); val = arg0.getValue();
_count += matching(val, valueForSearching, oper); _count += matching(val, matchingInfo);
} }
return this.value = new cNumber(_count); return this.value = new cNumber(_count);
...@@ -1238,8 +1213,7 @@ ...@@ -1238,8 +1213,7 @@
cCOUNTIFS.prototype = Object.create(cBaseFunction.prototype); cCOUNTIFS.prototype = Object.create(cBaseFunction.prototype);
cCOUNTIFS.prototype.Calculate = function (arg) { cCOUNTIFS.prototype.Calculate = function (arg) {
var i, j, arg0, arg1, valueForSearching, arg0Matrix, arg1Matrix, _count = 0; var i, j, arg0, arg1, matchingInfo, arg0Matrix, arg1Matrix, _count = 0;
var search, oper, match, operators = new RegExp("^ *[<=> ]+ *");
for (var k = 0; k < arg.length; k += 2) { for (var k = 0; k < arg.length; k += 2) {
arg0 = arg[k]; arg0 = arg[k];
arg1 = arg[k + 1]; arg1 = arg[k + 1];
...@@ -1258,16 +1232,7 @@ ...@@ -1258,16 +1232,7 @@
return this.value = new cError(cErrorType.wrong_value_type); return this.value = new cError(cErrorType.wrong_value_type);
} }
arg1 = arg1.toString(); matchingInfo = AscCommonExcel.matchingValue(arg1.toString());
match = arg1.match(operators);
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(); arg1Matrix = arg0.getMatrix();
if (!arg0Matrix) { if (!arg0Matrix) {
arg0Matrix = arg1Matrix; arg0Matrix = arg1Matrix;
...@@ -1280,7 +1245,7 @@ ...@@ -1280,7 +1245,7 @@
return this.value = new cError(cErrorType.wrong_value_type); return this.value = new cError(cErrorType.wrong_value_type);
} }
for (j = 0; j < arg1Matrix[i].length; ++j) { for (j = 0; j < arg1Matrix[i].length; ++j) {
if (arg0Matrix[i][j] && !matching(arg1Matrix[i][j], valueForSearching, oper)) { if (arg0Matrix[i][j] && !matching(arg1Matrix[i][j], matchingInfo)) {
arg0Matrix[i][j] = null; arg0Matrix[i][j] = null;
} }
} }
......
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