20
0

enhanced reading dates

This commit is contained in:
sergeilem 2019-03-05 23:25:31 +03:00
parent ffbdba585f
commit 47f24af084
7 changed files with 49 additions and 10 deletions

10
col.go
View File

@ -54,7 +54,15 @@ func (xf *XfRk) String(wb *WorkBook) string {
fNo := wb.Xfs[idx].formatNo() fNo := wb.Xfs[idx].formatNo()
if fNo >= 164 { // user defined format if fNo >= 164 { // user defined format
if formatter := wb.Formats[fNo]; formatter != nil { 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 //If format contains # or .00 then this is a number
return xf.Rk.String() return xf.Rk.String()
} else { } else {

View File

@ -3,17 +3,18 @@ package xls
import ( import (
"fmt" "fmt"
"github.com/tealeg/xlsx" "github.com/tealeg/xlsx"
"path" "math"
"strconv"
) )
//Compares xls and xlsx files //Compares xls and xlsx files
func compareXlsXlsx(filepathname string) string { func compareXlsXlsx(xlsfilepathname string, xlsxfilepathname string) string {
xlsFile, err := Open(path.Join("testdata", filepathname)+".xls", "utf-8") xlsFile, err := Open(xlsfilepathname, "utf-8")
if err != nil { if err != nil {
return fmt.Sprintf("Cant open xls file: %s", err) 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 { if err != nil {
return fmt.Sprintf("Cant open xlsx file: %s", err) return fmt.Sprintf("Cant open xlsx file: %s", err)
} }
@ -29,8 +30,22 @@ func compareXlsXlsx(filepathname string) string {
xlsText := xlsRow.Col(cell) xlsText := xlsRow.Col(cell)
xlsxText := xlsxCell.String() xlsxText := xlsxCell.String()
if xlsText != xlsxText { if xlsText != xlsxText {
return fmt.Sprintf("Sheet: %d, row: %d, col: %d, xlsx: (%s)[%d], xls: (%s)[%d].", //try to convert to numbers
sheet, row, cell, xlsxText, len(xlsxText), xlsText, len(xlsText)) 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))
}
} }
} }
} }

View File

@ -1,14 +1,30 @@
package xls package xls
import ( import (
"io/ioutil"
"path"
"path/filepath"
"strings"
"testing" "testing"
) )
func TestIssue47(t *testing.T) { func TestIssue47(t *testing.T) {
e := compareXlsXlsx("issue47") testdatapath := "testdata"
files, err := ioutil.ReadDir(testdatapath)
if e != "" { if err != nil {
t.Fatalf("XLS an XLSX are not equal: %s", e) 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)
}
}
} }
} }

BIN
testdata/negatives.xls vendored Normal file

Binary file not shown.

BIN
testdata/negatives.xlsx vendored Normal file

Binary file not shown.

BIN
testdata/superstore.xls vendored Normal file

Binary file not shown.

BIN
testdata/superstore.xlsx vendored Normal file

Binary file not shown.