From 0fc38d3bd84b57be903788979195a7fcf4d4dbe1 Mon Sep 17 00:00:00 2001
From: Boris Kocherov <bk@raskon.ru>
Date: Wed, 6 Dec 2017 22:10:09 +0300
Subject: [PATCH] [erp5_only_office] fix parserFormula.changeOffset

---
 .../model/FormulaObjects/cubeFunctions.js.js  |  2 --
 .../model/FormulaObjects/parserFormula.js.js  | 21 ++++++++++++++-----
 2 files changed, 16 insertions(+), 7 deletions(-)

diff --git a/bt5/erp5_only_office/SkinTemplateItem/portal_skins/erp5_only_office/onlyoffice/sdkjs/cell/model/FormulaObjects/cubeFunctions.js.js b/bt5/erp5_only_office/SkinTemplateItem/portal_skins/erp5_only_office/onlyoffice/sdkjs/cell/model/FormulaObjects/cubeFunctions.js.js
index 8ded8172a0..a210ad2184 100644
--- a/bt5/erp5_only_office/SkinTemplateItem/portal_skins/erp5_only_office/onlyoffice/sdkjs/cell/model/FormulaObjects/cubeFunctions.js.js
+++ b/bt5/erp5_only_office/SkinTemplateItem/portal_skins/erp5_only_office/onlyoffice/sdkjs/cell/model/FormulaObjects/cubeFunctions.js.js
@@ -614,8 +614,6 @@
 					new_member = level[member.level_index + offset.offsetCol + offset.offsetRow];
 					if (new_member) {
 						elem.value = new_member.uname;
-					} else {
-						elem.value = "";
 					}
 				}
 			}
diff --git a/bt5/erp5_only_office/SkinTemplateItem/portal_skins/erp5_only_office/onlyoffice/sdkjs/cell/model/FormulaObjects/parserFormula.js.js b/bt5/erp5_only_office/SkinTemplateItem/portal_skins/erp5_only_office/onlyoffice/sdkjs/cell/model/FormulaObjects/parserFormula.js.js
index acf0c66417..3f3d62cf5c 100644
--- a/bt5/erp5_only_office/SkinTemplateItem/portal_skins/erp5_only_office/onlyoffice/sdkjs/cell/model/FormulaObjects/parserFormula.js.js
+++ b/bt5/erp5_only_office/SkinTemplateItem/portal_skins/erp5_only_office/onlyoffice/sdkjs/cell/model/FormulaObjects/parserFormula.js.js
@@ -5543,7 +5543,8 @@ parserFormula.prototype.setFormula = function(formula) {
 	/* Для обратной сборки функции иногда необходимо поменять ссылки на ячейки */
 	parserFormula.prototype.changeOffset = function (offset, canResize) {//offset = AscCommonExcel.CRangeOffset
 		var elemArr = [], currentElement = null, arg,
-			disable_changeOffset_run = false;
+			disable_changeOffset_run = false,
+			argumentsCount;
 		for (var i = 0; i < this.outStack.length; i++) {
 			currentElement = this.outStack[i];
 			this.changeOffsetElem(currentElement, this.outStack, i, offset, canResize);
@@ -5553,14 +5554,24 @@ parserFormula.prototype.setFormula = function(formula) {
 			if (currentElement.name == "(") {
 				continue;
 			}
+			if(currentElement.type === cElementType.specialFunctionStart){
+				continue;
+			}
+			if(currentElement.type === cElementType.specialFunctionEnd){
+				continue;
+			}
+			if("number" === typeof(currentElement)){
+				continue;
+			}
 			if (currentElement.type === cElementType.operator || currentElement.type === cElementType.func) {
-				if (elemArr.length < currentElement.getArguments()) {
+				argumentsCount = "number" === typeof(this.outStack[i - 1]) ? this.outStack[i - 1] : currentElement.argumentsCurrent;
+				if (elemArr.length < argumentsCount) {
 					disable_changeOffset_run = true;
 					continue;
 				} else {
 					if (currentElement && currentElement.changeOffsetElem) {
 						arg = [];
-						for (var ind = 0; ind < currentElement.getArguments(); ind++) {
+						for (var ind = 0; ind < argumentsCount; ind++) {
 							arg.unshift(elemArr.pop());
 						}
 						currentElement.changeOffsetElem(arg, offset);
@@ -5569,9 +5580,9 @@ parserFormula.prototype.setFormula = function(formula) {
 					elemArr.push(new cEmpty());
 				}
 			} else if (currentElement.type === cElementType.name || currentElement.type === cElementType.name3D) {
-				elemArr.push(currentElement.Calculate(null, rangeCell));
+				elemArr.push(currentElement.Calculate(null, null));
 			} else if (currentElement.type === cElementType.table) {
-				elemArr.push(currentElement.toRef(rangeCell.getBBox0()));
+				elemArr.push(currentElement.toRef(null));
 			} else {
 				elemArr.push(currentElement);
 			}
-- 
2.30.9