Skip to content

Commit

Permalink
Fix empty values in Excel #82
Browse files Browse the repository at this point in the history
  • Loading branch information
miachm committed Dec 5, 2023
1 parent dcc40e0 commit 854f608
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 5 deletions.
8 changes: 8 additions & 0 deletions resources/features/bugs/crashExcel.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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"
4 changes: 2 additions & 2 deletions src/com/github/miachm/sods/OdsWritter.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
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, "string-value", value.toString().replace("\n", "
"));
writer.writeAttribute(OFFICE, "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: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
Expand Down
3 changes: 3 additions & 0 deletions tests/steps/World.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import java.io.InputStream;
import java.util.List;
import javax.xml.stream.XMLStreamReader;

public class World {
public static SpreadSheet spread;
Expand All @@ -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() {
Expand All @@ -29,6 +31,7 @@ public static void reset() {
conditionalFormat = null;
otherConditionalFormat = null;
style = null;
tag = null;
}

}
38 changes: 38 additions & 0 deletions tests/steps/ZipCucumber.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}

0 comments on commit 854f608

Please sign in to comment.