pjfanning commented on code in PR #1597:
URL: https://github.com/apache/poi/pull/1597#discussion_r4726381091


##########
poi/src/main/java/org/apache/poi/ss/format/CellFormat.java:
##########
@@ -212,9 +214,15 @@ private CellFormat(Locale locale, String format) {
         Matcher m = ONE_PART.matcher(format);
         List<CellFormatPart> parts = new ArrayList<>();
         String lastDesc = "";
+        int end = 0;
 
         try {
             while (m.find()) {
+                // As in Excel, an empty section shows nothing: 0;-0;;@ hides 
zeros and ;;; hides every value
+                if (addEmptySections(locale, parts, format.substring(end, 
m.start()))) {

Review Comment:
   Two things here:
   
   1. `DataFormatter.getFormat` only hands off to `CellFormat` when the format 
has two or more `;` or a range condition, so formats such as `0;` and `;-0` 
still take the `DecimalFormat` path there. That means 
`CellFormat.getInstance("0;").apply(-3.0)` returns an empty string (as the new 
test asserts) but `DataFormatter.formatRawCellContents(-3, -1, "0;")` still 
shows the negative number. Should `DataFormatter` also route formats with a 
leading or trailing `;` to `CellFormat`? A `DataFormatter` test would help 
either way.
   
   2. Not new in this PR, but related: a section that has only a colour or 
condition and no format text (e.g. `0;[Red]`) is not seen by 
`addEmptySections`, because `[Red]` matches `ONE_PART`. The `CellFormatPart` 
constructor then throws, `parts.add(null)` runs, and applying the format to a 
negative number throws `IllegalStateException` rather than showing nothing. It 
might be worth treating that as an empty section as well (keeping the 
colour/condition).



##########
poi/src/main/java/org/apache/poi/ss/format/CellFormat.java:
##########
@@ -229,6 +237,11 @@ private CellFormat(Locale locale, String format) {
                     parts.add(null);
                 }
             }
+            String rest = format.substring(end);
+            boolean separatorLast = !format.isEmpty() && 
format.charAt(format.length() - 1) == ';';

Review Comment:
   This only checks that the last character is `;`, but that semicolon may be a 
literal that the last section already consumed via `\;`, `_;` or `*;`. In that 
case `rest` is empty, `separatorLast` is true, and a spurious empty section is 
appended.
   
   For example, I'd expect the format `0\;` applied to -3 to now give an empty 
string instead of `-3;`, and `0;-0\;` to hide zeros.
   
   Could the trailing empty section be added only when the last `ONE_PART` 
match actually ended with its `;` separator group? A test for `0\;` would be 
good.



##########
poi/src/main/java/org/apache/poi/ss/format/CellNumberPartHandler.java:
##########
@@ -82,7 +82,15 @@ public String handlePart(Matcher m, String part, 
CellFormatType type, StringBuff
             break;
 
         case '.':
-            if (decimalPoint == null && !specials.isEmpty()) {
+            if (decimalPoint == null && specials.isEmpty()) {

Review Comment:
   This fires for any unquoted `.` that comes before the first digit 
placeholder, even when the dot is literal text that is not adjacent to the 
digits. For example `Nr. 000` applied to 5 used to give `Nr. 005`; with this 
change the `.` becomes `#.` and the following `000` is counted as fraction 
digits. `...0` looks similarly affected.
   
   Have you checked what Excel does with formats like these? If it differs, 
could this path be taken only when a digit placeholder immediately follows the 
`.`?



##########
poi/src/main/java/org/apache/poi/ss/format/CellNumberPartHandler.java:
##########
@@ -82,7 +82,15 @@ public String handlePart(Matcher m, String part, 
CellFormatType type, StringBuff
             break;
 
         case '.':
-            if (decimalPoint == null && !specials.isEmpty()) {
+            if (decimalPoint == null && specials.isEmpty()) {
+                // As in Excel, a decimal point before any digit placeholder 
still shows the integer digits:
+                // .00 shows 0.5 as .50 and 12.5 as 12.50, as #.00 does
+                specials.add(new Special('#', pos));

Review Comment:
   Injecting the `#` makes `specials` non-empty, which also unlocks the `e` and 
`/` branches that were previously skipped when no digit preceded them. With a 
format like `./0` or `.E+0`, the `/` or exponent now gets registered against 
the injected `#` (via `previousNumber()`), where previously those characters 
were plain literals. Only injecting the `#` when a digit placeholder follows 
the `.` would cover this too; tests for `./0` and `.E+0` would be useful.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to