use of org.odftoolkit.odfdom.doc.table.OdfTable in project OpenRefine by OpenRefine.
the class OdsImporter method parseOneFile.
@Override
public void parseOneFile(Project project, ProjectMetadata metadata, ImportingJob job, String fileSource, InputStream inputStream, int limit, JSONObject options, List<Exception> exceptions) {
OdfDocument odfDoc;
try {
odfDoc = OdfDocument.loadDocument(inputStream);
} catch (Exception e) {
// Ugh! could they throw any wider exception?
exceptions.add(e);
return;
}
List<OdfTable> tables = odfDoc.getTableList();
int[] sheets = JSONUtilities.getIntArray(options, "sheets");
for (int sheetIndex : sheets) {
final OdfTable table = tables.get(sheetIndex);
final int lastRow = table.getRowCount();
TableDataReader dataReader = new TableDataReader() {
int nextRow = 0;
Map<String, Recon> reconMap = new HashMap<String, Recon>();
@Override
public List<Object> getNextRowOfCells() throws IOException {
if (nextRow > lastRow) {
return null;
}
List<Object> cells = new ArrayList<Object>();
OdfTableRow row = table.getRowByIndex(nextRow++);
if (row != null) {
int lastCell = row.getCellCount();
for (int cellIndex = 0; cellIndex <= lastCell; cellIndex++) {
Cell cell = null;
OdfTableCell sourceCell = row.getCellByIndex(cellIndex);
if (sourceCell != null) {
cell = extractCell(sourceCell, reconMap);
}
cells.add(cell);
}
}
return cells;
}
};
TabularImportingParserBase.readTable(project, metadata, job, dataReader, fileSource + "#" + table.getTableName(), limit, options, exceptions);
}
}
use of org.odftoolkit.odfdom.doc.table.OdfTable in project OpenRefine by OpenRefine.
the class OdsImporter method parseOneFile.
@Override
public void parseOneFile(Project project, ProjectMetadata metadata, ImportingJob job, String fileSource, InputStream inputStream, int limit, ObjectNode options, List<Exception> exceptions) {
OdfDocument odfDoc;
try {
odfDoc = OdfDocument.loadDocument(inputStream);
} catch (Exception e) {
// Ugh! could they throw any wider exception?
exceptions.add(e);
return;
}
List<OdfTable> tables = odfDoc.getTableList();
ArrayNode sheets = JSONUtilities.getArray(options, "sheets");
for (int i = 0; i < sheets.size(); i++) {
String[] fileNameAndSheetIndex = new String[2];
ObjectNode sheetObj = JSONUtilities.getObjectElement(sheets, i);
// value is fileName#sheetIndex
fileNameAndSheetIndex = sheetObj.get("fileNameAndSheetIndex").asText().split("#");
if (!fileNameAndSheetIndex[0].equals(fileSource))
continue;
final OdfTable table = tables.get(Integer.parseInt(fileNameAndSheetIndex[1]));
final int lastRow = table.getRowCount();
TableDataReader dataReader = new TableDataReader() {
int nextRow = 0;
Map<String, Recon> reconMap = new HashMap<String, Recon>();
@Override
public List<Object> getNextRowOfCells() throws IOException {
if (nextRow > lastRow) {
return null;
}
List<Object> cells = new ArrayList<Object>();
OdfTableRow row = table.getRowByIndex(nextRow++);
int maxCol = 0;
if (row != null) {
int lastCell = row.getCellCount();
for (int cellIndex = 0; cellIndex <= lastCell; cellIndex++) {
Cell cell = null;
OdfTableCell sourceCell = row.getCellByIndex(cellIndex);
if (sourceCell != null) {
cell = extractCell(sourceCell, reconMap);
}
cells.add(cell);
if (cell != null && cellIndex > maxCol) {
maxCol = cellIndex;
}
}
}
// Right truncate null cells
return cells.subList(0, maxCol + 1);
}
};
TabularImportingParserBase.readTable(project, metadata, job, dataReader, fileSource + "#" + table.getTableName(), limit, options, exceptions);
}
}
use of org.odftoolkit.odfdom.doc.table.OdfTable in project structr by structr.
the class ODSExporter method writeCollectionToCells.
static void writeCollectionToCells(final OdfTable sheet, final OdfTableCell startCell, final Collection col) {
int rowIndex, colIndex;
colIndex = startCell.getColumnIndex();
rowIndex = startCell.getRowIndex();
Iterator<Collection> colIt = col.iterator();
while (colIt.hasNext()) {
Object obj = colIt.next();
if (obj instanceof String[]) {
String[] arr = (String[]) obj;
List<String> list = new ArrayList<>(Arrays.asList(arr));
StringJoiner sj = new StringJoiner(",");
list.forEach(s -> sj.add(s));
writeObjectToCell(sheet.getCellByPosition(colIndex, rowIndex), sj.toString());
} else if (obj instanceof Collection) {
Collection nestedCol = (Collection) obj;
StringJoiner sj = new StringJoiner(",");
nestedCol.forEach(s -> sj.add(s.toString()));
writeObjectToCell(sheet.getCellByPosition(colIndex, rowIndex), sj.toString());
} else {
writeObjectToCell(sheet.getCellByPosition(colIndex, rowIndex), obj);
}
rowIndex++;
}
}
Aggregations