Commit 47d09ad6 authored by Matija Obreza's avatar Matija Obreza
Browse files

Working version

parent 955f6d33
......@@ -2,3 +2,4 @@
.classpath
.project
result.xlsx
data
......@@ -107,6 +107,11 @@
<artifactId>jackson-databind</artifactId>
<version>2.4.5</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.5</version>
</dependency>
</dependencies>
<profiles>
......
......@@ -86,7 +86,9 @@ public class ExcelTemplatingServiceImpl implements ExcelTemplatingService {
AreaReference areaReference = new AreaReference(name.getRefersToFormula(), workbook.getSpreadsheetVersion());
CellReference[] crefs = areaReference.getAllReferencedCells();
LOG.info("Named range references cells={} size={}", name.getRefersToFormula(), crefs.length);
if (LOG.isDebugEnabled()) {
LOG.debug("Named range references cells={} size={}", name.getRefersToFormula(), crefs.length);
}
if (crefs.length == 1) {
LOG.debug("Single field needs populating");
......@@ -131,7 +133,8 @@ public class ExcelTemplatingServiceImpl implements ExcelTemplatingService {
if (name != null) {
String subPath = "$." + name.getNameName().substring(ExcelTemplatingService.NAMED_NAME_PREFIX.length());
LOG.info("Found name for cell name={} subPath={}", name.getNameName(), subPath);
if (LOG.isTraceEnabled())
LOG.trace("Found name for cell name={} subPath={}", name.getNameName(), subPath);
subPath = subPath.substring(jsonPath.length());
subs.add(subPath);
} else {
......@@ -164,10 +167,12 @@ public class ExcelTemplatingServiceImpl implements ExcelTemplatingService {
CellStyle style = cell.getCellStyle();
LOG.trace("Cell fmt={} type={}", style.getDataFormatString(), cell.getCellType());
if (jsonValue instanceof String) {
if (jsonValue == null) {
// Ignore
} else if (jsonValue instanceof String) {
cell.setCellValue((String) jsonValue);
} else if (jsonValue instanceof Number) {
if (looksLikeADate(style.getDataFormatString())) {
if (SpreadsheetUtil.looksLikeADate(style.getDataFormatString())) {
cell.setCellValue(new Date(((Number) jsonValue).longValue()));
} else {
cell.setCellValue(((Number) jsonValue).doubleValue());
......@@ -183,14 +188,4 @@ public class ExcelTemplatingServiceImpl implements ExcelTemplatingService {
LOG.debug("Value not defined path={} json={}", jsonPath, json.jsonString());
}
}
private boolean looksLikeADate(String fmt) {
if (StringUtils.isBlank(fmt)) {
return false;
}
if (fmt.contains("d") || fmt.contains("m") || fmt.contains("y")) {
return true;
}
return false;
}
}
......@@ -18,6 +18,7 @@ package org.croptrust.excel.templates.impl;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
......@@ -391,6 +392,9 @@ public class ExcelToMapsJSONImpl implements ExcelToJSON {
if (value instanceof Boolean) {
return BooleanNode.valueOf((Boolean) value);
}
if (value instanceof Date) {
return new LongNode(((Date) value).getTime());
}
throw new UnsupportedOperationException("Unsupported JSON type " + value.getClass());
}
}
......@@ -16,6 +16,7 @@
package org.croptrust.excel.templates.impl;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.FormulaEvaluator;
import org.apache.poi.ss.usermodel.Name;
......@@ -41,6 +42,10 @@ public class SpreadsheetUtil {
case Cell.CELL_TYPE_BOOLEAN:
return cell.getBooleanCellValue();
case Cell.CELL_TYPE_NUMERIC:
// could be date
if (looksLikeADate(cell.getCellStyle().getDataFormatString())) {
return cell.getDateCellValue();
}
return cell.getNumericCellValue();
case Cell.CELL_TYPE_STRING:
return cell.getStringCellValue();
......@@ -110,4 +115,14 @@ public class SpreadsheetUtil {
return null;
}
public static boolean looksLikeADate(String fmt) {
if (StringUtils.isBlank(fmt)) {
return false;
}
if (fmt.contains("d") || fmt.contains("m") || fmt.contains("y")) {
return true;
}
return false;
}
}
......@@ -21,7 +21,6 @@ import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.nullValue;
import static org.junit.Assert.assertThat;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URISyntaxException;
import java.text.DateFormat;
......@@ -67,8 +66,6 @@ public class TemplatingTest extends JsonPathTest {
for (int i = 0; i < wb.getNumberOfNames(); i++) {
Name name = wb.getNameAt(i);
System.err.println("Name='" + name.getNameName() + "' sheet='" + name.getSheetName() + "' deleted="
+ name.isDeleted());
}
}
}
......@@ -83,10 +80,7 @@ public class TemplatingTest extends JsonPathTest {
if (!name.getNameName().startsWith("JSON.")) {
continue;
}
System.err.println("Name='" + name.getNameName() + "' sheet='" + name.getSheetName() + "' deleted="
+ name.isDeleted());
System.err.println("\t" + name.getRefersToFormula() + " isFunction=" + name.isFunctionName());
if (name.isDeleted()) {
continue;
}
......@@ -179,7 +173,7 @@ public class TemplatingTest extends JsonPathTest {
bcode.put("year", 2016);
String defaults = root.toString();
System.err.println(defaults);
// System.err.println(defaults);
try (Workbook wb = WorkbookFactory.create(BasicExcelTest.PARTICIPANTS_XSLX)) {
Sheet sheet = wb.getSheetAt(0);
......@@ -209,13 +203,6 @@ public class TemplatingTest extends JsonPathTest {
.getStringCellValue(), is(json.read("$.budgetCodes[0].project")));
assertThat("Cell must contain the activity", SpreadsheetUtil.getCell(wb, "'TA REQUEST-1'!F23")
.getStringCellValue(), is(json.read("$.budgetCodes[0].activity")));
try (FileOutputStream fileOut = new FileOutputStream("result.xlsx")) {
wb.write(fileOut);
fileOut.flush();
}
return;
}
}
}
......@@ -246,7 +233,7 @@ public class TemplatingTest extends JsonPathTest {
travel.put("budgetUnit", "CRP1");
String defaults = root.toString();
System.err.println(defaults);
// System.err.println(defaults);
try (Workbook wb = WorkbookFactory.create(BasicExcelTest.PARTICIPANTS_XSLX)) {
Sheet sheet = wb.getSheetAt(0);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment