Index: src/java/org/apache/poi/hssf/util/AreaReference.java
===================================================================
--- src/java/org/apache/poi/hssf/util/AreaReference.java (revision 609621)
+++ src/java/org/apache/poi/hssf/util/AreaReference.java (working copy)
@@ -18,15 +18,24 @@
package org.apache.poi.hssf.util;
+import java.util.ArrayList;
+import java.util.StringTokenizer;
+
public class AreaReference {
private CellReference [] cells;
private int dim;
- /** Create an area ref from a string representation
+ /**
+ * Create an area ref from a string representation.
+ * The area reference must be contiguous
*/
public AreaReference(String reference) {
+ if(! isContiguous(reference)) {
+ throw new IllegalArgumentException("References passed to the AreaReference must be contiguous, use generateContiguous(ref) if you have non-contiguous references");
+ }
+
String[] refs = seperateAreaRefs(reference);
dim = refs.length;
cells = new CellReference[dim];
@@ -34,6 +43,36 @@
cells[i]=new CellReference(refs[i]);
}
}
+
+ /**
+ * Is the reference for a contiguous (i.e.
+ * unbroken) area, or is it made up of
+ * several different parts?
+ * (If it is, you will need to call
+ * ....
+ */
+ public static boolean isContiguous(String reference) {
+ if(reference.indexOf(',') == -1) {
+ return true;
+ }
+ return false;
+ }
+
+ /**
+ * Takes a non-contiguous area reference, and
+ * returns an array of contiguous area references.
+ */
+ public static AreaReference[] generateContiguous(String reference) {
+ ArrayList refs = new ArrayList();
+ StringTokenizer st = new StringTokenizer(reference, ",");
+ while(st.hasMoreTokens()) {
+ refs.add(
+ new AreaReference(st.nextToken())
+ );
+ }
+ return (AreaReference[])refs.toArray(new AreaReference[refs.size()]);
+ }
+
//not sure if we need to be flexible here!
/** return the dimensions of this area
**/
Index: src/java/org/apache/poi/hssf/record/NameRecord.java
===================================================================
--- src/java/org/apache/poi/hssf/record/NameRecord.java (revision 609621)
+++ src/java/org/apache/poi/hssf/record/NameRecord.java (working copy)
@@ -20,13 +20,10 @@
import java.util.List;
import java.util.Stack;
+import java.util.Iterator;
import org.apache.poi.hssf.model.Workbook;
-import org.apache.poi.hssf.record.formula.Area3DPtg;
-import org.apache.poi.hssf.record.formula.DeletedArea3DPtg;
-import org.apache.poi.hssf.record.formula.DeletedRef3DPtg;
-import org.apache.poi.hssf.record.formula.Ptg;
-import org.apache.poi.hssf.record.formula.Ref3DPtg;
+import org.apache.poi.hssf.record.formula.*;
import org.apache.poi.hssf.util.RangeAddress;
import org.apache.poi.util.HexDump;
import org.apache.poi.util.LittleEndian;
@@ -648,17 +645,46 @@
Ptg ptg = (Ptg) field_13_name_definition.peek();
String result = "";
- if (ptg.getClass() == Area3DPtg.class){
- result = ptg.toFormulaString(book);
+ // If it's a union, descend in and process
+ if (ptg.getClass() == UnionPtg.class) {
+ Iterator it =field_13_name_definition.iterator();
+ while( it.hasNext() ) {
+ Ptg p = (Ptg)it.next();
- } else if (ptg.getClass() == Ref3DPtg.class){
- result = ptg.toFormulaString(book);
- } else if (ptg.getClass() == DeletedArea3DPtg.class || ptg.getClass() == DeletedRef3DPtg.class) {
- result = "#REF!" ; }
+ String thisRes = getAreaRefString(p, book);
+ if(thisRes.length() > 0) {
+ // Add a comma to the end if needed
+ if(result.length() > 0 && !result.endsWith(",")) {
+ result += ",";
+ }
+ // And add the string it corresponds to
+ result += thisRes;
+ }
+ }
+ } else {
+ // Otherwise just get the string
+ result = getAreaRefString(ptg, book);
+ }
return result;
}
+ /**
+ * Turn the given ptg into a string, or
+ * return an empty string if nothing is possible
+ * for it.
+ */
+ private String getAreaRefString(Ptg ptg,Workbook book) {
+ if (ptg.getClass() == Area3DPtg.class){
+ return ptg.toFormulaString(book);
+ } else if (ptg.getClass() == Ref3DPtg.class){
+ return ptg.toFormulaString(book);
+ } else if (ptg.getClass() == DeletedArea3DPtg.class || ptg.getClass() == DeletedRef3DPtg.class) {
+ return "#REF!";
+ }
+ return "";
+ }
+
/** sets the reference , the area only (range)
* @param ref area reference
*/
Index: src/documentation/content/xdocs/hssf/quick-guide.xml
===================================================================
--- src/documentation/content/xdocs/hssf/quick-guide.xml (revision 609621)
+++ src/documentation/content/xdocs/hssf/quick-guide.xml (working copy)
@@ -1159,7 +1159,34 @@
// extract the cell contents based on cell type etc.
}
+