diff --git a/exceltesting.go b/exceltesting.go index 6bf2b78..fbc4dd7 100644 --- a/exceltesting.go +++ b/exceltesting.go @@ -55,6 +55,7 @@ func (e *exceltesing) LoadWithContext(ctx context.Context, r LoadRequest) error return fmt.Errorf("exceltesing: excelize.OpenFile: %w", err) } defer f.Close() + for _, sheet := range f.GetSheetList() { if slices.Contains(r.IgnoreSheet, sheet) { continue @@ -290,8 +291,15 @@ type DumpRequest struct { } func (e *exceltesing) loadExcelSheet(f *excelize.File, targetSheet string) (*table, error) { - const tableNmCell = "A2" - const columnDefineRowNum = 9 + var ( + tableNmCell = "A2" + columnDefineRowNum = 9 + ) + + formatVersion := extractSheetFormatVersion(f, targetSheet) + if formatVersion == "2.0" { + columnDefineRowNum = 6 + } tableNm, err := f.GetCellValue(targetSheet, tableNmCell) if err != nil { @@ -520,3 +528,31 @@ func convert(vs [][]any, columns []string) [][]x { } return resp } + +// extractSheetFormatVersion is extracting exceltesting sheet format version. +// default 1.0 +func extractSheetFormatVersion(f *excelize.File, sheet string) string { + index := f.GetSheetIndex(sheet) + if index == -1 { + return "1.0" + } + + rows, err := f.GetRows(sheet) + if err != nil { + return "1.0" + } + if len(rows) < 3 { + return "1.0" + } + + row := rows[2] // 3行目に記載があるとする + if len(row) < 2 { + return "1.0" + } + + if strings.TrimSpace(strings.ToLower(row[0])) == "version" { + return strings.TrimSpace(row[1]) + } + + return "1.0" +} diff --git a/exceltesting_test.go b/exceltesting_test.go index c145f35..ecc33eb 100644 --- a/exceltesting_test.go +++ b/exceltesting_test.go @@ -106,6 +106,44 @@ func Test_exceltesing_Load(t *testing.T) { }, }, }, + { + name: "inserted excel data using sheet format version2", + r: LoadRequest{ + TargetBookPath: filepath.Join("testdata", "load_v2.xlsx"), + SheetPrefix: "normal-", + IgnoreSheet: nil, + }, + want: []testX{ + { + ID: "test1", + A: true, + B: []byte("bytea"), + C: "a", + D: time.Date(2022, 1, 1, 0, 0, 0, 0, time.UTC), + E: 0.1, + F: 0.01, + G: pgtype.JSON{Bytes: []uint8("{}"), Status: pgtype.Present}, + H: pgtype.JSONB{Bytes: []uint8("{}"), Status: pgtype.Present}, + I: pgtype.Inet{IPNet: &net.IPNet{IP: net.ParseIP("0.0.0.0"), Mask: net.IPv4Mask(255, 255, 255, 255)}, Status: pgtype.Present}, + J: 32767, + K: 2147483647, + L: 9223372036854775807, + M: "00:00:01", + N: 11111, + O: 0, + P: "test", + Q: "01:02:03", + S: time.Date(2022, 1, 1, 1, 2, 3, 0, time.UTC), + T: time.Date(2022, 1, 1, 1, 2, 3, 0, jst), + U: "cee0db76-d69c-4ae3-ae33-5b5970adde48", + V: "abc", + W: 1, + X: 1, + Y: 1, + Z: 1, + }, + }, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -160,6 +198,23 @@ func Test_exceltesing_Compare(t *testing.T) { wantFile: filepath.Join("testdata", "compare.xlsx"), equal: true, }, + { + name: "equal on exceltesing version 2.0 sheet", + input: func(t *testing.T) { + t.Helper() + tdb := testonly.OpenTestDB(t) + defer tdb.Close() + if _, err := tdb.Exec(`TRUNCATE company;`); err != nil { + t.Fatal(err) + } + if _, err := tdb.Exec(`INSERT INTO company (company_cd,company_name,founded_year,created_at,updated_at,revision) + VALUES ('00001','Future',1989,current_timestamp,current_timestamp,1),('00002','YDC',1972,current_timestamp,current_timestamp,1);`); err != nil { + t.Fatal(err) + } + }, + wantFile: filepath.Join("testdata", "compare_v2.xlsx"), + equal: true, + }, { name: "diff", input: func(t *testing.T) { diff --git a/testdata/compare_v2.xlsx b/testdata/compare_v2.xlsx new file mode 100644 index 0000000..1f85d8a Binary files /dev/null and b/testdata/compare_v2.xlsx differ diff --git a/testdata/load_v2.xlsx b/testdata/load_v2.xlsx new file mode 100644 index 0000000..309e38e Binary files /dev/null and b/testdata/load_v2.xlsx differ