From 854f608b498c31e58e62a675a35e7f5d84d43c22 Mon Sep 17 00:00:00 2001 From: Miguel Chacon Date: Tue, 5 Dec 2023 16:59:15 +0000 Subject: [PATCH] Fix empty values in Excel #82 --- resources/features/bugs/crashExcel.feature | 8 ++++ src/com/github/miachm/sods/OdsWritter.java | 4 +- .../github/miachm/sods/OfficeValueType.java | 2 +- .../miachm/sods/OfficeValueTypeTest.java | 4 +- tests/steps/World.java | 3 ++ tests/steps/ZipCucumber.java | 38 +++++++++++++++++++ 6 files changed, 54 insertions(+), 5 deletions(-) diff --git a/resources/features/bugs/crashExcel.feature b/resources/features/bugs/crashExcel.feature index 90e0e01..80133af 100644 --- a/resources/features/bugs/crashExcel.feature +++ b/resources/features/bugs/crashExcel.feature @@ -8,3 +8,11 @@ Feature: Bugs related to loading a file generated by SODS in Excel And gets the "styles.xml" entry in the spreadsheet saved in the memory And gets the initial tag of the xml entry Then the tag is "office:document-styles" + + Scenario: Excel does not read values + When load a spreadsheet from the resource "emptyCell" + 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" diff --git a/src/com/github/miachm/sods/OdsWritter.java b/src/com/github/miachm/sods/OdsWritter.java index f2ea4b9..296e80a 100644 --- a/src/com/github/miachm/sods/OdsWritter.java +++ b/src/com/github/miachm/sods/OdsWritter.java @@ -282,9 +282,9 @@ 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); - } + //} out.writeStartElement(TEXT, "p"); String text = v.toString(); diff --git a/src/com/github/miachm/sods/OfficeValueType.java b/src/com/github/miachm/sods/OfficeValueType.java index 5c48f65..02c743a 100644 --- a/src/com/github/miachm/sods/OfficeValueType.java +++ b/src/com/github/miachm/sods/OfficeValueType.java @@ -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, "string-value", value.toString().replace("\n", " ")); + writer.writeAttribute(OFFICE, "value", value.toString().replace("\n", " ")); } } }, diff --git a/tests/com/github/miachm/sods/OfficeValueTypeTest.java b/tests/com/github/miachm/sods/OfficeValueTypeTest.java index cc8f21a..ee8822c 100644 --- a/tests/com/github/miachm/sods/OfficeValueTypeTest.java +++ b/tests/com/github/miachm/sods/OfficeValueTypeTest.java @@ -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:string-value", "hello"); + .containsAttribute("office:value", "hello"); assertWrite(OfficeValueType.STRING, "see\nyou") .containsAttribute("office:value-type", "string") - .containsAttribute("office:string-value", "see you"); + .containsAttribute("office:value", "see you"); } @Test diff --git a/tests/steps/World.java b/tests/steps/World.java index 8e15e48..41c5f66 100644 --- a/tests/steps/World.java +++ b/tests/steps/World.java @@ -4,6 +4,7 @@ import java.io.InputStream; import java.util.List; +import javax.xml.stream.XMLStreamReader; public class World { public static SpreadSheet spread; @@ -16,6 +17,7 @@ public class World { public static ConditionalFormat conditionalFormat; public static Style style; public static ConditionalFormat otherConditionalFormat; + public static XMLStreamReader tag; public static void reset() { @@ -29,6 +31,7 @@ public static void reset() { conditionalFormat = null; otherConditionalFormat = null; style = null; + tag = null; } } diff --git a/tests/steps/ZipCucumber.java b/tests/steps/ZipCucumber.java index 6a72e98..3e2ef58 100644 --- a/tests/steps/ZipCucumber.java +++ b/tests/steps/ZipCucumber.java @@ -60,4 +60,42 @@ public void the_file_is_present_in_the_manifest_file(String key) throws Throwabl assertTrue(found); } + + @When("^gets the first tag \"([^\"]*)\" of the xml entry$") + public void gets_the_first_tag_of_the_xml_entry(String tag) throws Throwable { + XMLInputFactory inputFactory = XMLInputFactory.newInstance(); + XMLStreamReader reader = inputFactory.createXMLStreamReader(World.in); + while (reader.hasNext()) + { + reader.next(); + if (reader.isStartElement()) { + String name = reader.getName().getPrefix() + ":" + reader.getName().getLocalPart(); + if (name.equals(tag)) { + World.tag = reader; + return; + } + } + } + + fail("Tag not found"); + } + + @Then("^the tag has the attribute \"([^\"]*)\" with the value \"([^\"]*)\"$") + public void the_tag_has_the_attribute_with_the_value(String key, String result) throws Throwable { + for (int i = 0; i < World.tag.getAttributeCount(); i++) { + String name = qNameToString(World.tag.getAttributeName(i)); + String value = World.tag.getAttributeValue(i); + if (name.equals(key)) { + assertEquals(value, result); + return; + } + } + + fail("Attribute not found"); + } + + private String qNameToString(QName qName) + { + return qName.getPrefix() + ":" + qName.getLocalPart(); + } }