Skip to content

Commit

Permalink
[bug-67784] experimental hack to fix regression
Browse files Browse the repository at this point in the history
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1913067 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
pjfanning committed Oct 17, 2023
1 parent 0dd2b18 commit 818091f
Showing 1 changed file with 66 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,9 @@ public void startElement(String uri, String localName, String qName,
formula.setLength(0);

// Mark us as being a formula if not already
nextDataType = xssfDataType.FORMULA;
if (this.nextDataType == XSSFSheetXMLHandler.xssfDataType.NUMBER) {
this.nextDataType = XSSFSheetXMLHandler.xssfDataType.FORMULA;
}

// Decide where to get the formula string from
String type = attributes.getValue("t");
Expand Down Expand Up @@ -260,6 +262,7 @@ public void startElement(String uri, String localName, String qName,
// c => cell
else if ("c".equals(localName)) {
// Set up defaults.
this.formula.setLength(0);
this.nextDataType = xssfDataType.NUMBER;
this.formatIndex = -1;
this.formatString = null;
Expand Down Expand Up @@ -367,68 +370,72 @@ private void outputCell() {
String thisStr = null;

// Process the value contents as required, now we have it all
switch (nextDataType) {
case BOOLEAN:
char first = value.charAt(0);
thisStr = first == '0' ? "FALSE" : "TRUE";
break;

case ERROR:
thisStr = "ERROR:" + value;
break;

case FORMULA:
if (formulasNotResults) {
thisStr = formula.toString();
} else {
String fv = value.toString();

if (this.formatString != null) {
try {
// Try to use the value as a formattable number
double d = Double.parseDouble(fv);
thisStr = formatter.formatRawCellContents(d, this.formatIndex, this.formatString);
} catch (NumberFormatException e) {
// Formula is a String result not a Numeric one
if (formulasNotResults && formula.length() > 0) {
thisStr = formula.toString();
} else {
switch (nextDataType) {
case BOOLEAN:
char first = value.charAt(0);
thisStr = first == '0' ? "FALSE" : "TRUE";
break;

case ERROR:
thisStr = "ERROR:" + value;
break;

case FORMULA:
if (formulasNotResults) {
thisStr = formula.toString();
} else {
String fv = value.toString();

if (this.formatString != null) {
try {
// Try to use the value as a formattable number
double d = Double.parseDouble(fv);
thisStr = formatter.formatRawCellContents(d, this.formatIndex, this.formatString);
} catch (NumberFormatException e) {
// Formula is a String result not a Numeric one
thisStr = fv;
}
} else {
// No formatting applied, just do raw value in all cases
thisStr = fv;
}
} else {
// No formatting applied, just do raw value in all cases
thisStr = fv;
}
}
break;

case INLINE_STRING:
// TODO: Can these ever have formatting on them?
XSSFRichTextString rtsi = new XSSFRichTextString(value.toString());
thisStr = rtsi.toString();
break;

case SST_STRING:
String sstIndex = value.toString();
if (sstIndex.length() > 0) {
try {
int idx = Integer.parseInt(sstIndex);
RichTextString rtss = sharedStringsTable.getItemAt(idx);
thisStr = rtss.toString();
} catch (NumberFormatException ex) {
LOG.atError().withThrowable(ex).log("Failed to parse SST index '{}'", sstIndex);
break;

case INLINE_STRING:
// TODO: Can these ever have formatting on them?
XSSFRichTextString rtsi = new XSSFRichTextString(value.toString());
thisStr = rtsi.toString();
break;

case SST_STRING:
String sstIndex = value.toString();
if (sstIndex.length() > 0) {
try {
int idx = Integer.parseInt(sstIndex);
RichTextString rtss = sharedStringsTable.getItemAt(idx);
thisStr = rtss.toString();
} catch (NumberFormatException ex) {
LOG.atError().withThrowable(ex).log("Failed to parse SST index '{}'", sstIndex);
}
}
}
break;

case NUMBER:
String n = value.toString();
if (this.formatString != null && n.length() > 0)
thisStr = formatter.formatRawCellContents(Double.parseDouble(n), this.formatIndex, this.formatString);
else
thisStr = n;
break;

default:
thisStr = "(TODO: Unexpected type: " + nextDataType + ")";
break;
break;

case NUMBER:
String n = value.toString();
if (this.formatString != null && n.length() > 0)
thisStr = formatter.formatRawCellContents(Double.parseDouble(n), this.formatIndex, this.formatString);
else
thisStr = n;
break;

default:
thisStr = "(TODO: Unexpected type: " + nextDataType + ")";
break;
}
}

// Do we have a comment for this cell?
Expand Down

0 comments on commit 818091f

Please sign in to comment.