aboutsummaryrefslogtreecommitdiffstats
path: root/src/java/org/apache
diff options
context:
space:
mode:
Diffstat (limited to 'src/java/org/apache')
-rw-r--r--src/java/org/apache/poi/hssf/model/HSSFFormulaParser.java18
-rw-r--r--src/java/org/apache/poi/hssf/record/CFRuleRecord.java31
-rw-r--r--src/java/org/apache/poi/hssf/usermodel/DVConstraint.java21
-rw-r--r--src/java/org/apache/poi/hssf/usermodel/HSSFCell.java12
-rw-r--r--src/java/org/apache/poi/hssf/usermodel/HSSFDataValidation.java4
-rw-r--r--src/java/org/apache/poi/hssf/usermodel/HSSFEvaluationWorkbook.java242
-rw-r--r--src/java/org/apache/poi/hssf/usermodel/HSSFName.java2
-rw-r--r--src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java4
-rw-r--r--src/java/org/apache/poi/hssf/usermodel/HSSFSheetConditionalFormatting.java20
-rw-r--r--src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java3
-rw-r--r--src/java/org/apache/poi/ss/formula/FormulaParser.java27
-rw-r--r--src/java/org/apache/poi/ss/formula/FormulaParsingWorkbook.java2
12 files changed, 214 insertions, 172 deletions
diff --git a/src/java/org/apache/poi/hssf/model/HSSFFormulaParser.java b/src/java/org/apache/poi/hssf/model/HSSFFormulaParser.java
index 1b19f87247..f24441841b 100644
--- a/src/java/org/apache/poi/hssf/model/HSSFFormulaParser.java
+++ b/src/java/org/apache/poi/hssf/model/HSSFFormulaParser.java
@@ -44,7 +44,7 @@ public final class HSSFFormulaParser {
* Convenience method for parsing cell formulas. see {@link #parse(String, HSSFWorkbook, int)}
*/
public static Ptg[] parse(String formula, HSSFWorkbook workbook) {
- return FormulaParser.parse(formula, createParsingWorkbook(workbook));
+ return parse(formula, workbook, FormulaType.CELL);
}
/**
@@ -52,9 +52,23 @@ public final class HSSFFormulaParser {
* @return the parsed formula tokens
*/
public static Ptg[] parse(String formula, HSSFWorkbook workbook, int formulaType) {
- return FormulaParser.parse(formula, createParsingWorkbook(workbook), formulaType);
+ return parse(formula, workbook, formulaType, -1);
}
+ /**
+ * @param formula the formula to parse
+ * @param workbook the parent workbook
+ * @param formulaType a constant from {@link FormulaType}
+ * @param sheetIndex the 0-based index of the sheet this formula belongs to.
+ * The sheet index is required to resolve sheet-level names. <code>-1</code> means that
+ * the scope of the name will be ignored and the parser will match named ranges only by name
+ *
+ * @return the parsed formula tokens
+ */
+ public static Ptg[] parse(String formula, HSSFWorkbook workbook, int formulaType, int sheetIndex) {
+ return FormulaParser.parse(formula, createParsingWorkbook(workbook), formulaType, sheetIndex);
+ }
+
/**
* Static method to convert an array of {@link Ptg}s in RPN order
* to a human readable string format in infix mode.
diff --git a/src/java/org/apache/poi/hssf/record/CFRuleRecord.java b/src/java/org/apache/poi/hssf/record/CFRuleRecord.java
index 986a0333bb..cfaa991e62 100644
--- a/src/java/org/apache/poi/hssf/record/CFRuleRecord.java
+++ b/src/java/org/apache/poi/hssf/record/CFRuleRecord.java
@@ -23,7 +23,9 @@ import org.apache.poi.hssf.record.cf.FontFormatting;
import org.apache.poi.hssf.record.cf.PatternFormatting;
import org.apache.poi.hssf.record.formula.Ptg;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
+import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.ss.formula.Formula;
+import org.apache.poi.ss.formula.FormulaType;
import org.apache.poi.util.BitField;
import org.apache.poi.util.BitFieldFactory;
import org.apache.poi.util.LittleEndianOutput;
@@ -132,18 +134,18 @@ public final class CFRuleRecord extends StandardRecord {
/**
* Creates a new comparison operation rule
*/
- public static CFRuleRecord create(HSSFWorkbook workbook, String formulaText) {
- Ptg[] formula1 = parseFormula(formulaText, workbook);
- return new CFRuleRecord(CONDITION_TYPE_FORMULA, ComparisonOperator.NO_COMPARISON,
- formula1, null);
- }
+ public static CFRuleRecord create(HSSFSheet sheet, String formulaText) {
+ Ptg[] formula1 = parseFormula(formulaText, sheet);
+ return new CFRuleRecord(CONDITION_TYPE_FORMULA, ComparisonOperator.NO_COMPARISON,
+ formula1, null);
+ }
/**
* Creates a new comparison operation rule
*/
- public static CFRuleRecord create(HSSFWorkbook workbook, byte comparisonOperation,
+ public static CFRuleRecord create(HSSFSheet sheet, byte comparisonOperation,
String formulaText1, String formulaText2) {
- Ptg[] formula1 = parseFormula(formulaText1, workbook);
- Ptg[] formula2 = parseFormula(formulaText2, workbook);
+ Ptg[] formula1 = parseFormula(formulaText1, sheet);
+ Ptg[] formula2 = parseFormula(formulaText2, sheet);
return new CFRuleRecord(CONDITION_TYPE_CELL_VALUE_IS, comparisonOperation, formula1, formula2);
}
@@ -527,10 +529,11 @@ public final class CFRuleRecord extends StandardRecord {
*
* @return <code>null</code> if <tt>formula</tt> was null.
*/
- private static Ptg[] parseFormula(String formula, HSSFWorkbook workbook) {
- if(formula == null) {
- return null;
- }
- return HSSFFormulaParser.parse(formula, workbook);
- }
+ private static Ptg[] parseFormula(String formula, HSSFSheet sheet) {
+ if(formula == null) {
+ return null;
+ }
+ int sheetIndex = sheet.getWorkbook().getSheetIndex(sheet);
+ return HSSFFormulaParser.parse(formula, sheet.getWorkbook(), FormulaType.CELL, sheetIndex);
+ }
}
diff --git a/src/java/org/apache/poi/hssf/usermodel/DVConstraint.java b/src/java/org/apache/poi/hssf/usermodel/DVConstraint.java
index 5bc478f424..68561d5a79 100644
--- a/src/java/org/apache/poi/hssf/usermodel/DVConstraint.java
+++ b/src/java/org/apache/poi/hssf/usermodel/DVConstraint.java
@@ -24,7 +24,6 @@ import org.apache.poi.hssf.model.HSSFFormulaParser;
import org.apache.poi.hssf.record.formula.NumberPtg;
import org.apache.poi.hssf.record.formula.Ptg;
import org.apache.poi.hssf.record.formula.StringPtg;
-import org.apache.poi.ss.formula.FormulaParser;
import org.apache.poi.ss.formula.FormulaType;
/**
@@ -324,24 +323,25 @@ public class DVConstraint {
/**
* @return both parsed formulas (for expression 1 and 2).
*/
- /* package */ FormulaPair createFormulas(HSSFWorkbook workbook) {
+ /* package */ FormulaPair createFormulas(HSSFSheet sheet) {
Ptg[] formula1;
Ptg[] formula2;
if (isListValidationType()) {
- formula1 = createListFormula(workbook);
+ formula1 = createListFormula(sheet);
formula2 = Ptg.EMPTY_PTG_ARRAY;
} else {
- formula1 = convertDoubleFormula(_formula1, _value1, workbook);
- formula2 = convertDoubleFormula(_formula2, _value2, workbook);
+ formula1 = convertDoubleFormula(_formula1, _value1, sheet);
+ formula2 = convertDoubleFormula(_formula2, _value2, sheet);
}
return new FormulaPair(formula1, formula2);
}
- private Ptg[] createListFormula(HSSFWorkbook workbook) {
+ private Ptg[] createListFormula(HSSFSheet sheet) {
if (_explicitListValues == null) {
- // formula is parsed with slightly different RVA rules: (root node type must be 'reference')
- return HSSFFormulaParser.parse(_formula1, workbook, FormulaType.DATAVALIDATION_LIST);
+ HSSFWorkbook wb = sheet.getWorkbook();
+ // formula is parsed with slightly different RVA rules: (root node type must be 'reference')
+ return HSSFFormulaParser.parse(_formula1, wb, FormulaType.DATAVALIDATION_LIST, wb.getSheetIndex(sheet));
// To do: Excel places restrictions on the available operations within a list formula.
// Some things like union and intersection are not allowed.
}
@@ -361,7 +361,7 @@ public class DVConstraint {
* @return The parsed token array representing the formula or value specified.
* Empty array if both formula and value are <code>null</code>
*/
- private static Ptg[] convertDoubleFormula(String formula, Double value, HSSFWorkbook workbook) {
+ private static Ptg[] convertDoubleFormula(String formula, Double value, HSSFSheet sheet) {
if (formula == null) {
if (value == null) {
return Ptg.EMPTY_PTG_ARRAY;
@@ -371,7 +371,8 @@ public class DVConstraint {
if (value != null) {
throw new IllegalStateException("Both formula and value cannot be present");
}
- return HSSFFormulaParser.parse(formula, workbook);
+ HSSFWorkbook wb = sheet.getWorkbook();
+ return HSSFFormulaParser.parse(formula, wb, FormulaType.CELL, wb.getSheetIndex(sheet));
}
diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java b/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java
index 838a0db95e..2e7431bcc8 100644
--- a/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java
+++ b/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java
@@ -54,6 +54,7 @@ import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Comment;
import org.apache.poi.ss.usermodel.Hyperlink;
import org.apache.poi.ss.usermodel.RichTextString;
+import org.apache.poi.ss.formula.FormulaType;
/**
* High level representation of a cell in a row of a spreadsheet.
@@ -269,7 +270,7 @@ public class HSSFCell implements Cell {
public short getCellNum() {
return (short) getColumnIndex();
}
-
+
public int getColumnIndex() {
return record.getColumn() & 0xFFFF;
}
@@ -594,7 +595,8 @@ public class HSSFCell implements Cell {
setCellType(CELL_TYPE_BLANK, false, row, col, styleIndex);
return;
}
- Ptg[] ptgs = HSSFFormulaParser.parse(formula, book);
+ int sheetIndex = book.getSheetIndex(sheet);
+ Ptg[] ptgs = HSSFFormulaParser.parse(formula, book, FormulaType.CELL, sheetIndex);
setCellType(CELL_TYPE_FORMULA, false, row, col, styleIndex);
FormulaRecordAggregate agg = (FormulaRecordAggregate) record;
FormulaRecord frec = agg.getFormulaRecord();
@@ -874,7 +876,7 @@ public class HSSFCell implements Cell {
* @see org.apache.poi.hssf.usermodel.HSSFWorkbook#getCellStyleAt(short)
*/
public void setCellStyle(CellStyle style) {
- setCellStyle( (HSSFCellStyle)style );
+ setCellStyle( (HSSFCellStyle)style );
}
public void setCellStyle(HSSFCellStyle style) {
// Verify it really does belong to our workbook
@@ -1111,8 +1113,8 @@ public class HSSFCell implements Cell {
* @param hyperlink hypelrink associated with this cell
*/
public void setHyperlink(Hyperlink hyperlink){
- HSSFHyperlink link = (HSSFHyperlink)hyperlink;
-
+ HSSFHyperlink link = (HSSFHyperlink)hyperlink;
+
link.setFirstRow(record.getRow());
link.setLastRow(record.getRow());
link.setFirstColumn(record.getColumn());
diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFDataValidation.java b/src/java/org/apache/poi/hssf/usermodel/HSSFDataValidation.java
index 0dfcbbfa63..288c8ac6e2 100644
--- a/src/java/org/apache/poi/hssf/usermodel/HSSFDataValidation.java
+++ b/src/java/org/apache/poi/hssf/usermodel/HSSFDataValidation.java
@@ -219,9 +219,9 @@ public final class HSSFDataValidation {
return _error_text;
}
- public DVRecord createDVRecord(HSSFWorkbook workbook) {
+ public DVRecord createDVRecord(HSSFSheet sheet) {
- FormulaPair fp = _constraint.createFormulas(workbook);
+ FormulaPair fp = _constraint.createFormulas(sheet);
return new DVRecord(_constraint.getValidationType(),
_constraint.getOperator(),
diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFEvaluationWorkbook.java b/src/java/org/apache/poi/hssf/usermodel/HSSFEvaluationWorkbook.java
index 9830b9f27b..046a9c2937 100644
--- a/src/java/org/apache/poi/hssf/usermodel/HSSFEvaluationWorkbook.java
+++ b/src/java/org/apache/poi/hssf/usermodel/HSSFEvaluationWorkbook.java
@@ -24,12 +24,7 @@ import org.apache.poi.hssf.record.aggregates.FormulaRecordAggregate;
import org.apache.poi.hssf.record.formula.NamePtg;
import org.apache.poi.hssf.record.formula.NameXPtg;
import org.apache.poi.hssf.record.formula.Ptg;
-import org.apache.poi.ss.formula.EvaluationCell;
-import org.apache.poi.ss.formula.EvaluationName;
-import org.apache.poi.ss.formula.EvaluationSheet;
-import org.apache.poi.ss.formula.EvaluationWorkbook;
-import org.apache.poi.ss.formula.FormulaParsingWorkbook;
-import org.apache.poi.ss.formula.FormulaRenderingWorkbook;
+import org.apache.poi.ss.formula.*;
/**
* Internal POI use only
@@ -38,118 +33,125 @@ import org.apache.poi.ss.formula.FormulaRenderingWorkbook;
*/
public final class HSSFEvaluationWorkbook implements FormulaRenderingWorkbook, EvaluationWorkbook, FormulaParsingWorkbook {
- private final HSSFWorkbook _uBook;
- private final Workbook _iBook;
-
- public static HSSFEvaluationWorkbook create(HSSFWorkbook book) {
- if (book == null) {
- return null;
- }
- return new HSSFEvaluationWorkbook(book);
- }
-
- private HSSFEvaluationWorkbook(HSSFWorkbook book) {
- _uBook = book;
- _iBook = book.getWorkbook();
- }
-
- public int getExternalSheetIndex(String sheetName) {
- int sheetIndex = _uBook.getSheetIndex(sheetName);
- return _iBook.checkExternSheet(sheetIndex);
- }
- public int getExternalSheetIndex(String workbookName, String sheetName) {
- return _iBook.getExternalSheetIndex(workbookName, sheetName);
- }
-
- public NameXPtg getNameXPtg(String name) {
- return _iBook.getNameXPtg(name);
- }
-
- public EvaluationName getName(String name) {
- for(int i=0; i < _iBook.getNumNames(); i++) {
- NameRecord nr = _iBook.getNameRecord(i);
- if (name.equalsIgnoreCase(nr.getNameText())) {
- return new Name(nr, i);
- }
- }
- return null;
- }
-
- public int getSheetIndex(EvaluationSheet evalSheet) {
- HSSFSheet sheet = ((HSSFEvaluationSheet)evalSheet).getHSSFSheet();
- return _uBook.getSheetIndex(sheet);
- }
- public int getSheetIndex(String sheetName) {
- return _uBook.getSheetIndex(sheetName);
- }
-
- public String getSheetName(int sheetIndex) {
- return _uBook.getSheetName(sheetIndex);
- }
-
- public EvaluationSheet getSheet(int sheetIndex) {
- return new HSSFEvaluationSheet(_uBook.getSheetAt(sheetIndex));
- }
- public int convertFromExternSheetIndex(int externSheetIndex) {
- return _iBook.getSheetIndexFromExternSheetIndex(externSheetIndex);
- }
-
- public ExternalSheet getExternalSheet(int externSheetIndex) {
- return _iBook.getExternalSheet(externSheetIndex);
- }
-
- public String resolveNameXText(NameXPtg n) {
- return _iBook.resolveNameXText(n.getSheetRefIndex(), n.getNameIndex());
- }
-
- public String getSheetNameByExternSheet(int externSheetIndex) {
- return _iBook.findSheetNameFromExternSheet(externSheetIndex);
- }
- public String getNameText(NamePtg namePtg) {
- return _iBook.getNameRecord(namePtg.getIndex()).getNameText();
- }
- public EvaluationName getName(NamePtg namePtg) {
- int ix = namePtg.getIndex();
- return new Name(_iBook.getNameRecord(ix), ix);
- }
- public Ptg[] getFormulaTokens(EvaluationCell evalCell) {
- HSSFCell cell = ((HSSFEvaluationCell)evalCell).getHSSFCell();
- if (false) {
- // re-parsing the formula text also works, but is a waste of time
- // It is useful from time to time to run all unit tests with this code
- // to make sure that all formulas POI can evaluate can also be parsed.
- return HSSFFormulaParser.parse(cell.getCellFormula(), _uBook);
- }
- FormulaRecordAggregate fra = (FormulaRecordAggregate) cell.getCellValueRecord();
- return fra.getFormulaTokens();
- }
-
- private static final class Name implements EvaluationName {
-
- private final NameRecord _nameRecord;
- private final int _index;
-
- public Name(NameRecord nameRecord, int index) {
- _nameRecord = nameRecord;
- _index = index;
- }
- public Ptg[] getNameDefinition() {
- return _nameRecord.getNameDefinition();
- }
- public String getNameText() {
- return _nameRecord.getNameText();
- }
- public boolean hasFormula() {
- return _nameRecord.hasFormula();
- }
- public boolean isFunctionName() {
- return _nameRecord.isFunctionName();
- }
- public boolean isRange() {
- return _nameRecord.hasFormula(); // TODO - is this right?
- }
- public NamePtg createPtg() {
- return new NamePtg(_index);
- }
- }
+ private final HSSFWorkbook _uBook;
+ private final Workbook _iBook;
+
+ public static HSSFEvaluationWorkbook create(HSSFWorkbook book) {
+ if (book == null) {
+ return null;
+ }
+ return new HSSFEvaluationWorkbook(book);
+ }
+
+ private HSSFEvaluationWorkbook(HSSFWorkbook book) {
+ _uBook = book;
+ _iBook = book.getWorkbook();
+ }
+
+ public int getExternalSheetIndex(String sheetName) {
+ int sheetIndex = _uBook.getSheetIndex(sheetName);
+ return _iBook.checkExternSheet(sheetIndex);
+ }
+ public int getExternalSheetIndex(String workbookName, String sheetName) {
+ return _iBook.getExternalSheetIndex(workbookName, sheetName);
+ }
+
+ public NameXPtg getNameXPtg(String name) {
+ return _iBook.getNameXPtg(name);
+ }
+
+ /**
+ * Lookup a named range by its name.
+ *
+ * @param name the name to search
+ * @param sheetIndex the 0-based index of the sheet this formula belongs to.
+ * The sheet index is required to resolve sheet-level names. <code>-1</code> means workbook-global names
+ */
+ public EvaluationName getName(String name, int sheetIndex) {
+ for(int i=0; i < _iBook.getNumNames(); i++) {
+ NameRecord nr = _iBook.getNameRecord(i);
+ if (nr.getSheetNumber() == sheetIndex+1 && name.equalsIgnoreCase(nr.getNameText())) {
+ return new Name(nr, i);
+ }
+ }
+ return sheetIndex == -1 ? null : getName(name, -1);
+ }
+
+ public int getSheetIndex(EvaluationSheet evalSheet) {
+ HSSFSheet sheet = ((HSSFEvaluationSheet)evalSheet).getHSSFSheet();
+ return _uBook.getSheetIndex(sheet);
+ }
+ public int getSheetIndex(String sheetName) {
+ return _uBook.getSheetIndex(sheetName);
+ }
+
+ public String getSheetName(int sheetIndex) {
+ return _uBook.getSheetName(sheetIndex);
+ }
+
+ public EvaluationSheet getSheet(int sheetIndex) {
+ return new HSSFEvaluationSheet(_uBook.getSheetAt(sheetIndex));
+ }
+ public int convertFromExternSheetIndex(int externSheetIndex) {
+ return _iBook.getSheetIndexFromExternSheetIndex(externSheetIndex);
+ }
+
+ public ExternalSheet getExternalSheet(int externSheetIndex) {
+ return _iBook.getExternalSheet(externSheetIndex);
+ }
+
+ public String resolveNameXText(NameXPtg n) {
+ return _iBook.resolveNameXText(n.getSheetRefIndex(), n.getNameIndex());
+ }
+
+ public String getSheetNameByExternSheet(int externSheetIndex) {
+ return _iBook.findSheetNameFromExternSheet(externSheetIndex);
+ }
+ public String getNameText(NamePtg namePtg) {
+ return _iBook.getNameRecord(namePtg.getIndex()).getNameText();
+ }
+ public EvaluationName getName(NamePtg namePtg) {
+ int ix = namePtg.getIndex();
+ return new Name(_iBook.getNameRecord(ix), ix);
+ }
+ public Ptg[] getFormulaTokens(EvaluationCell evalCell) {
+ HSSFCell cell = ((HSSFEvaluationCell)evalCell).getHSSFCell();
+ if (false) {
+ // re-parsing the formula text also works, but is a waste of time
+ // It is useful from time to time to run all unit tests with this code
+ // to make sure that all formulas POI can evaluate can also be parsed.
+ return HSSFFormulaParser.parse(cell.getCellFormula(), _uBook, FormulaType.CELL, _uBook.getSheetIndex(cell.getSheet()));
+ }
+ FormulaRecordAggregate fra = (FormulaRecordAggregate) cell.getCellValueRecord();
+ return fra.getFormulaTokens();
+ }
+
+ private static final class Name implements EvaluationName {
+
+ private final NameRecord _nameRecord;
+ private final int _index;
+
+ public Name(NameRecord nameRecord, int index) {
+ _nameRecord = nameRecord;
+ _index = index;
+ }
+ public Ptg[] getNameDefinition() {
+ return _nameRecord.getNameDefinition();
+ }
+ public String getNameText() {
+ return _nameRecord.getNameText();
+ }
+ public boolean hasFormula() {
+ return _nameRecord.hasFormula();
+ }
+ public boolean isFunctionName() {
+ return _nameRecord.isFunctionName();
+ }
+ public boolean isRange() {
+ return _nameRecord.hasFormula(); // TODO - is this right?
+ }
+ public NamePtg createPtg() {
+ return new NamePtg(_index);
+ }
+ }
}
diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFName.java b/src/java/org/apache/poi/hssf/usermodel/HSSFName.java
index 1865eaf0b8..8c21ae9962 100644
--- a/src/java/org/apache/poi/hssf/usermodel/HSSFName.java
+++ b/src/java/org/apache/poi/hssf/usermodel/HSSFName.java
@@ -163,7 +163,7 @@ public final class HSSFName implements Name {
* @throws IllegalArgumentException if the specified reference is unparsable
*/
public void setRefersToFormula(String formulaText) {
- Ptg[] ptgs = HSSFFormulaParser.parse(formulaText, _book, FormulaType.NAMEDRANGE);
+ Ptg[] ptgs = HSSFFormulaParser.parse(formulaText, _book, FormulaType.NAMEDRANGE, getSheetIndex());
_definedNameRec.setNameDefinition(ptgs);
}
diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java b/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java
index 08abbb61f6..ac84059467 100644
--- a/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java
+++ b/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java
@@ -367,7 +367,7 @@ public final class HSSFSheet implements org.apache.poi.ss.usermodel.Sheet {
}
DataValidityTable dvt = sheet.getOrCreateDataValidityTable();
- DVRecord dvRecord = dataValidation.createDVRecord(workbook);
+ DVRecord dvRecord = dataValidation.createDVRecord(this);
dvt.addDataValidation(dvRecord);
}
@@ -1789,6 +1789,6 @@ public final class HSSFSheet implements org.apache.poi.ss.usermodel.Sheet {
}
public HSSFSheetConditionalFormatting getSheetConditionalFormatting() {
- return new HSSFSheetConditionalFormatting(workbook, sheet);
+ return new HSSFSheetConditionalFormatting(this);
}
}
diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFSheetConditionalFormatting.java b/src/java/org/apache/poi/hssf/usermodel/HSSFSheetConditionalFormatting.java
index afaeb8031c..f3c93611ee 100644
--- a/src/java/org/apache/poi/hssf/usermodel/HSSFSheetConditionalFormatting.java
+++ b/src/java/org/apache/poi/hssf/usermodel/HSSFSheetConditionalFormatting.java
@@ -31,13 +31,13 @@ import org.apache.poi.ss.util.CellRangeAddress;
*/
public final class HSSFSheetConditionalFormatting {
- private final HSSFWorkbook _workbook;
+ private final HSSFSheet _sheet;
private final ConditionalFormattingTable _conditionalFormattingTable;
- /* package */ HSSFSheetConditionalFormatting(HSSFWorkbook workbook, Sheet sheet) {
- _workbook = workbook;
- _conditionalFormattingTable = sheet.getConditionalFormattingTable();
- }
+ /* package */ HSSFSheetConditionalFormatting(HSSFSheet sheet) {
+ _sheet = sheet;
+ _conditionalFormattingTable = sheet.getSheet().getConditionalFormattingTable();
+ }
/**
* A factory method allowing to create a conditional formatting rule
@@ -67,8 +67,8 @@ public final class HSSFSheetConditionalFormatting {
String formula1,
String formula2) {
- HSSFWorkbook wb = _workbook;
- CFRuleRecord rr = CFRuleRecord.create(wb, comparisonOperation, formula1, formula2);
+ HSSFWorkbook wb = _sheet.getWorkbook();
+ CFRuleRecord rr = CFRuleRecord.create(_sheet, comparisonOperation, formula1, formula2);
return new HSSFConditionalFormattingRule(wb, rr);
}
@@ -80,8 +80,8 @@ public final class HSSFSheetConditionalFormatting {
* @param formula - formula for the valued, compared with the cell
*/
public HSSFConditionalFormattingRule createConditionalFormattingRule(String formula) {
- HSSFWorkbook wb = _workbook;
- CFRuleRecord rr = CFRuleRecord.create(wb, formula);
+ HSSFWorkbook wb = _sheet.getWorkbook();
+ CFRuleRecord rr = CFRuleRecord.create(_sheet, formula);
return new HSSFConditionalFormattingRule(wb, rr);
}
@@ -171,7 +171,7 @@ public final class HSSFSheetConditionalFormatting {
if (cf == null) {
return null;
}
- return new HSSFConditionalFormatting(_workbook, cf);
+ return new HSSFConditionalFormatting(_sheet.getWorkbook(), cf);
}
/**
diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java b/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java
index 3b47ea31ba..bb2a23bf5e 100644
--- a/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java
+++ b/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java
@@ -66,6 +66,7 @@ import org.apache.poi.poifs.filesystem.DirectoryNode;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.CreationHelper;
import org.apache.poi.ss.usermodel.Row.MissingCellPolicy;
+import org.apache.poi.ss.formula.FormulaType;
import org.apache.poi.util.POILogFactory;
import org.apache.poi.util.POILogger;
@@ -1381,7 +1382,7 @@ public class HSSFWorkbook extends POIDocument implements org.apache.poi.ss.userm
sb.append("!");
sb.append(parts[i]);
}
- name.setNameDefinition(HSSFFormulaParser.parse(sb.toString(), this));
+ name.setNameDefinition(HSSFFormulaParser.parse(sb.toString(), this, FormulaType.CELL, sheetIndex));
}
/**
diff --git a/src/java/org/apache/poi/ss/formula/FormulaParser.java b/src/java/org/apache/poi/ss/formula/FormulaParser.java
index 4551c35853..1a3edcdd59 100644
--- a/src/java/org/apache/poi/ss/formula/FormulaParser.java
+++ b/src/java/org/apache/poi/ss/formula/FormulaParser.java
@@ -142,6 +142,7 @@ public final class FormulaParser {
private FormulaParsingWorkbook _book;
+ private int _sheetIndex;
/**
@@ -156,11 +157,12 @@ public final class FormulaParser {
* model.Workbook, then use the convenience method on
* usermodel.HSSFFormulaEvaluator
*/
- private FormulaParser(String formula, FormulaParsingWorkbook book){
+ private FormulaParser(String formula, FormulaParsingWorkbook book, int sheetIndex){
_formulaString = formula;
_pointer=0;
_book = book;
_formulaLength = _formulaString.length();
+ _sheetIndex = sheetIndex;
}
public static Ptg[] parse(String formula, FormulaParsingWorkbook book) {
@@ -168,7 +170,24 @@ public final class FormulaParser {
}
public static Ptg[] parse(String formula, FormulaParsingWorkbook workbook, int formulaType) {
- FormulaParser fp = new FormulaParser(formula, workbook);
+ return parse(formula, workbook, formulaType, -1);
+ }
+
+ /**
+ * Parse a formula into a array of tokens
+ *
+ * @param formula the formula to parse
+ * @param workbook the parent workbook
+ * @param formulaType the type of the formula, see {@link FormulaType}
+ * @param sheetIndex the 0-based index of the sheet this formula belongs to.
+ * The sheet index is required to resolve sheet-level names. <code>-1</code> means that
+ * the scope of the name will be ignored and the parser will match names only by name
+ *
+ * @return array of parsed tokens
+ * @throws FormulaParseException if the formula is unparsable
+ */
+ public static Ptg[] parse(String formula, FormulaParsingWorkbook workbook, int formulaType, int sheetIndex) {
+ FormulaParser fp = new FormulaParser(formula, workbook, sheetIndex);
fp.parse();
return fp.getRPNPtg(formulaType);
}
@@ -413,7 +432,7 @@ public final class FormulaParser {
new FormulaParseException("Name '" + name
+ "' does not look like a cell reference or named range");
}
- EvaluationName evalName = _book.getName(name);
+ EvaluationName evalName = _book.getName(name, _sheetIndex);
if (evalName == null) {
throw new FormulaParseException("Specified named range '"
+ name + "' does not exist in the current workbook.");
@@ -516,7 +535,7 @@ public final class FormulaParser {
// user defined function
// in the token tree, the name is more or less the first argument
- EvaluationName hName = _book.getName(name);
+ EvaluationName hName = _book.getName(name, _sheetIndex);
if (hName == null) {
nameToken = _book.getNameXPtg(name);
diff --git a/src/java/org/apache/poi/ss/formula/FormulaParsingWorkbook.java b/src/java/org/apache/poi/ss/formula/FormulaParsingWorkbook.java
index e9be7d1d39..57f0f7a2c4 100644
--- a/src/java/org/apache/poi/ss/formula/FormulaParsingWorkbook.java
+++ b/src/java/org/apache/poi/ss/formula/FormulaParsingWorkbook.java
@@ -30,7 +30,7 @@ public interface FormulaParsingWorkbook {
/**
* named range name matching is case insensitive
*/
- EvaluationName getName(String name);
+ EvaluationName getName(String name, int sheetIndex);
NameXPtg getNameXPtg(String name);