import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
-import java.util.regex.Pattern;
//import PTGs .. since we need everything, import *
import org.apache.poi.hssf.record.formula.*;
/** Report What Was Expected */
private RuntimeException expected(String s) {
- String msg = "Parse error near char " + (pointer-1) + " '" + look + "'"
- + " in specified formula '" + formulaString + "'. Expected "
- + s;
+ String msg;
+
+ if (look == '=' && formulaString.substring(0, pointer-1).trim().length() < 1) {
+ msg = "The specified formula '" + formulaString
+ + "' starts with an equals sign which is not allowed.";
+ } else {
+ msg = "Parse error near char " + (pointer-1) + " '" + look + "'"
+ + " in specified formula '" + formulaString + "'. Expected "
+ + s;
+ }
return new FormulaParseException(msg);
}
package org.apache.poi.hssf.record.formula;
-import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.hssf.record.RecordInputStream;
/**
public final class BoolPtg extends ScalarConstantPtg {
public final static int SIZE = 2;
public final static byte sid = 0x1d;
- private final boolean field_1_value;
+ private final boolean _value;
- public BoolPtg(RecordInputStream in)
- {
- field_1_value = (in.readByte() == 1);
+ public BoolPtg(RecordInputStream in) {
+ _value = (in.readByte() == 1);
}
-
public BoolPtg(String formulaToken) {
- field_1_value = (formulaToken.equals("TRUE"));
+ _value = (formulaToken.equalsIgnoreCase("TRUE"));
}
- public boolean getValue()
- {
- return field_1_value;
+ public boolean getValue() {
+ return _value;
}
- public void writeBytes(byte [] array, int offset)
- {
+ public void writeBytes(byte [] array, int offset) {
array[ offset + 0 ] = sid;
- array[ offset + 1 ] = (byte) (field_1_value ? 1 : 0);
+ array[ offset + 1 ] = (byte) (_value ? 1 : 0);
}
- public int getSize()
- {
+ public int getSize() {
return SIZE;
}
- public String toFormulaString(Workbook book)
- {
- return field_1_value ? "TRUE" : "FALSE";
+ protected String toFormulaString() {
+ return _value ? "TRUE" : "FALSE";
}
}
package org.apache.poi.hssf.record.formula;
-import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.hssf.record.RecordInputStream;
import org.apache.poi.hssf.usermodel.HSSFErrorConstants;
array[offset + 1] = (byte)field_1_error_code;
}
- public String toFormulaString(Workbook book) {
+ protected String toFormulaString() {
return HSSFErrorConstants.getText(field_1_error_code);
}
package org.apache.poi.hssf.record.formula;
-import org.apache.poi.util.LittleEndian;
-import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.hssf.record.RecordInputStream;
+import org.apache.poi.util.LittleEndian;
/**
* Integer (unsigned short integer)
return field_1_value;
}
- public void writeBytes(byte [] array, int offset)
- {
+ public void writeBytes(byte [] array, int offset) {
array[ offset + 0 ] = sid;
LittleEndian.putUShort(array, offset + 1, getValue());
}
return SIZE;
}
- public String toFormulaString(Workbook book) {
+ protected String toFormulaString() {
return String.valueOf(getValue());
}
-
- public String toString() {
- StringBuffer sb = new StringBuffer(64);
- sb.append(getClass().getName()).append(" [");
- sb.append(field_1_value);
- sb.append("]");
- return sb.toString();
- }
}
package org.apache.poi.hssf.record.formula;
-import org.apache.poi.ss.usermodel.Workbook;
-
/**
* Missing Function Arguments
*
public final static byte sid = 0x16;
public static final Ptg instance = new MissingArgPtg();
- private MissingArgPtg()
- {
+
+ private MissingArgPtg() {
+ // enforce singleton
}
- public void writeBytes(byte [] array, int offset)
- {
+ public void writeBytes(byte [] array, int offset) {
array[ offset + 0 ] = sid;
}
- public int getSize()
- {
+ public int getSize() {
return SIZE;
}
- public String toFormulaString(Workbook book)
- {
+ protected String toFormulaString() {
return " ";
}
}
package org.apache.poi.hssf.record.formula;
-import org.apache.poi.util.LittleEndian;
-import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.hssf.record.RecordInputStream;
+import org.apache.poi.util.LittleEndian;
/**
* Number
private final double field_1_value;
/** Create a NumberPtg from a byte array read from disk */
- public NumberPtg(RecordInputStream in)
- {
+ public NumberPtg(RecordInputStream in) {
this(in.readDouble());
}
field_1_value = value;
}
- public double getValue()
- {
+ public double getValue() {
return field_1_value;
}
- public void writeBytes(byte [] array, int offset)
- {
+ public void writeBytes(byte [] array, int offset) {
array[ offset + 0 ] = sid;
LittleEndian.putDouble(array, offset + 1, getValue());
}
- public int getSize()
- {
+ public int getSize() {
return SIZE;
}
- public String toFormulaString(Workbook book)
- {
+ protected String toFormulaString() {
// TODO - java's rendering of double values is not quite same as excel's
+ // Maybe use HSSFDataFormatter?
return String.valueOf(field_1_value);
}
-
- public String toString() {
- StringBuffer sb = new StringBuffer(64);
- sb.append(getClass().getName()).append(" [");
- sb.append(field_1_value);
- sb.append("]");
- return sb.toString();
- }
}
\r
package org.apache.poi.hssf.record.formula;\r
\r
+import org.apache.poi.ss.usermodel.Workbook;\r
+\r
/**\r
* @author Josh Micich\r
*/\r
public boolean isBaseToken() {\r
return true;\r
}\r
- public final byte getDefaultOperandClass() {\r
- return Ptg.CLASS_VALUE;\r
- }\r
+\r
+ public final byte getDefaultOperandClass() {\r
+ return Ptg.CLASS_VALUE;\r
+ }\r
+\r
+ public final String toFormulaString(Workbook book) {\r
+ return toFormulaString();\r
+ }\r
+\r
+ protected abstract String toFormulaString();\r
+\r
+ public final String toString() {\r
+ StringBuffer sb = new StringBuffer(64);\r
+ sb.append(getClass().getName()).append(" [");\r
+ sb.append(toFormulaString());\r
+ sb.append("]");\r
+ return sb.toString();\r
+ }\r
}\r
package org.apache.poi.hssf.record.formula;
-import org.apache.poi.ss.usermodel.Workbook;
+import org.apache.poi.hssf.record.RecordInputStream;
import org.apache.poi.util.BitField;
import org.apache.poi.util.BitFieldFactory;
import org.apache.poi.util.StringUtil;
-import org.apache.poi.hssf.record.RecordInputStream;
/**
* String Stores a String value in a formula value stored in the format
} else {
field_3_string = in.readCompressedUnicode(field_1_length);
}
-
- // setValue(new String(data, offset+3, data[offset+1] + 256*data[offset+2]));
}
/**
}
}
- public String toFormulaString(Workbook book) {
+ protected String toFormulaString() {
String value = field_3_string;
int len = value.length();
StringBuffer sb = new StringBuffer(len + 4);
sb.append(FORMULA_DELIMITER);
return sb.toString();
}
-
- public String toString() {
- StringBuffer sb = new StringBuffer(64);
- sb.append(getClass().getName()).append(" [");
- sb.append(field_3_string);
- sb.append("]");
- return sb.toString();
- }
}
if(commentsRel != null && commentsRel.size() > 0) {
PackagePart commentsPart = getTargetPart(commentsRel.getRelationship(0));
CommentsDocument cmntdoc = CommentsDocument.Factory.parse(commentsPart.getInputStream());
- for(CTComment ctcomment : cmntdoc.getComments().getCommentArray())
- {
+ for(CTComment ctcomment : cmntdoc.getComments().getCommentArray()) {
comments.add(new XWPFComment(ctcomment));
}
-
- for(CTTbl table : getDocumentBody().getTblArray())
- {
- tables.add(new XWPFTable(table));
- }
}
+ // Get any tables
+ for(CTTbl table : getDocumentBody().getTblArray()) {
+ tables.add(new XWPFTable(table));
+ }
+
+ /// Process embedded document parts
this.embedds = new LinkedList<PackagePart>();
for(PackageRelationship rel : getCorePart().getRelationshipsByType(OLE_OBJECT_REL_TYPE)) {
embedds.add(getTargetPart(rel));
}
-
for(PackageRelationship rel : getCorePart().getRelationshipsByType(PACK_OBJECT_REL_TYPE)) {
embedds.add(getTargetPart(rel));
}
import org.apache.poi.xwpf.model.XMLParagraph;
import org.apache.poi.xwpf.XWPFDocument;
+import org.apache.xmlbeans.XmlObject;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP;
+import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTPicture;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTR;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTText;
+import org.w3c.dom.NodeList;
+import org.w3c.dom.Text;
/**
* Sketch of XWPF paragraph class
* TODO - replace with RichText String
*/
private StringBuffer text = new StringBuffer();
+ private StringBuffer pictureText = new StringBuffer();
public XWPFParagraph(CTP prgrph, XWPFDocument docRef)
{
texts[k].getStringValue()
);
}
+
+ // Loop over pictures inside our
+ // paragraph, looking for text in them
+ CTPicture[] picts = rs[j].getPictArray();
+ for (int k = 0; k < picts.length; k++) {
+ XmlObject[] t = picts[k].selectPath("declare namespace w='http://schemas.openxmlformats.org/wordprocessingml/2006/main' .//w:t");
+ for (int m = 0; m < t.length; m++) {
+ NodeList kids = t[m].getDomNode().getChildNodes();
+ for (int n = 0; n < kids.getLength(); n++) {
+ if (kids.item(n) instanceof Text) {
+ pictureText.append("\n" + kids.item(n).getNodeValue());
+ }
+ }
+ }
+ }
}
}
return docRef;
}
+ /**
+ * Return the textual content of the paragraph,
+ * including text from pictures in it.
+ */
public String getText() {
return text.toString();
}
HSSFRow row = sheet.createRow(0);
HSSFCell cell;
- cell = row.createCell((short)0);
+ cell = row.createCell(0);
cell.setCellFormula("NoQuotesNeeded!A1");
- cell = row.createCell((short)1);
+ cell = row.createCell(1);
cell.setCellFormula("'Quotes Needed Here &#$@'!A1");
}
HSSFRow row = sheet.createRow(0);
HSSFCell cell;
- cell = row.createCell((short)0);
+ cell = row.createCell(0);
cell.setCellFormula("Cash_Flow!A1");
}
HSSFSheet sheet = wb.createSheet("Test");
HSSFRow row = sheet.createRow(0);
- HSSFCell cell = row.createCell((short)0);
+ HSSFCell cell = row.createCell(0);
String formula = null;
cell.setCellFormula("1.3E21/3");
HSSFSheet sheet = wb.createSheet("Test");
HSSFRow row = sheet.createRow(0);
- HSSFCell cell = row.createCell((short)0);
+ HSSFCell cell = row.createCell(0);
String formula = null;
// starts from decimal point
HSSFSheet sheet = wb.createSheet("Test");
HSSFRow row = sheet.createRow(0);
- HSSFCell cell = row.createCell((short)0);
+ HSSFCell cell = row.createCell(0);
String formula = null;
cell.setCellFormula("A1.A2");
confirmParseErrorLiteral(ErrPtg.NAME_INVALID, "#NAME?");
confirmParseErrorLiteral(ErrPtg.NUM_ERROR, "#NUM!");
confirmParseErrorLiteral(ErrPtg.N_A, "#N/A");
+ parseFormula("HLOOKUP(F7,#REF!,G7,#REF!)");
}
private static void confirmParseErrorLiteral(ErrPtg expectedToken, String formula) {
wb.setSheetName(0, "Sheet1");
HSSFRow row = sheet.createRow(0);
- HSSFCell cell = row.createCell((short)0);
+ HSSFCell cell = row.createCell(0);
cell.setCellFormula("right(\"test\"\"ing\", 3)");
String actualCellFormula = cell.getCellFormula();
if("RIGHT(\"test\"ing\",3)".equals(actualCellFormula)) {
wb.setSheetName(0, "Sheet1");
HSSFRow row = sheet.createRow(0);
- HSSFCell cell = row.createCell((short)0);
+ HSSFCell cell = row.createCell(0);
cell.setCellFormula("SUM(A32769:A32770)");
if("SUM(A-32767:A-32766)".equals(cell.getCellFormula())) {
fail("Identified bug 44539");
} catch (FormulaParseException e) {
assertEquals("Parse error near char 10 ';' in specified formula 'round(3.14;2)'. Expected ',' or ')'", e.getMessage());
}
+
+ try {
+ parseFormula(" =2+2");
+ throw new AssertionFailedError("Didn't get parse exception as expected");
+ } catch (FormulaParseException e) {
+ assertEquals("The specified formula ' =2+2' starts with an equals sign which is not allowed.", e.getMessage());
+ }
}
/**
assertEquals(2, ptgs.length);
assertEquals(NamePtg.class, ptgs[0].getClass());
- HSSFCell cell = sheet.createRow(0).createCell((short)0);
+ HSSFCell cell = sheet.createRow(0).createCell(0);
cell.setCellFormula("count(pfy1)");
assertEquals("COUNT(pfy1)", cell.getCellFormula());
try {
}
}
cell.setCellFormula("count(fp1)"); // plain cell ref, col is in range
-
}
}