aboutsummaryrefslogtreecommitdiffstats
path: root/src/java/org/apache/poi/ss
diff options
context:
space:
mode:
authorJosh Micich <josh@apache.org>2008-11-07 23:16:48 +0000
committerJosh Micich <josh@apache.org>2008-11-07 23:16:48 +0000
commitfb53e42579b53ba930f068525696116698d3e8ea (patch)
tree12bb7e9581ad19ac5a433e2933662f35d1b6f237 /src/java/org/apache/poi/ss
parentf1438095498ea70e80195365bf168dd14e80cecd (diff)
downloadpoi-fb53e42579b53ba930f068525696116698d3e8ea.tar.gz
poi-fb53e42579b53ba930f068525696116698d3e8ea.zip
Fixed problem with linking shared formulas when ranges overlap
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@712307 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/java/org/apache/poi/ss')
-rw-r--r--src/java/org/apache/poi/ss/formula/Formula.java40
-rw-r--r--src/java/org/apache/poi/ss/util/CellRangeAddressBase.java7
2 files changed, 44 insertions, 3 deletions
diff --git a/src/java/org/apache/poi/ss/formula/Formula.java b/src/java/org/apache/poi/ss/formula/Formula.java
index 55314452a2..eaf73cd637 100644
--- a/src/java/org/apache/poi/ss/formula/Formula.java
+++ b/src/java/org/apache/poi/ss/formula/Formula.java
@@ -1,6 +1,15 @@
package org.apache.poi.ss.formula;
+import java.util.Arrays;
+
+import org.apache.poi.hssf.record.ArrayRecord;
+import org.apache.poi.hssf.record.SharedFormulaRecord;
+import org.apache.poi.hssf.record.TableRecord;
+import org.apache.poi.hssf.record.formula.ExpPtg;
import org.apache.poi.hssf.record.formula.Ptg;
+import org.apache.poi.hssf.record.formula.TblPtg;
+import org.apache.poi.hssf.util.CellReference;
+import org.apache.poi.util.LittleEndian;
import org.apache.poi.util.LittleEndianByteArrayInputStream;
import org.apache.poi.util.LittleEndianInput;
import org.apache.poi.util.LittleEndianOutput;
@@ -130,4 +139,35 @@ public class Formula {
// OK to return this for the moment because currently immutable
return this;
}
+
+ /**
+ * Gets the locator for the corresponding {@link SharedFormulaRecord}, {@link ArrayRecord} or
+ * {@link TableRecord} if this formula belongs to such a grouping. The {@link CellReference}
+ * returned by this method will match the top left corner of the range of that grouping.
+ * The return value is usually not the same as the location of the cell containing this formula.
+ *
+ * @return the firstRow & firstColumn of an array formula or shared formula that this formula
+ * belongs to. <code>null</code> if this formula is not part of an array or shared formula.
+ */
+ public CellReference getExpReference() {
+ byte[] data = _byteEncoding;
+ if (data.length != 5) {
+ // tExp and tTbl are always 5 bytes long, and the only ptg in the formula
+ return null;
+ }
+ switch (data[0]) {
+ case ExpPtg.sid:
+ break;
+ case TblPtg.sid:
+ break;
+ default:
+ return null;
+ }
+ int firstRow = LittleEndian.getUShort(data, 1);
+ int firstColumn = LittleEndian.getUShort(data, 3);
+ return new CellReference(firstRow, firstColumn);
+ }
+ public boolean isSame(Formula other) {
+ return Arrays.equals(_byteEncoding, other._byteEncoding);
+ }
}
diff --git a/src/java/org/apache/poi/ss/util/CellRangeAddressBase.java b/src/java/org/apache/poi/ss/util/CellRangeAddressBase.java
index b26780d85b..9e292929af 100644
--- a/src/java/org/apache/poi/ss/util/CellRangeAddressBase.java
+++ b/src/java/org/apache/poi/ss/util/CellRangeAddressBase.java
@@ -46,8 +46,7 @@ public abstract class CellRangeAddressBase {
_firstCol = firstCol;
_lastCol = lastCol;
}
- private static boolean isValid(int firstRow, int lastRow, int firstColumn, int lastColumn)
- {
+ private static boolean isValid(int firstRow, int lastRow, int firstColumn, int lastColumn) {
if(lastRow < 0 || lastRow > LAST_ROW_INDEX) {
return false;
}
@@ -129,6 +128,8 @@ public abstract class CellRangeAddressBase {
}
public final String toString() {
- return getClass().getName() + " ["+_firstRow+", "+_lastRow+", "+_firstCol+", "+_lastCol+"]";
+ CellReference crA = new CellReference(_firstRow, _firstCol);
+ CellReference crB = new CellReference(_lastRow, _lastCol);
+ return getClass().getName() + " [" + crA.formatAsString() + ":" + crB.formatAsString() +"]";
}
}