Top Description Fields Constructors Methods
org.apache.poi.ss.formula

public final Class SheetNameFormatter

extends Object
Class Inheritance
Imports
java.io.IOException, java.util.regex.Matcher, .Pattern, org.apache.poi.ss.util.CellReference, org.apache.poi.ss.SpreadsheetVersion

Formats sheet names for use in formula expressions.

Field Summary

Modifier and TypeField and Description
private static final Pattern
CELL_REF_PATTERN

Matches a single cell ref with no absolute ('$') markers

private static final char

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
pack-priv static void
appendAndEscape(Appendable sb, String rawSheetName)

public static void
appendFormat(Appendable
- sheet name will be appended here possibly with delimiting quotes
out
,
String
- sheet name
rawSheetName
)

Convenience method for (format(String)) when a StringBuffer is already available.

public static void
appendFormat(Appendable
- sheet name will be appended here possibly with delimiting quotes
out
,
String
- workbook name
workbookName
,
String
- sheet name
rawSheetName
)

Convenience method for (format(String)) when a StringBuffer is already available.

private static void
pack-priv static boolean
cellReferenceIsWithinRange(String lettersPrefix, String numbersSuffix)

Used to decide whether sheet names like 'AB123' need delimiting due to the fact that they look like cell references.

public static String

Returns:

the sheet name unchanged if there is no need for delimiting. Otherwise the sheet name is enclosed in single quotes ('). Any single quotes which were already present in the sheet name will be converted to double single quotes ('').
format
(String rawSheetName)

Used to format sheet names as they would appear in cell formula expressions.

pack-priv static boolean

Returns:

true if the presence of the specified character in a sheet name would require the sheet name to be delimited in formulas. This includes every non-alphanumeric character besides underscore '_' and dot '.'.
isSpecialChar
(char ch)

private static boolean
pack-priv static boolean

Returns:

true if there is any possible ambiguity that the specified rawSheetName could be interpreted as a valid cell name.
nameLooksLikePlainCellReference
(String rawSheetName)

Note - this method assumes the specified rawSheetName has only letters and digits.

pack-priv static boolean

Returns:

true if the specified rawSheetName starts with R1C1 style cell reference
nameStartsWithR1C1CellReference
(String rawSheetName)

Checks if the sheet name starts with R1C1 style cell reference.

pack-priv static boolean

Returns:

true if the given raw sheet name needs screening/delimiting, false otherwise or if the sheet name is null.
needsDelimiting
(String
the sheet name.
rawSheetName
)

Tell if the given raw sheet name needs screening/delimiting.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

CELL_REF_PATTERNback to summary
private static final Pattern CELL_REF_PATTERN

Matches a single cell ref with no absolute ('$') markers

DELIMITERback to summary
private static final char DELIMITER

Constructor Detail

SheetNameFormatterback to summary
private SheetNameFormatter()

Method Detail

appendAndEscapeback to summary
pack-priv static void appendAndEscape(Appendable sb, String rawSheetName)
appendFormatback to summary
public static void appendFormat(Appendable out, String rawSheetName)

Convenience method for (format(String)) when a StringBuffer is already available.

Parameters
out:Appendable

- sheet name will be appended here possibly with delimiting quotes

rawSheetName:String

- sheet name

appendFormatback to summary
public static void appendFormat(Appendable out, String workbookName, String rawSheetName)

Convenience method for (format(String)) when a StringBuffer is already available.

Parameters
out:Appendable

- sheet name will be appended here possibly with delimiting quotes

workbookName:String

- workbook name

rawSheetName:String

- sheet name

appendOrREFback to summary
private static void appendOrREF(Appendable out, String name) throws IOException
cellReferenceIsWithinRangeback to summary
pack-priv static boolean cellReferenceIsWithinRange(String lettersPrefix, String numbersSuffix)

Used to decide whether sheet names like 'AB123' need delimiting due to the fact that they look like cell references.

This code is currently being used for translating formulas represented with Ptg tokens into human readable text form. In formula expressions, a sheet name always has a trailing '!' so there is little chance for ambiguity. It doesn't matter too much what this method returns but it is worth noting the likely consumers of these formula text strings:

  1. POI's own formula parser
  2. Visual reading by human
  3. VBA automation entry into Excel cell contents e.g. ActiveCell.Formula = "=c64!A1"
  4. Manual entry into Excel cell contents
  5. Some third party formula parser
At the time of writing, POI's formula parser tolerates cell-like sheet names in formulas with or without delimiters. The same goes for Excel(2007), both manual and automated entry.

For better or worse this implementation attempts to replicate Excel's formula renderer. Excel uses range checking on the apparent 'row' and 'column' components. Note however that the maximum sheet size varies across versions.

See Also
org.apache.poi.ss.util.CellReference
formatback to summary
public static String format(String rawSheetName)

Used to format sheet names as they would appear in cell formula expressions.

Returns:String

the sheet name unchanged if there is no need for delimiting. Otherwise the sheet name is enclosed in single quotes ('). Any single quotes which were already present in the sheet name will be converted to double single quotes ('').

isSpecialCharback to summary
pack-priv static boolean isSpecialChar(char ch)
Returns:boolean

true if the presence of the specified character in a sheet name would require the sheet name to be delimited in formulas. This includes every non-alphanumeric character besides underscore '_' and dot '.'.

nameLooksLikeBooleanLiteralback to summary
private static boolean nameLooksLikeBooleanLiteral(String rawSheetName)
nameLooksLikePlainCellReferenceback to summary
pack-priv static boolean nameLooksLikePlainCellReference(String rawSheetName)

Note - this method assumes the specified rawSheetName has only letters and digits. It cannot be used to match absolute or range references (using the dollar or colon char).

Some notable cases:

Notable cases
Input Result Comments
"A1"  true 
"a111"  true 
"AA"  false 
"aa1"  true 
"A1A"  false 
"A1A1"  false 
"A$1:$C$20"  falseNot a plain cell reference
"SALES20080101"  true Still needs delimiting even though well out of range
Returns:boolean

true if there is any possible ambiguity that the specified rawSheetName could be interpreted as a valid cell name.

nameStartsWithR1C1CellReferenceback to summary
pack-priv static boolean nameStartsWithR1C1CellReference(String rawSheetName)

Checks if the sheet name starts with R1C1 style cell reference. If this is the case Excel requires the sheet name to be enclosed in single quotes.

Returns:boolean

true if the specified rawSheetName starts with R1C1 style cell reference

needsDelimitingback to summary
pack-priv static boolean needsDelimiting(String rawSheetName)

Tell if the given raw sheet name needs screening/delimiting.

Parameters
rawSheetName:String

the sheet name.

Returns:boolean

true if the given raw sheet name needs screening/delimiting, false otherwise or if the sheet name is null.