Skip to content

Commit

Permalink
Add a --diff_ignore_formulas flag
Browse files Browse the repository at this point in the history
  • Loading branch information
na-ka-na committed May 8, 2016
1 parent 81f6fd0 commit b01a275
Show file tree
Hide file tree
Showing 13 changed files with 147 additions and 13 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ This software is distributed under the [MIT](http://www.opensource.org/licenses/
* Works with xls, xlsx, xlsm, ods. You may compare any of these with each other.
* Compares only cell "contents". Formatting, macros are currently ignored.
* Using --ignore1 & --ignore2 (both optional) you may tell the diff to skip any number of sheets / rows / columns / cells.
* Use --diff_numeric_precision=0.01 to control diffs of numbers.
* --diff_numeric_precision: by default numbers are diffed with double precision, to change that specify this flag as --diff_numeric_precision=0.0001
* --diff_ignore_formulas: by default for cells with formula, formula is compared instead of the evaluated value. Use this flag to compare evaluated value instead

Report bugs / issues / requests [here](https://github.com/na-ka-na/ExcelCompare/issues)

Expand Down
29 changes: 23 additions & 6 deletions src/com/ka/spreadsheet/diff/Flags.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,25 @@ public class Flags {
private static final String DEBUG_FLAG = "--debug";
// double value, default null
private static final String DIFF_NUMERIC_PRECISION_FLAG = "--diff_numeric_precision";
// no value, default absent
private static final String DIFF_IGNORE_FORMULAS_FLAG = "--diff_ignore_formulas";

public static boolean DEBUG = false;
public static Double DIFF_NUMERIC_PRECISION = null;
public static File WORKBOOK1 = null;
public static File WORKBOOK2 = null;
public static WorkbookIgnores WORKBOOK_IGNORES1 = null;
public static WorkbookIgnores WORKBOOK_IGNORES2 = null;
public static boolean DEBUG;
public static Double DIFF_NUMERIC_PRECISION;
public static boolean DIFF_IGNORE_FORMULAS;
public static File WORKBOOK1;
public static File WORKBOOK2;
public static WorkbookIgnores WORKBOOK_IGNORES1;
public static WorkbookIgnores WORKBOOK_IGNORES2;

public static boolean parseFlags(String[] args) {
DEBUG = false;
DIFF_NUMERIC_PRECISION = null;
DIFF_IGNORE_FORMULAS = false;
WORKBOOK1 = null;
WORKBOOK2 = null;
WORKBOOK_IGNORES1 = null;
WORKBOOK_IGNORES2 = null;
int idx = findFlag(DEBUG_FLAG, args);
if (idx != -1) {
DEBUG = true;
Expand All @@ -26,6 +36,11 @@ public static boolean parseFlags(String[] args) {
DIFF_NUMERIC_PRECISION = parseDoubleFlagValue(idx, args);
args = removeFlag(idx, args);
}
idx = findFlag(DIFF_IGNORE_FORMULAS_FLAG, args);
if (idx != -1) {
DIFF_IGNORE_FORMULAS = true;
args = removeFlag(idx, args);
}
if (args.length < 2) {
System.out.println(usage());
return false;
Expand Down Expand Up @@ -81,6 +96,8 @@ private static String usage() {
+ "\n"
+ " * --diff_numeric_precision: by default numbers are diffed with double precision, to change that specify this flag as --diff_numeric_precision=0.0001"
+ "\n"
+ " * --diff_ignore_formulas: by default for cells with formula, formula is compared instead of the evaluated value. Use this flag to compare evaluated value instead"
+ "\n"
+ "\n"
+ "Sheet Ignore Spec: <sheet-name>:<row-ignore-spec>:<column-ignore-spec>:<cell-ignore-spec>"
+ "\n"
Expand Down
9 changes: 7 additions & 2 deletions src/com/ka/spreadsheet/diff/SpreadSheetExcel.java
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,13 @@ public int getColumnIndex() {
@Override
public Object getValue() {
int cellType = cell.getCellType();
if (cellType == Cell.CELL_TYPE_FORMULA) {
if (!Flags.DIFF_IGNORE_FORMULAS) {
return cell.getCellFormula();
} else {
cellType = cell.getCachedFormulaResultType();
}
}
switch (cellType) {
case Cell.CELL_TYPE_NUMERIC:
return cell.getNumericCellValue();
Expand All @@ -173,8 +180,6 @@ public Object getValue() {
case Cell.CELL_TYPE_BLANK:
case Cell.CELL_TYPE_STRING:
return cell.getStringCellValue();
case Cell.CELL_TYPE_FORMULA:
return cell.getCellFormula();
case Cell.CELL_TYPE_ERROR:
return String.valueOf(cell.getErrorCellValue());
}
Expand Down
2 changes: 1 addition & 1 deletion src/com/ka/spreadsheet/diff/SpreadSheetOdf.java
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ public int getColumnIndex() {
@Override
public Object getValue() {
String formula = cell.getFormula();
if (formula != null) {
if ((formula != null) && !Flags.DIFF_IGNORE_FORMULAS) {
return formula;
}
String valueType = cell.getValueType();
Expand Down
26 changes: 23 additions & 3 deletions test/com/ka/spreadsheet/diff/SpreadSheetDifferSmokeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,13 @@ public static void main(String[] args) throws Exception {
"test/resources/numeric_and_formula.ods"},
resultFile("test/resources/numeric_and_formula.xls.ods.out"),
null);
testDiff(
"Numeric and formula odf xlsx with flag",
new String[] {"--diff_ignore_formulas",
"test/resources/numeric_and_formula.ods",
"test/resources/numeric_and_formula.xlsx"},
resultFile("test/resources/numeric_and_formula_ignoreformulaflag.ods.xlsx.out"),
null);
testDiff(
"Nullable Sheet",
new String[] {"test/resources/MultiSheet.xls", "test/resources/MultiSheet.xls",
Expand Down Expand Up @@ -105,8 +112,20 @@ public static void main(String[] args) throws Exception {
resultFile("test/resources/dev_null_ss1_xlsx.out"),
null);
}

System.out.println("All tests pass");
testDiff(
"With without formula with flag",
new String[] {"--diff_ignore_formulas",
"test/resources/ss_without_formula.xlsx",
"test/resources/ss_with_formula.xlsx"},
resultFile("test/resources/ss_with_without_formula_ignoreformulaflag.out"),
null);
testDiff(
"With without formula without flag",
new String[] {"test/resources/ss_without_formula.xlsx",
"test/resources/ss_with_formula.xlsx"},
resultFile("test/resources/ss_with_without_formula.out"),
null);
System.err.println("All tests pass");
}

private static File resultFile(String resultFile) {
Expand All @@ -115,6 +134,7 @@ private static File resultFile(String resultFile) {

public static void testDiff(String testName, String[] args, @Nullable File expectedOutFile,
@Nullable File expectedErrFile) throws Exception {
System.err.print(testName + "... ");
PrintStream oldOut = System.out;
PrintStream oldErr = System.err;
File outFile = File.createTempFile("testOutput", "out", TEMP_DIR);
Expand Down Expand Up @@ -149,6 +169,6 @@ public static void testDiff(String testName, String[] args, @Nullable File expec
assertTrue(testCompleted);
verifyFileContentsSame(errFile, expectedErrFile);
verifyFileContentsSame(outFile, expectedOutFile);
System.out.println(testName + " passed");
System.err.println("passed");
}
}
16 changes: 16 additions & 0 deletions test/resources/numeric_and_formula_ignoreformulaflag.ods.xlsx.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
EXTRA Cell in WB1 Sheet1!A1 => ''
EXTRA Cell in WB1 Sheet1!B1 => ''
----------------- DIFF -------------------
Sheets: []
Rows: []
Cols: []
----------------- EXTRA WB1 -------------------
Sheets: [Sheet1]
Rows: [1]
Cols: [A, B]
----------------- EXTRA WB2 -------------------
Sheets: []
Rows: []
Cols: []
-----------------------------------------
Excel files test/resources/numeric_and_formula.ods and test/resources/numeric_and_formula.xlsx differ
Binary file added test/resources/ss_with_formula.xlsx
Binary file not shown.
16 changes: 16 additions & 0 deletions test/resources/ss_with_without_formula.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
DIFF Cell at Sheet1!A1 => '2.0' v/s '1+1'
DIFF Cell at Sheet1!B2 => 'ab' v/s 'CONCATENATE("a","b")'
----------------- DIFF -------------------
Sheets: [Sheet1]
Rows: [1, 2]
Cols: [A, B]
----------------- EXTRA WB1 -------------------
Sheets: []
Rows: []
Cols: []
----------------- EXTRA WB2 -------------------
Sheets: []
Rows: []
Cols: []
-----------------------------------------
Excel files test/resources/ss_without_formula.xlsx and test/resources/ss_with_formula.xlsx differ
14 changes: 14 additions & 0 deletions test/resources/ss_with_without_formula_ignoreformulaflag.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
----------------- DIFF -------------------
Sheets: []
Rows: []
Cols: []
----------------- EXTRA WB1 -------------------
Sheets: []
Rows: []
Cols: []
----------------- EXTRA WB2 -------------------
Sheets: []
Rows: []
Cols: []
-----------------------------------------
Excel files test/resources/ss_without_formula.xlsx and test/resources/ss_with_formula.xlsx match
Binary file added test/resources/ss_without_formula.xlsx
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
EXTRA Cell in WB1 Sheet1!A1 => ''
EXTRA Cell in WB1 Sheet1!B1 => ''
----------------- DIFF -------------------
Sheets: []
Rows: []
Cols: []
----------------- EXTRA WB1 -------------------
Sheets: [Sheet1]
Rows: [1]
Cols: [A, B]
----------------- EXTRA WB2 -------------------
Sheets: []
Rows: []
Cols: []
-----------------------------------------
Excel files test\resources\numeric_and_formula.ods and test\resources\numeric_and_formula.xlsx differ
15 changes: 15 additions & 0 deletions test/resources/win_ss_with_without_formula.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
DIFF Cell at Sheet1!A1 => '2.0' v/s '1+1'
----------------- DIFF -------------------
Sheets: [Sheet1]
Rows: [1]
Cols: [A]
----------------- EXTRA WB1 -------------------
Sheets: []
Rows: []
Cols: []
----------------- EXTRA WB2 -------------------
Sheets: []
Rows: []
Cols: []
-----------------------------------------
Excel files test\resources\ss_without_formula.xlsx and test\resources\ss_with_formula.xlsx differ
14 changes: 14 additions & 0 deletions test/resources/win_ss_with_without_formula_ignoreformulaflag.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
----------------- DIFF -------------------
Sheets: [Sheet1]
Rows: []
Cols: []
----------------- EXTRA WB1 -------------------
Sheets: []
Rows: []
Cols: []
----------------- EXTRA WB2 -------------------
Sheets: []
Rows: []
Cols: []
-----------------------------------------
Excel files test\resources\ss_without_formula.xlsx and test\resources\ss_with_formula.xlsx match

0 comments on commit b01a275

Please sign in to comment.