Skip to content

Commit fdb4418

Browse files
ma91nfuture-mano
andauthored
#36 support sheet format version2 (#43)
* #36 support sheet format version2 * Add test Co-authored-by: future-mano <[email protected]>
1 parent 20a5321 commit fdb4418

File tree

4 files changed

+93
-2
lines changed

4 files changed

+93
-2
lines changed

exceltesting.go

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ func (e *exceltesing) LoadWithContext(ctx context.Context, r LoadRequest) error
5555
return fmt.Errorf("exceltesing: excelize.OpenFile: %w", err)
5656
}
5757
defer f.Close()
58+
5859
for _, sheet := range f.GetSheetList() {
5960
if slices.Contains(r.IgnoreSheet, sheet) {
6061
continue
@@ -290,8 +291,15 @@ type DumpRequest struct {
290291
}
291292

292293
func (e *exceltesing) loadExcelSheet(f *excelize.File, targetSheet string) (*table, error) {
293-
const tableNmCell = "A2"
294-
const columnDefineRowNum = 9
294+
var (
295+
tableNmCell = "A2"
296+
columnDefineRowNum = 9
297+
)
298+
299+
formatVersion := extractSheetFormatVersion(f, targetSheet)
300+
if formatVersion == "2.0" {
301+
columnDefineRowNum = 6
302+
}
295303

296304
tableNm, err := f.GetCellValue(targetSheet, tableNmCell)
297305
if err != nil {
@@ -520,3 +528,31 @@ func convert(vs [][]any, columns []string) [][]x {
520528
}
521529
return resp
522530
}
531+
532+
// extractSheetFormatVersion is extracting exceltesting sheet format version.
533+
// default 1.0
534+
func extractSheetFormatVersion(f *excelize.File, sheet string) string {
535+
index := f.GetSheetIndex(sheet)
536+
if index == -1 {
537+
return "1.0"
538+
}
539+
540+
rows, err := f.GetRows(sheet)
541+
if err != nil {
542+
return "1.0"
543+
}
544+
if len(rows) < 3 {
545+
return "1.0"
546+
}
547+
548+
row := rows[2] // 3行目に記載があるとする
549+
if len(row) < 2 {
550+
return "1.0"
551+
}
552+
553+
if strings.TrimSpace(strings.ToLower(row[0])) == "version" {
554+
return strings.TrimSpace(row[1])
555+
}
556+
557+
return "1.0"
558+
}

exceltesting_test.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,44 @@ func Test_exceltesing_Load(t *testing.T) {
106106
},
107107
},
108108
},
109+
{
110+
name: "inserted excel data using sheet format version2",
111+
r: LoadRequest{
112+
TargetBookPath: filepath.Join("testdata", "load_v2.xlsx"),
113+
SheetPrefix: "normal-",
114+
IgnoreSheet: nil,
115+
},
116+
want: []testX{
117+
{
118+
ID: "test1",
119+
A: true,
120+
B: []byte("bytea"),
121+
C: "a",
122+
D: time.Date(2022, 1, 1, 0, 0, 0, 0, time.UTC),
123+
E: 0.1,
124+
F: 0.01,
125+
G: pgtype.JSON{Bytes: []uint8("{}"), Status: pgtype.Present},
126+
H: pgtype.JSONB{Bytes: []uint8("{}"), Status: pgtype.Present},
127+
I: pgtype.Inet{IPNet: &net.IPNet{IP: net.ParseIP("0.0.0.0"), Mask: net.IPv4Mask(255, 255, 255, 255)}, Status: pgtype.Present},
128+
J: 32767,
129+
K: 2147483647,
130+
L: 9223372036854775807,
131+
M: "00:00:01",
132+
N: 11111,
133+
O: 0,
134+
P: "test",
135+
Q: "01:02:03",
136+
S: time.Date(2022, 1, 1, 1, 2, 3, 0, time.UTC),
137+
T: time.Date(2022, 1, 1, 1, 2, 3, 0, jst),
138+
U: "cee0db76-d69c-4ae3-ae33-5b5970adde48",
139+
V: "abc",
140+
W: 1,
141+
X: 1,
142+
Y: 1,
143+
Z: 1,
144+
},
145+
},
146+
},
109147
}
110148
for _, tt := range tests {
111149
t.Run(tt.name, func(t *testing.T) {
@@ -160,6 +198,23 @@ func Test_exceltesing_Compare(t *testing.T) {
160198
wantFile: filepath.Join("testdata", "compare.xlsx"),
161199
equal: true,
162200
},
201+
{
202+
name: "equal on exceltesing version 2.0 sheet",
203+
input: func(t *testing.T) {
204+
t.Helper()
205+
tdb := testonly.OpenTestDB(t)
206+
defer tdb.Close()
207+
if _, err := tdb.Exec(`TRUNCATE company;`); err != nil {
208+
t.Fatal(err)
209+
}
210+
if _, err := tdb.Exec(`INSERT INTO company (company_cd,company_name,founded_year,created_at,updated_at,revision)
211+
VALUES ('00001','Future',1989,current_timestamp,current_timestamp,1),('00002','YDC',1972,current_timestamp,current_timestamp,1);`); err != nil {
212+
t.Fatal(err)
213+
}
214+
},
215+
wantFile: filepath.Join("testdata", "compare_v2.xlsx"),
216+
equal: true,
217+
},
163218
{
164219
name: "diff",
165220
input: func(t *testing.T) {

testdata/compare_v2.xlsx

84 KB
Binary file not shown.

testdata/load_v2.xlsx

85.9 KB
Binary file not shown.

0 commit comments

Comments
 (0)