Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support DataFormatter #238

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 13 additions & 8 deletions src/main/java/com/monitorjbl/xlsx/impl/StreamingSheet.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

public class StreamingSheet implements Sheet {

private Workbook workbook;
private final String name;
private final StreamingSheetReader reader;

Expand All @@ -36,6 +37,10 @@ public StreamingSheet(String name, StreamingSheetReader reader) {
reader.setSheet(this);
}

void setWorkbook(Workbook workbook) {
this.workbook = workbook;
}

StreamingSheetReader getReader() {
return reader;
}
Expand Down Expand Up @@ -77,6 +82,14 @@ public boolean isColumnHidden(int columnIndex) {
return reader.isColumnHidden(columnIndex);
}

@Override
public Workbook getWorkbook() {
if (workbook == null) {
throw new UnsupportedOperationException();
}
return workbook;
}

/* Unsupported */

/**
Expand Down Expand Up @@ -849,14 +862,6 @@ public Drawing createDrawingPatriarch() {
throw new UnsupportedOperationException();
}

/**
* Not supported
*/
@Override
public Workbook getWorkbook() {
throw new UnsupportedOperationException();
}

/**
* Not supported
*/
Expand Down
22 changes: 18 additions & 4 deletions src/main/java/com/monitorjbl/xlsx/impl/StreamingWorkbook.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.CreationHelper;
import org.apache.poi.ss.usermodel.DataFormat;
import org.apache.poi.ss.usermodel.Date1904Support;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.Name;
import org.apache.poi.ss.usermodel.PictureData;
Expand All @@ -19,8 +20,9 @@
import java.io.OutputStream;
import java.util.Iterator;
import java.util.List;
import java.util.stream.StreamSupport;

public class StreamingWorkbook implements Workbook, AutoCloseable {
public class StreamingWorkbook implements Workbook, Date1904Support, AutoCloseable {
private final StreamingWorkbookReader reader;

public StreamingWorkbook(StreamingWorkbookReader reader) {
Expand All @@ -43,7 +45,7 @@ int findSheetByName(String name) {
*/
@Override
public Iterator<Sheet> iterator() {
return reader.iterator();
return StreamSupport.stream(reader.spliterator(), false).map(this::withWorkbook).iterator();
}

/**
Expand Down Expand Up @@ -95,7 +97,7 @@ public int getNumberOfSheets() {
*/
@Override
public Sheet getSheetAt(int index) {
return reader.getSheets().get(index);
return withWorkbook(reader.getSheets().get(index));
}

/**
Expand All @@ -107,7 +109,7 @@ public Sheet getSheet(String name) {
if(index == -1) {
throw new MissingSheetException("Sheet '" + name + "' does not exist");
}
return reader.getSheets().get(index);
return withWorkbook(reader.getSheets().get(index));
}

/**
Expand All @@ -126,6 +128,11 @@ public boolean isSheetVeryHidden(int sheetIx) {
return "veryHidden".equals(reader.getSheetProperties().get(sheetIx).get("state"));
}

@Override
public boolean isDate1904() {
return reader.isDate1904();
}

/**
* {@inheritDoc}
*/
Expand All @@ -134,6 +141,13 @@ public void close() throws IOException {
reader.close();
}

private Sheet withWorkbook(Sheet sheet) {
if (sheet instanceof StreamingSheet) {
((StreamingSheet)sheet).setWorkbook(this);
}
return sheet;
}

/* Not supported */

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,10 @@ List<? extends Sheet> getSheets() {
return sheets;
}

boolean isDate1904() {
return use1904Dates;
}

public List<Map<String, String>> getSheetProperties() {
return sheetProperties;
}
Expand Down
17 changes: 17 additions & 0 deletions src/test/java/com/monitorjbl/xlsx/StreamingWorkbookTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.util.function.Consumer;

import static com.monitorjbl.xlsx.TestUtils.expectCachedType;
import static com.monitorjbl.xlsx.TestUtils.expectFormattedContent;
import static com.monitorjbl.xlsx.TestUtils.expectFormula;
import static com.monitorjbl.xlsx.TestUtils.expectSameStringContent;
import static com.monitorjbl.xlsx.TestUtils.expectStringContent;
Expand Down Expand Up @@ -190,6 +191,22 @@ public void testEntityExpansion() {
}));
}

@Test
public void testDataFormatter() throws IOException {
try(Workbook workbook = openWorkbook("formats.xlsx")) {
Sheet sheet = workbook.getSheetAt(0);
Iterator<Row> rowIterator = sheet.rowIterator();

Cell A1 = getCellFromNextRow(rowIterator, 0);
Cell A2 = getCellFromNextRow(rowIterator, 0);
Cell A3 = getCellFromNextRow(rowIterator, 0);

expectFormattedContent(A1, "1234.6");
expectFormattedContent(A2, "1918-11-11");
expectFormattedContent(A3, "50%");
}
}

private static class ExploitServer extends NanoHTTPD implements AutoCloseable {
private final Consumer<IHTTPSession> onRequest;

Expand Down
5 changes: 5 additions & 0 deletions src/test/java/com/monitorjbl/xlsx/TestUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.DataFormatter;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.CellReference;
Expand Down Expand Up @@ -30,6 +31,10 @@ static void expectStringContent(Cell cell, String value) {
assertEquals(value, cell.getStringCellValue(), "Cell " + ref(cell) + " has wrong string content.");
}

static void expectFormattedContent(Cell cell, String value) {
assertEquals(value, new DataFormatter().formatCellValue(cell), "Cell " + ref(cell) + " has wrong formatted content.");
}

static void expectCachedType(Cell cell, CellType cellType) {
assertEquals(cellType, cell.getCachedFormulaResultType(), "Cell " + ref(cell) + " has wrong cached type." + cellType);
}
Expand Down
Binary file added src/test/resources/formats.xlsx
Binary file not shown.