aboutsummaryrefslogtreecommitdiffstats
path: root/poi/src
diff options
context:
space:
mode:
authorPJ Fanning <fanningpj@apache.org>2021-10-25 09:32:50 +0000
committerPJ Fanning <fanningpj@apache.org>2021-10-25 09:32:50 +0000
commit6017327bae3201a96cdbcdc1d809453e9dba8182 (patch)
treeaded78b428eaca00133ed41def8d13c7b315bbac /poi/src
parent7e17a5784899408bc349c168a775b2772fbb806a (diff)
downloadpoi-6017327bae3201a96cdbcdc1d809453e9dba8182.tar.gz
poi-6017327bae3201a96cdbcdc1d809453e9dba8182.zip
[github-267] Issue with sheet range in formula With spaces and quotes. Thanks to aspojo. This closes #267
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1894549 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'poi/src')
-rw-r--r--poi/src/main/java/org/apache/poi/ss/formula/FormulaParser.java13
-rw-r--r--poi/src/main/java/org/apache/poi/ss/formula/ptg/ExternSheetNameResolver.java11
-rw-r--r--poi/src/test/java/org/apache/poi/hssf/model/TestFormulaParser.java8
3 files changed, 23 insertions, 9 deletions
diff --git a/poi/src/main/java/org/apache/poi/ss/formula/FormulaParser.java b/poi/src/main/java/org/apache/poi/ss/formula/FormulaParser.java
index 94c88219e9..8be98c5656 100644
--- a/poi/src/main/java/org/apache/poi/ss/formula/FormulaParser.java
+++ b/poi/src/main/java/org/apache/poi/ss/formula/FormulaParser.java
@@ -1171,10 +1171,11 @@ public final class FormulaParser {
while(!done) {
sb.appendCodePoint(look);
GetChar();
- if(look == '\'')
- {
- Match('\'');
- done = look != '\'';
+ switch (look){
+ case '\'' :
+ GetChar();
+ case ':' :
+ done = true;
}
}
@@ -1200,6 +1201,9 @@ public final class FormulaParser {
sb.appendCodePoint(look);
GetChar();
}
+ if (look == '\'') {
+ GetChar();
+ }
NameIdentifier iden = new NameIdentifier(sb.toString(), false);
SkipWhite();
if (look == '!') {
@@ -1249,6 +1253,7 @@ public final class FormulaParser {
switch(ch) {
case '.': // dot is OK
case '_': // underscore is OK
+ case ' ': // space is OK
return true;
}
return false;
diff --git a/poi/src/main/java/org/apache/poi/ss/formula/ptg/ExternSheetNameResolver.java b/poi/src/main/java/org/apache/poi/ss/formula/ptg/ExternSheetNameResolver.java
index 3a35909cc2..2a90ae6f44 100644
--- a/poi/src/main/java/org/apache/poi/ss/formula/ptg/ExternSheetNameResolver.java
+++ b/poi/src/main/java/org/apache/poi/ss/formula/ptg/ExternSheetNameResolver.java
@@ -44,7 +44,16 @@ final class ExternSheetNameResolver {
ExternalSheetRange r = (ExternalSheetRange)externalSheet;
if (! r.getFirstSheetName().equals(r.getLastSheetName())) {
sb.append(':');
- SheetNameFormatter.appendFormat(sb, r.getLastSheetName());
+ // quote should appear at the beginning and end.
+ StringBuilder innerBuilder = new StringBuilder();
+ SheetNameFormatter.appendFormat(innerBuilder, r.getLastSheetName());
+ char quote = '\'';
+ if (innerBuilder.charAt(0) == quote){
+ sb.insert(0 , quote);
+ sb.append(innerBuilder.substring(1));
+ } else {
+ sb.append(innerBuilder);
+ }
}
}
} else {
diff --git a/poi/src/test/java/org/apache/poi/hssf/model/TestFormulaParser.java b/poi/src/test/java/org/apache/poi/hssf/model/TestFormulaParser.java
index 6c55d18224..0768cd713e 100644
--- a/poi/src/test/java/org/apache/poi/hssf/model/TestFormulaParser.java
+++ b/poi/src/test/java/org/apache/poi/hssf/model/TestFormulaParser.java
@@ -535,9 +535,9 @@ final class TestFormulaParser {
assertEquals("'Test Sheet'!A1", formula);
// Now both
- cell.setCellFormula("Cash_Flow:'Test Sheet'!A1");
+ cell.setCellFormula("'Cash_Flow:Test Sheet'!A1");
formula = cell.getCellFormula();
- assertEquals("Cash_Flow:'Test Sheet'!A1", formula);
+ assertEquals("'Cash_Flow:Test Sheet'!A1", formula);
// References to a range (area) of cells:
@@ -553,9 +553,9 @@ final class TestFormulaParser {
assertEquals("'Test Sheet'!A1:B2", formula);
// Now both
- cell.setCellFormula("Cash_Flow:'Test Sheet'!A1:B2");
+ cell.setCellFormula("'Cash_Flow:Test Sheet'!A1:B2");
formula = cell.getCellFormula();
- assertEquals("Cash_Flow:'Test Sheet'!A1:B2", formula);
+ assertEquals("'Cash_Flow:Test Sheet'!A1:B2", formula);
wb.close();
}