From 44d7103dc5d7b78ad09aa87cbab43ab417f4578f Mon Sep 17 00:00:00 2001
From: GoshaZotov <Igor.Zotov@onlyoffice.com>
Date: Wed, 11 May 2016 18:52:31 +0300
Subject: [PATCH] fix Bug #32335

---
 cell/model/WorkbookElems.js | 76 ++++++++++++++++++++++++++++++++++++-
 cell/model/autofilters.js   | 30 +++++++++------
 2 files changed, 93 insertions(+), 13 deletions(-)

diff --git a/cell/model/WorkbookElems.js b/cell/model/WorkbookElems.js
index 91d22d43d..d686f1930 100644
--- a/cell/model/WorkbookElems.js
+++ b/cell/model/WorkbookElems.js
@@ -4557,8 +4557,11 @@ TablePart.prototype.changeRef = function(col, row, bIsFirst) {
 		ref.setOffsetLast({offsetCol: col ? col : 0, offsetRow: row ? row : 0});
 	
 	this.Ref = ref;
+	
 	//event
-	this.handlers.trigger("changeRefTablePart", this.DisplayName, this.Ref);
+	var endRow = this.TotalsRowCount && this.TotalsRowCount >= 1 ? this.Ref.r2 - 1 : this.Ref.r2;
+	var refNamedRanges = Asc.Range(this.Ref.c1, this.Ref.r1, this.Ref.c2, endRow);
+	this.handlers.trigger("changeRefTablePart", this.DisplayName, refNamedRanges);
 	
 	if(this.AutoFilter)
 		this.AutoFilter.changeRef(col, row, bIsFirst);
@@ -4743,7 +4746,10 @@ TablePart.prototype.getTableRangeForFormula = function(objectParam)
 			if(endCol === null)
 				endCol = startCol;
 			
-			res = new Asc.Range(this.Ref.c1 + startCol, this.Ref.r1+1, this.Ref.c1 + endCol, this.Ref.r2);
+			var startRow = this.HeaderRowCount === null ? this.Ref.r1 + 1 : this.Ref.r1;
+			var endRow = this.TotalsRowCount > 0 ? this.Ref.r2 - 1 : this.Ref.r2;
+			
+			res = new Asc.Range(this.Ref.c1 + startCol, startRow, this.Ref.c1 + endCol, endRow);
 			break;
 		}
 	}
@@ -4831,6 +4837,7 @@ TablePart.prototype.generateTotalsRowLabel = function()
 	}
 	
 	this.TableColumns[0].generateTotalsRowLabel();
+	this.TableColumns[this.TableColumns.length - 1].generateTotalsRowFunction();
 };
 
 TablePart.prototype.changeDisplayName = function(newName)
@@ -5055,6 +5062,71 @@ TableColumn.prototype.generateTotalsRowLabel = function(){
 		this.TotalsRowLabel = "Summary";
 	}
 };
+TableColumn.prototype.generateTotalsRowFunction = function(){
+	//TODO добавить в перевод
+	if(this.TotalsRowFunction === null)
+	{	
+		this.TotalsRowFunction = Asc.ETotalsRowFunction.totalrowfunctionSum;
+	}
+};
+
+TableColumn.prototype.getTotalRowFormula = function(tablePart){
+	var res = null;
+	
+	if(null !== this.TotalsRowFunction)
+	{
+		switch(this.TotalsRowFunction)
+		{
+			case Asc.ETotalsRowFunction.totalrowfunctionAverage:
+			{
+				break;
+			}
+			case Asc.ETotalsRowFunction.totalrowfunctionCount:
+			{
+				break;
+			}
+			case Asc.ETotalsRowFunction.totalrowfunctionCountNums:
+			{
+				break;
+			}
+			case Asc.ETotalsRowFunction.totalrowfunctionCustom:
+			{
+				break;
+			}
+			case Asc.ETotalsRowFunction.totalrowfunctionMax:
+			{
+				break;
+			}
+			case Asc.ETotalsRowFunction.totalrowfunctionMin:
+			{
+				break;
+			}
+			case Asc.ETotalsRowFunction.totalrowfunctionNone:
+			{
+				break;
+			}
+			case Asc.ETotalsRowFunction.totalrowfunctionStdDev:
+			{
+				break;
+			}
+			case Asc.ETotalsRowFunction.totalrowfunctionSum:
+			{
+				res = "=SUBTOTAL(109;" + tablePart.DisplayName + "[" + this.Name + "]";
+				break;
+			}
+			case Asc.ETotalsRowFunction.totalrowfunctionVar:
+			{
+				break;
+			}
+		}
+	}
+	else if(null !== this.TotalsRowFormula)
+	{
+		res = this.TotalsRowFormula;
+	}
+	
+	return res;
+};
 
 /** @constructor */
 function TableStyleInfo() {
diff --git a/cell/model/autofilters.js b/cell/model/autofilters.js
index 324c4c8a7..ae77e083c 100644
--- a/cell/model/autofilters.js
+++ b/cell/model/autofilters.js
@@ -2162,9 +2162,9 @@
 						{
 							var clearRange = new AscCommonExcel.Range(worksheet, tablePart.Ref.r2, tablePart.Ref.c1, tablePart.Ref.r2, tablePart.Ref.c2);
 							this._clearRange(clearRange, true);
-							tablePart.changeRef(null, -1);
 							
 							tablePart.TotalsRowCount = tablePart.TotalsRowCount === null ? 1 : null;
+							tablePart.changeRef(null, -1);
 						}
 						else
 						{
@@ -2172,19 +2172,19 @@
 							var rangeUpTable = new Asc.Range(tablePart.Ref.c1, tablePart.Ref.r2 + 1, tablePart.Ref.c2, tablePart.Ref.r2 + 1); 
 							if(this._isEmptyCurrentRange(rangeUpTable) && this.searchRangeInTableParts(rangeUpTable) === -1)
 							{
-								tablePart.changeRef(null, 1);
 								isSetValue = true;
 								
 								tablePart.TotalsRowCount = tablePart.TotalsRowCount === null ? 1 : null;
+								tablePart.changeRef(null, 1);
 							}
 							else
 							{
 								worksheet.getRange3(tablePart.Ref.r2 + 1, tablePart.Ref.c1, tablePart.Ref.r2 + 1, tablePart.Ref.c2).addCellsShiftBottom();
 									
-								tablePart.changeRef(null, 1);
 								isSetValue = true;
 								
 								tablePart.TotalsRowCount = tablePart.TotalsRowCount === null ? 1 : null;
+								tablePart.changeRef(null, 1);
 							}
 							
 							if(val === true)
@@ -2201,9 +2201,9 @@
 						{
 							var clearRange = new AscCommonExcel.Range(worksheet, tablePart.Ref.r1, tablePart.Ref.c1, tablePart.Ref.r1, tablePart.Ref.c2);
 							this._clearRange(clearRange, true);
-							tablePart.changeRef(null, 1, true);
 							
 							tablePart.HeaderRowCount = tablePart.HeaderRowCount === null ? 0 : null;
+							tablePart.changeRef(null, 1, true);
 						}
 						else
 						{
@@ -2211,21 +2211,20 @@
 							var rangeUpTable = new Asc.Range(tablePart.Ref.c1, tablePart.Ref.r1 - 1, tablePart.Ref.c2, tablePart.Ref.r1 - 1); 
 							if(this._isEmptyCurrentRange(rangeUpTable) && this.searchRangeInTableParts(rangeUpTable) === -1)
 							{
-								tablePart.changeRef(null, -1, true);
 								isSetValue = true;
 								
 								tablePart.HeaderRowCount = tablePart.HeaderRowCount === null ? 0 : null;
+								tablePart.changeRef(null, -1, true);
 							}
 							else
 							{
 								worksheet.getRange3(tablePart.Ref.r2 + 1, tablePart.Ref.c1, tablePart.Ref.r2 + 1, tablePart.Ref.c2).addCellsShiftBottom();
 								worksheet._moveRange(tablePart.Ref,  new Asc.Range(tablePart.Ref.c1, tablePart.Ref.r1 + 1, tablePart.Ref.c2, tablePart.Ref.r2 + 1));
 									
-								tablePart.changeRef(null, -1, true);
 								isSetValue = true;
 								
-								
 								tablePart.HeaderRowCount = tablePart.HeaderRowCount === null ? 0 : null;
+								tablePart.changeRef(null, -1, true);
 							}
 						}
 						
@@ -3920,12 +3919,21 @@
 									range.setType(CellValueType.String);
 								}
 								
-								if(tableColumn !== null && tableColumn.TotalsRowLabel !== null && totalsRowCount > 0)
+								if(tableColumn !== null && totalsRowCount > 0)
 								{
 									range = worksheet.getCell3(bbox.r2, ncol);
-									range.setValue(tableColumn.TotalsRowLabel);
-									range.setType(CellValueType.String);
-									//TODO + далее необходимо добавлять формулу 
+									
+									if(null !== tableColumn.TotalsRowLabel)
+									{
+										range.setValue(tableColumn.TotalsRowLabel);
+										range.setType(CellValueType.String);
+									}
+									
+									var formula = tableColumn.getTotalRowFormula(options);
+									if(null !== formula)
+									{
+										range.setValue(formula);
+									}
 								}
 							}
 						}
-- 
2.30.9