Skip to content

Commit

Permalink
Add row number to prefix columns - version bumped
Browse files Browse the repository at this point in the history
  • Loading branch information
dariober committed Apr 20, 2020
1 parent 2d5491e commit b3149be
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 14 deletions.
13 changes: 8 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* [Description & Usage](#description--usage)
* [Options](#options)
* [Installation](#installation)
* [Similar programs](#similar-programs)
* [Similar programs](#similar-programs)
* [Developer](#developer)
* [Cut new release](#cut-new-release)

Expand Down Expand Up @@ -48,7 +48,7 @@ programs:
* Weaker dependency (Java 1.8+) and no installation needed compared to Python,
Perl, R solutions

Unless option `--no-prefix` is set, the first three columns of the output CSV
Unless option `-p/--no-prefix` is set, the first four columns of the output CSV
are always:

* Source file name
Expand All @@ -57,7 +57,9 @@ are always:

* Name of the exported spreadsheet

So the actual data starts at column 4.
* Row number (1-based)

So the actual data starts at column 5.

Options
-------
Expand Down Expand Up @@ -114,7 +116,7 @@ cp excelToCsv /usr/local/bin/ # Or else in your PATH e.g. ~/bin/
```

Similar programs
----------------
================

There are a number of Excel-to-CSV exporters. I found this
[excel2csv](https://github.com/informationsea/excel2csv) when I already wrote
Expand Down Expand Up @@ -165,5 +167,6 @@ cd ~/git_repos/excelToCsv ## Or wherever the latest local dir is
./gradlew build
cat excelToCsv.stub build/libs/excelToCsv.jar > excelToCsv
excelToCsv -h ## Check it works ok
chmod a+x excelToCsv
./excelToCsv -v ## Check it works ok
```
4 changes: 2 additions & 2 deletions src/main/java/excelToCsv/ArgParse.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
public class ArgParse {

public static String PROG_NAME= "excelToCsv";
public static String VERSION= "0.1.1";
public static String VERSION= "0.2.0";

/* Parse command line args */
public static Namespace argParse(String[] args){
Expand Down Expand Up @@ -79,7 +79,7 @@ public static Namespace argParse(String[] args){
parser.addArgument("--no-prefix", "-p")
.action(Arguments.storeTrue())
.help("Do not prefix rows with filename, sheet index,\n"
+ "sheet name");
+ "sheet name, row number");

parser.addArgument("--version", "-v").action(Arguments.version());

Expand Down
4 changes: 3 additions & 1 deletion src/main/java/excelToCsv/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,20 +102,22 @@ private static void printSheet(Workbook wb, String excelFile, String sheetName,
prefix.add(excelFile);
prefix.add(Integer.toString(wb.getSheetIndex(sheetName) + 1));
prefix.add(sheetName);
prefix.add("0"); // Row number

Set<Integer> emptyColsIdx = new HashSet<Integer>();
if(dropEmptyColumns) {
emptyColsIdx = indexOfEmptyColumns(sheet);
}

for (int r = 0; r <= lastRowNo; r++) {

prefix.set(3, Integer.toString(r + 1));
Row row = sheet.getRow(r);
if(isEmptyRow(row) && dropEmptyRows) {
continue;
}
List<String> line = makeEmptyRow(lastColNo, na);
if ( row != null ) {
assert prefix.get(3).equals(Integer.toString((row.getRowNum()+1)));
for (int c = 0, cn = lastColNo; c < cn ; c++) {

Cell cell = row.getCell(c, Row.MissingCellPolicy.RETURN_BLANK_AS_NULL);
Expand Down
15 changes: 9 additions & 6 deletions src/test/java/excelToCsv/MainTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ public void testSize() throws InvalidFormatException, IOException {
assertEquals(14, rows.length);
for(String row : rows) {
if(row.contains("Sheet1")) {
assertEquals(3+7, row.split(",").length);
assertEquals(4+7, row.split(",").length);
}
}
}
Expand Down Expand Up @@ -218,8 +218,7 @@ public void testQuote() throws InvalidFormatException, IOException {
out = this.runMain(args);
stdout = out.get(0);
stderr = out.get(1);
System.out.println(stdout);
assertEquals("test_data/quotes.xlsx,1,Sheet1,eggs,\"Foo, \"\"bar\"\", eggs\",spam with traling space ,bob", stdout.trim());
assertEquals("test_data/quotes.xlsx,1,Sheet1,1,eggs,\"Foo, \"\"bar\"\", eggs\",spam with traling space ,bob", stdout.trim());
}

@Test
Expand Down Expand Up @@ -274,6 +273,10 @@ public void testRowPrefix() throws InvalidFormatException, IOException {
String stdout = out.get(0);
String stderr = out.get(1);
assertEquals(0, stderr.length());
assertTrue(stdout.contains("test_data/simple01.xlsx,1,Sheet1,1,"));
assertTrue(stdout.contains("test_data/simple01.xlsx,1,Sheet1,8,"));
assertTrue(stdout.contains("test_data/simple01.xlsx,2,Sheet2,1,"));
assertTrue(stdout.contains("test_data/simple01.xlsx,2,Sheet2,4,"));
assertEquals(StringUtils.countMatches(stdout, "test_data/simple01.xlsx,1,Sheet1,"), 10);
assertEquals(StringUtils.countMatches(stdout, "test_data/simple01.xlsx,2,Sheet2,"), 4);

Expand Down Expand Up @@ -325,7 +328,7 @@ public void testCanSkipEmptyRows() throws InvalidFormatException, IOException {
String stderr = out.get(1);
assertEquals(0, stderr.length());
assertTrue( ! stdout.contains("test_data/empty_cols.xlsx,1,Sheet1,,,,,"));
assertTrue(stdout.contains("test_data/empty_cols.xlsx,1,Sheet1,,2.66666156237642,,,"));
assertTrue(stdout.contains("test_data/empty_cols.xlsx,1,Sheet1,4,,2.66666156237642,,,"));
}

@Test
Expand All @@ -335,8 +338,8 @@ public void testCanSkipEmptyColumns() throws InvalidFormatException, IOException
String stdout = out.get(0);
String stderr = out.get(1);
assertEquals(0, stderr.length());
assertTrue(stdout.contains("test_data/empty_cols.xlsx|1|Sheet1|||"));
assertTrue(stdout.contains("test_data/empty_cols.xlsx|1|Sheet1|a|e|h"));
assertTrue(stdout.contains("test_data/empty_cols.xlsx|1|Sheet1|1|||"));
assertTrue(stdout.contains("test_data/empty_cols.xlsx|1|Sheet1|5|a|e|h"));
}

/** Execute main with the given array of arguments and return a list of length 2 containing 1) stdout and 2) stderr.
Expand Down

0 comments on commit b3149be

Please sign in to comment.