diff --git a/col.go b/col.go index 13d5ad7..52b412f 100644 --- a/col.go +++ b/col.go @@ -54,7 +54,15 @@ func (xf *XfRk) String(wb *WorkBook) string { fNo := wb.Xfs[idx].formatNo() if fNo >= 164 { // user defined format if formatter := wb.Formats[fNo]; formatter != nil { - if strings.Contains(formatter.str, "#") || strings.Contains(formatter.str, ".00") { + formatterLower := strings.ToLower(formatter.str) + if formatterLower == "general" || + strings.Contains(formatter.str, "#") || + strings.Contains(formatter.str, ".00") || + strings.Contains(formatterLower, "mm/yy") || + strings.Contains(formatterLower, "dd/yy") || + strings.Contains(formatterLower, "mm.yy") || + strings.Contains(formatterLower, "dd.yy") || + strings.Contains(formatterLower, "дд.гг") { //If format contains # or .00 then this is a number return xf.Rk.String() } else { diff --git a/comparexlsxlsx.go b/comparexlsxlsx.go index 080f50a..eab82b2 100644 --- a/comparexlsxlsx.go +++ b/comparexlsxlsx.go @@ -3,17 +3,18 @@ package xls import ( "fmt" "github.com/tealeg/xlsx" - "path" + "math" + "strconv" ) //Compares xls and xlsx files -func compareXlsXlsx(filepathname string) string { - xlsFile, err := Open(path.Join("testdata", filepathname)+".xls", "utf-8") +func compareXlsXlsx(xlsfilepathname string, xlsxfilepathname string) string { + xlsFile, err := Open(xlsfilepathname, "utf-8") if err != nil { return fmt.Sprintf("Cant open xls file: %s", err) } - xlsxFile, err := xlsx.OpenFile(path.Join("testdata", filepathname) + ".xlsx") + xlsxFile, err := xlsx.OpenFile(xlsxfilepathname) if err != nil { return fmt.Sprintf("Cant open xlsx file: %s", err) } @@ -29,8 +30,22 @@ func compareXlsXlsx(filepathname string) string { xlsText := xlsRow.Col(cell) xlsxText := xlsxCell.String() if xlsText != xlsxText { - return fmt.Sprintf("Sheet: %d, row: %d, col: %d, xlsx: (%s)[%d], xls: (%s)[%d].", - sheet, row, cell, xlsxText, len(xlsxText), xlsText, len(xlsText)) + //try to convert to numbers + xlsFloat, xlsErr := strconv.ParseFloat(xlsText, 64) + xlsxFloat, xlsxErr := strconv.ParseFloat(xlsxText, 64) + //check if numbers have no significant difference + if xlsErr == nil && xlsxErr == nil { + diff := math.Abs(xlsFloat - xlsxFloat) + if diff > 0.0000001 { + return fmt.Sprintf("sheet:%d, row/col: %d/%d, xlsx: (%s)[%d], xls: (%s)[%d], numbers difference: %f.", + sheet, row, cell, xlsxText, len(xlsxText), + xlsText, len(xlsText), diff) + } + } else { + return fmt.Sprintf("sheet:%d, row/col: %d/%d, xlsx: (%s)[%d], xls: (%s)[%d].", + sheet, row, cell, xlsxText, len(xlsxText), + xlsText, len(xlsText)) + } } } } diff --git a/issue47_test.go b/issue47_test.go index ab5a713..098604d 100644 --- a/issue47_test.go +++ b/issue47_test.go @@ -1,14 +1,30 @@ package xls import ( + "io/ioutil" + "path" + "path/filepath" + "strings" "testing" ) func TestIssue47(t *testing.T) { - e := compareXlsXlsx("issue47") + testdatapath := "testdata" + files, err := ioutil.ReadDir(testdatapath) + if err != nil { + t.Fatalf("Cant read testdata directory contents: %s", err) + } + for _, f := range files { + if filepath.Ext(f.Name()) == ".xls" { + xlsfilename := f.Name() + xlsxfilename := strings.TrimSuffix(xlsfilename, filepath.Ext(xlsfilename)) + ".xlsx" + err := compareXlsXlsx(path.Join(testdatapath, xlsfilename), + path.Join(testdatapath, xlsxfilename)) + if err != "" { + t.Fatalf("XLS file %s an XLSX file are not equal: %s", xlsfilename, err) + } - if e != "" { - t.Fatalf("XLS an XLSX are not equal: %s", e) + } } } diff --git a/testdata/negatives.xls b/testdata/negatives.xls new file mode 100644 index 0000000..47902e3 Binary files /dev/null and b/testdata/negatives.xls differ diff --git a/testdata/negatives.xlsx b/testdata/negatives.xlsx new file mode 100644 index 0000000..0d705cc Binary files /dev/null and b/testdata/negatives.xlsx differ diff --git a/testdata/superstore.xls b/testdata/superstore.xls new file mode 100644 index 0000000..34c7090 Binary files /dev/null and b/testdata/superstore.xls differ diff --git a/testdata/superstore.xlsx b/testdata/superstore.xlsx new file mode 100644 index 0000000..b610f26 Binary files /dev/null and b/testdata/superstore.xlsx differ