Skip to content

Commit

Permalink
Fix issue #85 with string values in Excel
Browse files Browse the repository at this point in the history
  • Loading branch information
miachm committed Dec 22, 2023
1 parent df9b7b3 commit 483231d
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 7 deletions.
Binary file added resources/excelFileStringValue.ods
Binary file not shown.
3 changes: 1 addition & 2 deletions resources/features/bugs/crashExcel.feature
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@ Feature: Bugs related to loading a file generated by SODS in Excel
Then the tag is "office:document-styles"

Scenario: Excel does not read values
When load a spreadsheet from the resource "emptyCell"
When load a spreadsheet from the resource "excelFileStringValue"
And save the spreadsheet in the memory
And gets the "content.xml" entry in the spreadsheet saved in the memory
And gets the first tag "table:table-cell" of the xml entry
Then the tag has the attribute "office:value-type" with the value "string"
Then the tag has the attribute "office:value" with the value "Test 1"
7 changes: 5 additions & 2 deletions src/com/github/miachm/sods/OdsWritter.java
Original file line number Diff line number Diff line change
Expand Up @@ -283,9 +283,12 @@ private void writeValue(XMLStreamWriter out, Cell cell) throws XMLStreamExceptio
LibreOffice only writes the "string-value" attribute for formulaic cells. Writing it for non-formulaic
cells makes LibreOffice discard newlines when opening the sheet.
*/
//if (valueType != OfficeValueType.STRING || cell.getFormula() != null) {
if (valueType != OfficeValueType.STRING || cell.getFormula() != null) {
valueType.write(v, out);
//}
}
else if (valueType == OfficeValueType.STRING) {
out.writeAttribute(OFFICE, "value-type", "string");
}

out.writeStartElement(TEXT, "p");
String text = v.toString();
Expand Down
2 changes: 1 addition & 1 deletion src/com/github/miachm/sods/OfficeValueType.java
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ public Object read(XmlReaderInstance reader) {
public void write(Object value, XMLStreamWriter writer) throws XMLStreamException {
if (value instanceof String) {
writer.writeAttribute(OFFICE, "value-type", this.getId());
writer.writeAttribute(OFFICE, "value", value.toString().replace("\n", "
"));
writer.writeAttribute(OFFICE, "string-value", value.toString().replace("\n", "
"));
}
}
},
Expand Down
4 changes: 2 additions & 2 deletions tests/com/github/miachm/sods/OfficeValueTypeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -166,11 +166,11 @@ public void testReadString() throws Exception {
public void testWriteString() throws Exception {
assertWrite(OfficeValueType.STRING, "hello")
.containsAttribute("office:value-type", "string")
.containsAttribute("office:value", "hello");
.containsAttribute("office:string-value", "hello");

assertWrite(OfficeValueType.STRING, "see\nyou")
.containsAttribute("office:value-type", "string")
.containsAttribute("office:value", "see
you");
.containsAttribute("office:string-value", "see
you");
}

@Test
Expand Down

0 comments on commit 483231d

Please sign in to comment.