Commit 79673cc0 authored by Alexander.Trofimov's avatar Alexander.Trofimov

update values pivot table after change

parent c2d5a528
......@@ -1705,6 +1705,9 @@ CT_PivotCacheDefinition.prototype.toXml = function(writer) {
CT_PivotCacheDefinition.prototype.getFields = function () {
return this.cacheFields && this.cacheFields.cacheField;
};
CT_PivotCacheDefinition.prototype.getRecords = function () {
return this.cacheRecords && this.cacheRecords.r;
};
function CT_PivotCacheRecords() {
//Attributes
this.count = null;
......@@ -2697,6 +2700,31 @@ CT_pivotTableDefinition.prototype.getRowItems = function () {
CT_pivotTableDefinition.prototype.getColItems = function () {
return this.colItems && this.colItems.i;
};
CT_pivotTableDefinition.prototype.getRecords = function () {
return this.cacheDefinition.getRecords();
};
CT_pivotTableDefinition.prototype.getValues = function (records, index, value) {
var res = [];
var elem;
for (var i = 0; i < records.length; ++i) {
elem = records[i][index];
if (elem && 0 === elem.type && value === elem.v) {
res.push(records[i]);
}
}
return res;
};
CT_pivotTableDefinition.prototype.getValue = function (records, index) {
var res = 0;
var elem;
for (var i = 0; i < records.length; ++i) {
elem = records[i][index];
if (elem) {
res += elem.v;
}
}
return res;
};
CT_pivotTableDefinition.prototype.asc_getName = function () {
return this.name;
};
......@@ -4037,6 +4065,7 @@ function CT_Index() {
//Attributes
this.v = null;
}
CT_Index.prototype.type = 0;
CT_Index.prototype.readAttributes = function(attr, uq) {
if (attr()) {
var vals = attr();
......
......@@ -4924,17 +4924,19 @@
}
};
Worksheet.prototype._updatePivotTable = function (pivotTable, cleanRanges) {
var pos, cells, index, i, j, v, r, c1, r1, field, indexField, cacheIndex, sharedItem, item, items, setName,
oCellValue;
var pos, cells, index, i, j, k, r, c1, r1, field, indexField, cacheIndex, sharedItem, item, items, setName,
oCellValue, cacheRecord;
for (i = 0; i < cleanRanges.length; ++i) {
cleanRanges[i].cleanAll();
}
var pivotRange = pivotTable.getRange();
var cacheRecords = pivotTable.getRecords();
var cacheFields = pivotTable.asc_getCacheFields();
var pivotFields = pivotTable.asc_getPivotFields();
var pageFields = pivotTable.asc_getPageFields();
var colFields = pivotTable.asc_getColumnFields();
var rowFields = pivotTable.asc_getRowFields();
var dataFields = pivotTable.asc_getDataFields();
var rowFieldsPos = [];
for (i = 0; i < pivotTable.pageFieldsPositions.length; ++i) {
pos = pivotTable.pageFieldsPositions[i];
......@@ -4949,6 +4951,56 @@
var countC = pivotTable.getColumnFieldsCount();
var countR = pivotTable.getRowFieldsCount(true);
var countD = pivotTable.getDataFieldsCount();
var cacheValuesCol = [];
var cacheValuesRow = [];
if (countC) {
c1 = pivotRange.c1 + countR;
r1 = pivotRange.r1;
cells = this.getRange4(r1, c1);
cells.setValue('Column Labels');
++r1;
items = pivotTable.getColItems();
if (items && countC) {
for (i = 0; i < items.length; ++i) {
item = items[i];
r = item.getR();
cacheRecord = cacheRecords;
for (j = 0; j < item.x.length; ++j) {
if (AscCommonExcel.c_oAscItemType.Grand === item.t) {
field = null;
oCellValue = new AscCommonExcel.CCellValue();
oCellValue.text = 'Grand Total';
oCellValue.type = AscCommon.CellValueType.String;
} else {
indexField = colFields[r + j].asc_getIndex();
field = pivotFields[indexField];
cacheIndex = field.getItem(item.x[j].getV());
sharedItem = cacheFields[indexField].getSharedItem(cacheIndex.x);
// ToDo add other names by type
oCellValue = sharedItem.getCellValue(
AscCommonExcel.c_oAscItemType.Default === item.t ? ' Total' : null);
if (countD) {
cacheRecord = pivotTable.getValues(cacheRecord, indexField, cacheIndex.x);
}
}
cells = this.getRange4(r1 + r + j, c1 + i);
if (field && null !== field.numFmtId) {
cells.setNum(new AscCommonExcel.Num({id: field.numFmtId}));
}
cells.setValueData(new AscCommonExcel.UndoRedoData_CellValueData(null, oCellValue));
}
if (countD) {
cacheValuesCol.push(cacheRecord);
}
}
}
}
if (countR) {
c1 = pivotRange.c1;
......@@ -4992,53 +5044,34 @@
cacheIndex = field.getItem(item.x[j].getV());
sharedItem = cacheFields[indexField].getSharedItem(cacheIndex.x);
oCellValue = sharedItem.getCellValue();
if (countD) {
cacheValuesRow.push(indexField, cacheIndex.x);
}
}
cells = this.getRange4(r1 + i, rowFieldsPos[item.getR()]);
cells = this.getRange4(r1 + i, rowFieldsPos[r]);
if (field && null !== field.numFmtId) {
cells.setNum(new AscCommonExcel.Num({id: field.numFmtId}));
}
cells.setValueData(new AscCommonExcel.UndoRedoData_CellValueData(null, oCellValue));
}
}
}
}
if (countC) {
c1 = pivotRange.c1 + countR;
r1 = pivotRange.r1;
cells = this.getRange4(r1, c1);
cells.setValue('Column Labels');
++r1;
items = pivotTable.getColItems();
if (items && countC) {
for (i = 0; i < items.length; ++i) {
item = items[i];
r = item.getR();
for (j = 0; j < item.x.length; ++j) {
if (AscCommonExcel.c_oAscItemType.Grand === item.t) {
field = null;
oCellValue = new AscCommonExcel.CCellValue();
oCellValue.text = 'Grand Total';
oCellValue.type = AscCommon.CellValueType.String;
} else {
indexField = colFields[r + j].asc_getIndex();
field = pivotFields[indexField];
cacheIndex = field.getItem(item.x[j].getV());
sharedItem = cacheFields[indexField].getSharedItem(cacheIndex.x);
// ToDo add other names by type
oCellValue = sharedItem.getCellValue(
AscCommonExcel.c_oAscItemType.Default === item.t ? ' Total' : null);
}
cells = this.getRange4(r1 + r + j, c1 + i);
if (field && null !== field.numFmtId) {
cells.setNum(new AscCommonExcel.Num({id: field.numFmtId}));
if (countD && r === countR - 1) {
for (j = 0; j < cacheValuesCol.length; ++j) {
if (cacheRecord = cacheValuesCol[j]) {
for (k = 0; k < cacheValuesRow.length && 0 !== cacheRecord.length; k += 2) {
cacheRecord =
pivotTable.getValues(cacheRecord, cacheValuesRow[k], cacheValuesRow[k + 1]);
}
if (0 !== cacheRecord.length) {
cells = this.getRange4(r1 + i, rowFieldsPos[r] + 1 + j);
oCellValue = new AscCommonExcel.CCellValue();
oCellValue.number = pivotTable.getValue(cacheRecord, dataFields[0].asc_getIndex());
oCellValue.type = AscCommon.CellValueType.Number;
cells.setValueData(new AscCommonExcel.UndoRedoData_CellValueData(null, oCellValue));
}
}
}
cells.setValueData(new AscCommonExcel.UndoRedoData_CellValueData(null, oCellValue));
cacheValuesRow = [];
}
}
}
......
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