diff options
author | PJ Fanning <fanningpj@apache.org> | 2020-01-09 21:19:34 +0000 |
---|---|---|
committer | PJ Fanning <fanningpj@apache.org> | 2020-01-09 21:19:34 +0000 |
commit | b594ec2947685dd658bfa19107d71f8716736d2a (patch) | |
tree | 9a8526e166b0a7bbdee059f362c5233623c0fe00 /src/java/org/apache/poi/ss/util | |
parent | 11b2c7e8985294679eb315da9d78cf17c87bcca3 (diff) | |
download | poi-b594ec2947685dd658bfa19107d71f8716736d2a.tar.gz poi-b594ec2947685dd658bfa19107d71f8716736d2a.zip |
[bug-63998] Support commas, exclamation marks correctly in AreaReference. Thanks to hzwhuang
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1872567 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/java/org/apache/poi/ss/util')
-rw-r--r-- | src/java/org/apache/poi/ss/util/AreaReference.java | 45 |
1 files changed, 32 insertions, 13 deletions
diff --git a/src/java/org/apache/poi/ss/util/AreaReference.java b/src/java/org/apache/poi/ss/util/AreaReference.java index 4dd4771be6..a832fec6b4 100644 --- a/src/java/org/apache/poi/ss/util/AreaReference.java +++ b/src/java/org/apache/poi/ss/util/AreaReference.java @@ -22,6 +22,7 @@ import java.util.List; import java.util.StringTokenizer; import org.apache.poi.ss.SpreadsheetVersion; +import org.apache.poi.util.StringUtil; public class AreaReference { @@ -166,14 +167,7 @@ public class AreaReference { * {@link #generateContiguous(SpreadsheetVersion, String)}) */ public static boolean isContiguous(String reference) { - // If there's a sheet name, strip it off - int sheetRefEnd = reference.indexOf('!'); - if(sheetRefEnd != -1) { - reference = reference.substring(sheetRefEnd); - } - - // Check for the , as a sign of non-coniguous - return !reference.contains(","); + return splitAreaReferences(reference).length == 1; } public static AreaReference getWholeRow(SpreadsheetVersion version, String start, String end) { @@ -220,11 +214,9 @@ public class AreaReference { version = DEFAULT_SPREADSHEET_VERSION; // how the code used to behave. } List<AreaReference> refs = new ArrayList<>(); - StringTokenizer st = new StringTokenizer(reference, ","); - while(st.hasMoreTokens()) { - refs.add( - new AreaReference(st.nextToken(), version) - ); + String[] splitReferences = splitAreaReferences(reference); + for (String ref : splitReferences) { + refs.add(new AreaReference(ref, version)); } return refs.toArray(new AreaReference[0]); } @@ -405,4 +397,31 @@ public class AreaReference { return new String [] { partA, sheetName + partB, }; } + + /** + * Splits a comma-separated area references string into an array of + * individual references + * @param reference Area references, i.e. A1:B2, 'Sheet1'!A1:B2 + * @return Area references in an array, size >= 1 + */ + private static String[] splitAreaReferences(String reference) { + List<String> results = new ArrayList<>(); + String currentSegment = ""; + StringTokenizer st = new StringTokenizer(reference, ","); + while(st.hasMoreTokens()) { + if (currentSegment.length() > 0) { + currentSegment += ","; + } + currentSegment += st.nextToken(); + int numSingleQuotes = StringUtil.countMatches(currentSegment, '\''); + if (numSingleQuotes == 0 || numSingleQuotes == 2) { + results.add(currentSegment); + currentSegment = ""; + } + } + if (currentSegment.length() > 0) { + results.add(currentSegment); + } + return results.toArray(new String[0]); + } } |