Skip to content

Commit

Permalink
#36 support sheet format version2
Browse files Browse the repository at this point in the history
  • Loading branch information
future-mano committed Oct 1, 2022
1 parent b143efc commit 3e72947
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 2 deletions.
40 changes: 38 additions & 2 deletions exceltesting.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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"
}
38 changes: 38 additions & 0 deletions exceltesting_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Binary file added testdata/load_v2.xlsx
Binary file not shown.

0 comments on commit 3e72947

Please sign in to comment.