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


##########
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:
   Confirmed and fixed in 5d89c7baf7. An empty trailing section is now added 
only when the last match ended with a real separator. The same check now 
decides whether to strip the `;` from a section: it was stripped even when 
`\;`, `_;` or `*;` had taken it, which was already so before this PR. `0\;` 
shows -3 as `-3;`, as Excel does. Tests added.



##########
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:
   Both addressed in 5d89c7baf7.
   
   1. Formats with a leading or trailing `;` now go to `CellFormat`, with a 
`DataFormatter` test.
   2. Fixed, but not as an empty section: Excel (checked in Excel 2007) shows 
such a section as General and saves `0;[Red]` as `0;[Red]General`. So -3 is 
shown as `3` in red.
   
   An empty format (`getInstance("")`, from the review summary) is now General 
too.



##########
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:
   I checked in Excel 2007, and it treats the first `.` as the decimal point 
even when no digit follows it directly:
   
   | format | value | Excel | this PR |
   |---|---|---|---|
   | `"Nr". 000` | 5 | `Nr5. 000` | `Nr5. 000` |
   | `...0` | 0.5 | `...5` | `...5` |
   | `. 0` | 12.5 | `12. 5` | `12. 5` |
   
   So I kept this behaviour and added tests (5d89c7baf7). Unquoted `Nr. 000` is 
rejected by Excel's format editor.



##########
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:
   Agreed that the injected `#` was wrong here. In scientific and fraction 
formats a leading point is now left as before this PR (5d89c7baf7), with tests. 
This also fixes `. ?/?`, which threw `StringIndexOutOfBoundsException` with the 
injected `#`.
   
   These formats still differ from Excel, as they did before: Excel shows 5 as 
`.5E+1` for `.0E+0` (POI `CellFormat`: `.5E+0`).



-- 
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