Skip to content

Commit

Permalink
[csv]
Browse files Browse the repository at this point in the history
- [`fromExcel(excel,worksheet,csvFile)`]: fix to now consider surround text with comma with double quotes, thus making such data "CSV safe".
- [`fromExcel(excel,worksheet,csvFile)`]: fix to consider `nexial.textDelim` as CSV cell delimiter.

Signed-off-by: automike <mikeliucc@users.noreply.github.com>
  • Loading branch information
mikeliucc committed Jun 3, 2019
1 parent 5b599e4 commit d607a0c
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 13 deletions.
Expand Up @@ -378,7 +378,6 @@ protected <T extends JdbcResult> T resultToCSV(ResultSet rs, T result, File file

result.setRowCount(rowCount);
outputStream.flush();
outputStream.close();
} catch (Throwable e) {
result.setError(e.getMessage());
}
Expand Down
31 changes: 21 additions & 10 deletions src/main/java/org/nexial/core/plugins/io/ExcelHelper.java
Expand Up @@ -45,6 +45,7 @@
import org.nexial.core.excel.ExcelAddress;
import org.nexial.core.model.ExecutionContext;
import org.nexial.core.model.StepResult;
import org.nexial.core.plugins.db.DaoUtils;
import org.nexial.core.utils.ConsoleUtils;

import static java.lang.System.lineSeparator;
Expand Down Expand Up @@ -87,6 +88,7 @@ protected StringBuilder xlsx2csv(File excelFile, String worksheet) throws IOExce
XSSFSheet excelSheet = workBook.getSheet(worksheet);
Iterator rowIterator = excelSheet.rowIterator();
StringBuilder csv = new StringBuilder();
String delim = context.getTextDelim();

while (rowIterator.hasNext()) {
XSSFRow row = (XSSFRow) rowIterator.next();
Expand All @@ -96,10 +98,10 @@ protected StringBuilder xlsx2csv(File excelFile, String worksheet) throws IOExce
for (int i = 0; i < row.getLastCellNum(); i++) {
value = returnCellValue(row.getCell(i));
if (StringUtils.isEmpty(value)) { value = ""; }
oneRow += value + ",";
oneRow += value + delim;
}

oneRow = StringUtils.trim(StringUtils.removeEnd(oneRow, ","));
oneRow = StringUtils.trim(StringUtils.removeEnd(oneRow, delim));
if (!oneRow.isEmpty()) { csv.append(oneRow).append(lineSeparator()); }
}

Expand All @@ -111,6 +113,7 @@ protected StringBuilder xls2csv(File excelFile, String worksheet) throws IOExcep
HSSFSheet excelSheet = workBook.getSheet(worksheet);
Iterator rowIterator = excelSheet.rowIterator();
StringBuilder csv = new StringBuilder();
String delim = context.getTextDelim();

while (rowIterator.hasNext()) {
HSSFRow row = (HSSFRow) rowIterator.next();
Expand All @@ -121,10 +124,10 @@ protected StringBuilder xls2csv(File excelFile, String worksheet) throws IOExcep
HSSFCell cell = row.getCell(i);
value = returnCellValue(cell);
if (StringUtils.isEmpty(value)) { value = ""; }
oneRow += value + ",";
oneRow += value + delim;
}

oneRow = StringUtils.trim(StringUtils.removeEnd(oneRow, ","));
oneRow = StringUtils.trim(StringUtils.removeEnd(oneRow, delim));
if (!oneRow.isEmpty()) { csv.append(oneRow).append(lineSeparator()); }
}

Expand Down Expand Up @@ -157,24 +160,32 @@ protected StepResult saveCSVContentToFile(String file, StringBuilder csv) {

protected String returnCellValue(Cell cell) {
try {
String value;

switch (cell.getCellTypeEnum()) {
case STRING:
case BOOLEAN:
return cell.getRichStringCellValue().toString();
value = cell.getRichStringCellValue().toString();
break;
case NUMERIC:
return formattedCellToString(cell);
value = formattedCellToString(cell);
break;
case FORMULA:
CellType resultType = cell.getCachedFormulaResultTypeEnum();
if (resultType == STRING) {
return cell.getRichStringCellValue().toString();
value = cell.getRichStringCellValue().toString();
} else if (resultType == NUMERIC) {
return formattedCellToString(cell);
value = formattedCellToString(cell);
} else {
return cell.getStringCellValue();
value = cell.getStringCellValue();
}

break;
default:
return cell.getStringCellValue();
value = cell.getStringCellValue();
}

return DaoUtils.csvFriendly(value, context.getTextDelim(), true);
} catch (Exception e) {
return null;
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/kotlin/org/nexial/core/plugins/db/RdbmsCommand.kt
Expand Up @@ -59,7 +59,7 @@ class RdbmsCommand : BaseCommand() {
// requires(dataAccess.validSQL(query), "invalid sql", sql);

val result = dataAccess.execute(query, resolveDao(db)) ?: return StepResult.fail(
"FAILED TO EXECUTE SQL '$sql': no result found")
"FAILED TO EXECUTE SQL '$sql': no result found")
context.setData(`var`, result)

log("executed query in ${result.elapsedTime} ms with " +
Expand Down Expand Up @@ -254,7 +254,7 @@ class RdbmsCommand : BaseCommand() {
// requires(dataAccess.validSQL(query), "invalid sql", sql);

val result = dataAccess.execute(query, dao, File(outputFile)) ?: return StepResult.fail(
"FAILED TO EXECUTE SQL '$sql': no result found")
"FAILED TO EXECUTE SQL '$sql': no result found")

log("executed query in ${result.elapsedTime} ms with " +
if (result.hasError()) "ERROR ${result.error}" else "${result.rowCount} row(s)")
Expand Down

0 comments on commit d607a0c

Please sign in to comment.