Commit 1f731062 authored by Sergey Konovalov's avatar Sergey Konovalov

relative references in def names; getRangeByRef activeCell; table convertToRange errors;

parent 14b892ad
......@@ -615,7 +615,7 @@
parseReference();
if (found_operand) {
if (cElementType.name === found_operand.type) {
found_operand = found_operand.toRef();
found_operand = found_operand.toRef(arguments[1]);
}
ret = found_operand;
......
......@@ -632,13 +632,11 @@ parserHelp.setDigitSeparator(AscCommon.g_oDefaultCultureInfo.NumberDecimalSepara
/** @constructor */
function cBaseType(val) {
this.needRecalc = false;
this.numFormat = null;
this.value = val;
}
cBaseType.prototype.cloneTo = function (oRes) {
oRes.needRecalc = this.needRecalc;
oRes.numFormat = this.numFormat;
oRes.value = this.value;
};
......@@ -1548,12 +1546,12 @@ parserHelp.setDigitSeparator(AscCommon.g_oDefaultCultureInfo.NumberDecimalSepara
this.cloneTo(oRes);
return oRes;
};
cName.prototype.toRef = function () {
cName.prototype.toRef = function (opt_bbox) {
var defName = this.getDefName();
if (!defName || !defName.ref) {
return new cError(cErrorType.wrong_name);
}
return this.Calculate();
return this.Calculate(undefined, opt_bbox);
};
cName.prototype.toString = function () {
var defName = this.getDefName();
......@@ -1576,9 +1574,13 @@ parserHelp.setDigitSeparator(AscCommon.g_oDefaultCultureInfo.NumberDecimalSepara
return new cError(cErrorType.wrong_name);
}
//defName not linked to cell, use inherit range
var r1 = arguments[1];
return defName.parsedRef.calculate(this, r1);
var offset;
var bbox = arguments[1];
if (bbox) {
//offset - to support relative references in def names
offset = {offsetRow: bbox.r1, offsetCol: bbox.c1};
}
return defName.parsedRef.calculate(this, bbox, offset);
};
cName.prototype.getDefName = function () {
return this.ws ? this.ws.workbook.getDefinesNames(this.value, this.ws.getId()) : null;
......@@ -1666,7 +1668,7 @@ parserHelp.setDigitSeparator(AscCommon.g_oDefaultCultureInfo.NumberDecimalSepara
return val;
}
};
cStrucTable.prototype.toRef = function (opt_bbox) {
cStrucTable.prototype.toRef = function (opt_bbox, opt_bConvertTableFormulaToRef) {
//opt_bbox usefull only for #This row
//case null == opt_bbox works like FormulaTablePartInfo.data
var table = this.wb.getDefinesNames(this.tableName, this.ws ? this.ws.getId() : null);
......@@ -1674,7 +1676,7 @@ parserHelp.setDigitSeparator(AscCommon.g_oDefaultCultureInfo.NumberDecimalSepara
return new cError(cErrorType.wrong_name);
}
if (!this.area || this.isDynamic) {
this._updateArea(opt_bbox, true);
this._updateArea(opt_bbox, true, opt_bConvertTableFormulaToRef);
}
return this.area;
};
......@@ -1786,8 +1788,8 @@ parserHelp.setDigitSeparator(AscCommon.g_oDefaultCultureInfo.NumberDecimalSepara
}
return bRes;
};
cStrucTable.prototype._updateArea = function (opt_bbox, opt_toRef) {
var paramObj = {param: null, startCol: null, endCol: null, cell: opt_bbox, toRef: opt_toRef};
cStrucTable.prototype._updateArea = function (bbox, toRef, bConvertTableFormulaToRef) {
var paramObj = {param: null, startCol: null, endCol: null, cell: bbox, toRef: toRef, bConvertTableFormulaToRef: bConvertTableFormulaToRef};
var isThisRow = false;
var tableData;
if (this.oneColumnIndex) {
......@@ -1832,7 +1834,12 @@ parserHelp.setDigitSeparator(AscCommon.g_oDefaultCultureInfo.NumberDecimalSepara
return this._createAreaError(isThisRow);
}
if (range) {
var r1Abs = range.isAbsR1();
var c1Abs = data.range.isAbsC1();
var r2Abs = range.isAbsR2();
var c2Abs = data.range.isAbsC2();
range = new Asc.Range(data.range.c1, range.r1, data.range.c2, range.r2);
range.setAbs(r1Abs, c1Abs, r2Abs, c2Abs);
} else {
range = data.range;
}
......@@ -1850,7 +1857,7 @@ parserHelp.setDigitSeparator(AscCommon.g_oDefaultCultureInfo.NumberDecimalSepara
}
}
if (tableData.range) {
var refName = tableData.range.getAbsName();
var refName = tableData.range.getName();
var wsFrom = this.wb.getWorksheetById(tableData.wsID);
if (tableData.range.isOneCell()) {
this.area = new cRef3D(refName, wsFrom);
......@@ -3054,6 +3061,8 @@ var cFormulaOperators = {
if (ref[ref.length - 1] === ')') {
ref = ref.slice(0, -1);
}
var activeCell = ws.selectionRange.activeCell;
var bbox = new Asc.Range(activeCell.col, activeCell.row, activeCell.col, activeCell.row);
// ToDo in parser formula
var ranges = [];
var arrRefs = ref.split(',');
......@@ -3072,7 +3081,7 @@ var cFormulaOperators = {
case cElementType.table:
case cElementType.name:
case cElementType.name3D:
ref = item.oper.toRef();
ref = item.oper.toRef(bbox);
break;
case cElementType.cell:
case cElementType.cell3D:
......@@ -4858,7 +4867,7 @@ parserFormula.prototype.parse = function(local, digitDelim) {
}
};
parserFormula.prototype.calculate = function (opt_defName, opt_bbox) {
parserFormula.prototype.calculate = function (opt_defName, opt_bbox, opt_offset) {
if (this.isCalculate) {
this.value = new cError(cErrorType.bad_reference);
this._endCalculate();
......@@ -4909,6 +4918,35 @@ parserFormula.prototype.parse = function(local, digitDelim) {
elemArr.push(currentElement.Calculate(arg, bbox));
} else if (currentElement.type === cElementType.table) {
elemArr.push(currentElement.toRef(bbox));
} else if (opt_offset) {
var cloneElem = null;
var bbox = null;
var ws;
if (cElementType.cell === currentElement.type || cElementType.cell3D === currentElement.type ||
cElementType.cellsRange === currentElement.type) {
var range = currentElement.getRange();
if (range) {
bbox = range.getBBox0();
ws = range.getWorksheet();
if (!bbox.isAbsAll()) {
cloneElem = currentElement.clone();
bbox = cloneElem.getRange().getBBox0();
}
}
} else if (cElementType.cellsRange3D === currentElement.type) {
bbox = currentElement.getBBox0();
if (!bbox.isAbsAll()) {
cloneElem = currentElement.clone();
bbox = cloneElem.getBBox0();
}
}
if(cloneElem){
bbox.setOffsetWithAbs(opt_offset, false, true);
this.changeOffsetBBox(cloneElem, bbox, ws);
elemArr.push(cloneElem);
} else {
elemArr.push(currentElement);
}
} else {
elemArr.push(currentElement);
}
......@@ -4936,13 +4974,14 @@ parserFormula.prototype.parse = function(local, digitDelim) {
};
parserFormula.prototype.changeOffsetElem = function(elem, container, index, offset, canResize) {//offset =
// AscCommonExcel.CRangeOffset
var range, bbox = null, isErr = false;
var range, bbox = null, ws, isErr = false;
if (cElementType.cell === elem.type || cElementType.cell3D === elem.type ||
cElementType.cellsRange === elem.type) {
isErr = true;
range = elem.getRange();
if (range) {
bbox = range.getBBox0();
ws = range.getWorksheet();
}
} else if (cElementType.cellsRange3D === elem.type) {
isErr = true;
......@@ -4951,12 +4990,7 @@ parserFormula.prototype.parse = function(local, digitDelim) {
if (bbox) {
if (bbox.setOffsetWithAbs(offset, canResize)) {
isErr = false;
if (cElementType.cellsRange3D === elem.type) {
elem.bbox = bbox;
} else {
elem.range = AscCommonExcel.Range.prototype.createFromBBox(range.getWorksheet(), bbox);
}
elem.value = bbox.getName();
this.changeOffsetBBox(elem, bbox, ws);
}
}
if (isErr) {
......@@ -4964,6 +4998,14 @@ parserFormula.prototype.parse = function(local, digitDelim) {
}
return elem;
};
parserFormula.prototype.changeOffsetBBox = function(elem, bbox, ws) {
if (cElementType.cellsRange3D === elem.type) {
elem.bbox = bbox;
} else {
elem.range = AscCommonExcel.Range.prototype.createFromBBox(ws, bbox);
}
elem.value = bbox.getName();
};
parserFormula.prototype.changeDefName = function(from, to) {
var i, elem;
for (i = 0; i < this.outStack.length; i++) {
......@@ -4975,12 +5017,17 @@ parserFormula.prototype.parse = function(local, digitDelim) {
};
parserFormula.prototype.removeTableName = function(defName, bConvertTableFormulaToRef) {
var i, elem;
var bbox;
if (this.parent && this.parent.onFormulaEvent) {
bbox= this.parent.onFormulaEvent(AscCommon.c_oNotifyParentType.GetRangeCell);
}
for (i = 0; i < this.outStack.length; i++) {
elem = this.outStack[i];
if (elem.type == cElementType.table && elem.tableName == defName.name) {
if(bConvertTableFormulaToRef)
{
this.outStack[i] = this.outStack[i].toRef();
this.outStack[i] = this.outStack[i].toRef(bbox, bConvertTableFormulaToRef);
}
else
{
......
......@@ -5482,7 +5482,7 @@ TablePart.prototype.getTableRangeForFormula = function(objectParam)
{
if(this.HeaderRowCount === null) {
res = new Asc.Range(this.Ref.c1, this.Ref.r1, this.Ref.c2, this.Ref.r1);
} else if(!objectParam.toRef) {
} else if(!objectParam.toRef || objectParam.bConvertTableFormulaToRef) {
res = new Asc.Range(this.Ref.c1, startRow, this.Ref.c2, endRow);
}
break;
......@@ -5491,7 +5491,7 @@ TablePart.prototype.getTableRangeForFormula = function(objectParam)
{
if(this.TotalsRowCount) {
res = new Asc.Range(this.Ref.c1, this.Ref.r2, this.Ref.c2, this.Ref.r2);
} else if(!objectParam.toRef) {
} else if(!objectParam.toRef || objectParam.bConvertTableFormulaToRef) {
res = new Asc.Range(this.Ref.c1, startRow, this.Ref.c2, endRow);
}
break;
......@@ -5501,9 +5501,15 @@ TablePart.prototype.getTableRangeForFormula = function(objectParam)
if (objectParam.cell) {
if (startRow <= objectParam.cell.r1 && objectParam.cell.r1 <= endRow) {
res = new Asc.Range(this.Ref.c1, objectParam.cell.r1, this.Ref.c2, objectParam.cell.r1);
} else if (objectParam.bConvertTableFormulaToRef) {
res = new Asc.Range(this.Ref.c1, startRow, this.Ref.c2, endRow);
}
} else {
res = new Asc.Range(this.Ref.c1, startRow, this.Ref.c2, endRow);
if (objectParam.bConvertTableFormulaToRef) {
res = new Asc.Range(this.Ref.c1, 0, this.Ref.c2, 0);
} else {
res = new Asc.Range(this.Ref.c1, startRow, this.Ref.c2, endRow);
}
}
break;
}
......@@ -5521,6 +5527,13 @@ TablePart.prototype.getTableRangeForFormula = function(objectParam)
break;
}
}
if (res) {
if (objectParam.param === FormulaTablePartInfo.thisRow) {
res.setAbs(false, true, false, true);
} else {
res.setAbs(true, true, true, true);
}
}
return res;
};
......
......@@ -502,7 +502,7 @@
this.r1 = Math.min(this.r1, r);
this.r2 = Math.max(this.r2, r);
};
Range.prototype.setOffsetWithAbs = function(offset, canResize) {
Range.prototype.setOffsetWithAbs = function(offset, opt_canResize, opt_circle) {
var temp;
var offsetRow = offset.offsetRow;
var offsetCol = offset.offsetCol;
......@@ -519,52 +519,84 @@
if (!isAbsRow1) {
this.r1 += offsetRow;
if (this.r1 < 0) {
this.r1 = 0;
if (!canResize) {
return false;
if (opt_circle) {
this.r1 += gc_nMaxRow0 + 1;
} else {
this.r1 = 0;
if (!opt_canResize) {
return false;
}
}
}
if (this.r1 > gc_nMaxRow0) {
this.r1 = gc_nMaxRow0;
return false;
if (opt_circle) {
this.r1 -= gc_nMaxRow0 + 1;
} else {
this.r1 = gc_nMaxRow0;
return false;
}
}
}
if (!isAbsCol1) {
this.c1 += offsetCol;
if (this.c1 < 0) {
this.c1 = 0;
if (!canResize) {
return false;
if (opt_circle) {
this.c1 += gc_nMaxCol0 + 1;
} else {
this.c1 = 0;
if (!opt_canResize) {
return false;
}
}
}
if (this.c1 > gc_nMaxCol0) {
this.c1 = gc_nMaxCol0;
return false;
if (opt_circle) {
this.c1 -= gc_nMaxCol0 + 1;
} else {
this.c1 = gc_nMaxCol0;
return false;
}
}
}
if (!isAbsRow2) {
this.r2 += offsetRow;
if (this.r2 < 0) {
this.r2 = 0;
return false;
if (opt_circle) {
this.r2 += gc_nMaxRow0 + 1;
} else {
this.r2 = 0;
return false;
}
}
if (this.r2 > gc_nMaxRow0) {
this.r2 = gc_nMaxRow0;
if (!canResize) {
return false;
if (opt_circle) {
this.r2 -= gc_nMaxRow0 + 1;
} else {
this.r2 = gc_nMaxRow0;
if (!opt_canResize) {
return false;
}
}
}
}
if (!isAbsCol2) {
this.c2 += offsetCol;
if (this.c2 < 0) {
this.c2 = 0;
return false;
if (opt_circle) {
this.c2 += gc_nMaxCol0 + 1;
} else {
this.c2 = 0;
return false;
}
}
if (this.c2 > gc_nMaxCol0) {
this.c2 = gc_nMaxCol0;
if (!canResize) {
return false;
if (opt_circle) {
this.c2 -= gc_nMaxCol0 + 1;
} else {
this.c2 = gc_nMaxCol0;
if (!opt_canResize) {
return false;
}
}
}
}
......@@ -784,6 +816,21 @@
Range.prototype.isAbsRow = function (refType) {
return (refType === referenceType.A || refType === referenceType.ARRC);
};
Range.prototype.isAbsR1 = function () {
return this.isAbsRow(this.refType1);
};
Range.prototype.isAbsC1 = function () {
return this.isAbsCol(this.refType1);
};
Range.prototype.isAbsR2 = function () {
return this.isAbsRow(this.refType2);
};
Range.prototype.isAbsC2 = function () {
return this.isAbsCol(this.refType2);
};
Range.prototype.isAbsAll = function () {
return this.isAbsR1() && this.isAbsC1() && this.isAbsR2() && this.isAbsC2();
};
Range.prototype.switchReference = function () {
this.refType1 = (this.refType1 + 1) % 4;
this.refType2 = (this.refType2 + 1) % 4;
......@@ -1393,6 +1440,8 @@
}
if (null == oRes && null != oCacheVal.first && null != oCacheVal.last) {
var r1 = oCacheVal.first.getRow0(), r2 = oCacheVal.last.getRow0(), c1 = oCacheVal.first.getCol0(), c2 = oCacheVal.last.getCol0();
var r1Abs = oCacheVal.first.getRowAbs(), r2Abs = oCacheVal.last.getRowAbs(),
c1Abs = oCacheVal.first.getColAbs(), c2Abs = oCacheVal.first.getColAbs();
if (oCacheVal.first.getIsRow() && oCacheVal.last.getIsRow()) {
c1 = 0;
c2 = gc_nMaxCol0;
......@@ -1405,18 +1454,23 @@
var temp = r1;
r1 = r2;
r2 = temp;
temp = r1Abs;
r1Abs = r2Abs;
r2Abs = temp;
}
if (c1 > c2) {
var temp = c1;
c1 = c2;
c2 = temp;
temp = c1Abs;
c1Abs = c2Abs;
c2Abs = temp;
}
if (1 == type) {
if (null == oCacheVal.ascRange) {
var oAscRange = new Range(c1, r1, c2, r2);
oAscRange.setAbs(oCacheVal.first.getRowAbs(), oCacheVal.first.getColAbs(), oCacheVal.last.getRowAbs(),
oCacheVal.last.getColAbs());
oAscRange.setAbs(r1Abs, c1Abs, r2Abs, c2Abs);
oCacheVal.ascRange = oAscRange;
}
......@@ -1424,8 +1478,7 @@
} else if (2 == type) {
if (null == oCacheVal.activeRange) {
var oActiveRange = new ActiveRange(c1, r1, c2, r2);
oActiveRange.setAbs(oCacheVal.first.getRowAbs(), oCacheVal.first.getColAbs(), oCacheVal.last.getRowAbs(),
oCacheVal.last.getColAbs());
oActiveRange.setAbs(r1Abs, c1Abs, r2Abs, c2Abs);
var bCol = 0 == r1 && gc_nMaxRow0 == r2;
var bRow = 0 == c1 && gc_nMaxCol0 == c2;
......@@ -1446,8 +1499,7 @@
} else {
if (null == oCacheVal.formulaRange) {
var oFormulaRange = new FormulaRange(c1, r1, c2, r2);
oFormulaRange.setAbs(oCacheVal.first.getRowAbs(), oCacheVal.first.getColAbs(), oCacheVal.last.getRowAbs(),
oCacheVal.last.getColAbs());
oFormulaRange.setAbs(r1Abs, c1Abs, r2Abs, c2Abs);
oCacheVal.formulaRange = oFormulaRange;
}
......
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