Skip to content

Commit

Permalink
xlsx formula fix for field import files
Browse files Browse the repository at this point in the history
  • Loading branch information
chaneylc committed Jul 5, 2023
1 parent be16779 commit 5e56c6d
Showing 1 changed file with 37 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package com.fieldbook.tracker.objects;

import static org.apache.poi.ss.usermodel.Cell.CELL_TYPE_BOOLEAN;
import static org.apache.poi.ss.usermodel.Cell.CELL_TYPE_NUMERIC;
import static org.apache.poi.ss.usermodel.Cell.CELL_TYPE_STRING;

import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
Expand All @@ -11,8 +15,10 @@

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DataFormatter;
import org.apache.poi.ss.usermodel.FormulaEvaluator;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFFormulaEvaluator;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
Expand Down Expand Up @@ -443,6 +449,8 @@ public String[] readNext() {
ArrayList<XSSFRow> rows = new ArrayList<>();
XSSFSheet sheet = wb.getSheetAt(0);

XSSFFormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator();

for (Iterator<Row> it = sheet.rowIterator(); it.hasNext();) {
rows.add((XSSFRow) it.next());
}
Expand All @@ -454,7 +462,22 @@ public String[] readNext() {
ArrayList<String> data = new ArrayList<>();
for (Iterator<Cell> it = rows.get(currentRow).cellIterator(); it.hasNext();) {
XSSFCell cell = (XSSFCell) it.next();
data.add(fmt.formatCellValue(cell));
if (cell.getCellType() == Cell.CELL_TYPE_FORMULA) {//formula
int type = evaluator.evaluateFormulaCell(cell);
switch (type) {
case CELL_TYPE_BOOLEAN:
data.add(String.valueOf(cell.getBooleanCellValue()));
break;
case CELL_TYPE_NUMERIC:
data.add(String.valueOf(cell.getNumericCellValue()));
break;
default:
data.add(cell.getStringCellValue());
break;
}
} else {
data.add(fmt.formatCellValue(cell));
}
}

currentRow += 1;
Expand All @@ -472,13 +495,26 @@ public void close() {
* @return attempt to parse the string value of the cell
*/
private static String getCellStringValue(XSSFCell cell) {

FormulaEvaluator evaluator = cell.getSheet().getWorkbook().getCreationHelper().createFormulaEvaluator();

switch (cell.getCellType()) {
case 0: { //numeric
return String.valueOf(cell.getNumericCellValue());
}
case 1: { //text
return cell.getStringCellValue();
}
case Cell.CELL_TYPE_FORMULA: { //formula
switch (evaluator.evaluateFormulaCell(cell)) {
case CELL_TYPE_BOOLEAN:
return String.valueOf(cell.getBooleanCellValue());
case CELL_TYPE_NUMERIC:
return String.valueOf(cell.getNumericCellValue());
case CELL_TYPE_STRING:
return cell.getStringCellValue();
}
}
case 3: { //boolean
return String.valueOf(cell.getBooleanCellValue());
}
Expand Down

0 comments on commit 5e56c6d

Please sign in to comment.