Skip to content

Commit d13cd91

Browse files
authored
Add optional parameter withoutValues for the GetMergeCells function (qax-os#2184)
- Update dependencies modules - Update unit tests
1 parent b674220 commit d13cd91

File tree

4 files changed

+43
-18
lines changed

4 files changed

+43
-18
lines changed

go.mod

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ require (
88
github.com/tiendc/go-deepcopy v1.6.1
99
github.com/xuri/efp v0.0.1
1010
github.com/xuri/nfp v0.0.2-0.20250530014748-2ddeb826f9a9
11-
golang.org/x/crypto v0.39.0
11+
golang.org/x/crypto v0.40.0
1212
golang.org/x/image v0.25.0
13-
golang.org/x/net v0.41.0
14-
golang.org/x/text v0.26.0
13+
golang.org/x/net v0.42.0
14+
golang.org/x/text v0.27.0
1515
)
1616

1717
require (

go.sum

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@ github.com/xuri/efp v0.0.1 h1:fws5Rv3myXyYni8uwj2qKjVaRP30PdjeYe2Y6FDsCL8=
1515
github.com/xuri/efp v0.0.1/go.mod h1:ybY/Jr0T0GTCnYjKqmdwxyxn2BQf2RcQIIvex5QldPI=
1616
github.com/xuri/nfp v0.0.2-0.20250530014748-2ddeb826f9a9 h1:+C0TIdyyYmzadGaL/HBLbf3WdLgC29pgyhTjAT/0nuE=
1717
github.com/xuri/nfp v0.0.2-0.20250530014748-2ddeb826f9a9/go.mod h1:WwHg+CVyzlv/TX9xqBFXEZAuxOPxn2k1GNHwG41IIUQ=
18-
golang.org/x/crypto v0.39.0 h1:SHs+kF4LP+f+p14esP5jAoDpHU8Gu/v9lFRK6IT5imM=
19-
golang.org/x/crypto v0.39.0/go.mod h1:L+Xg3Wf6HoL4Bn4238Z6ft6KfEpN0tJGo53AAPC632U=
18+
golang.org/x/crypto v0.40.0 h1:r4x+VvoG5Fm+eJcxMaY8CQM7Lb0l1lsmjGBQ6s8BfKM=
19+
golang.org/x/crypto v0.40.0/go.mod h1:Qr1vMER5WyS2dfPHAlsOj01wgLbsyWtFn/aY+5+ZdxY=
2020
golang.org/x/image v0.25.0 h1:Y6uW6rH1y5y/LK1J8BPWZtr6yZ7hrsy6hFrXjgsc2fQ=
2121
golang.org/x/image v0.25.0/go.mod h1:tCAmOEGthTtkalusGp1g3xa2gke8J6c2N565dTyl9Rs=
22-
golang.org/x/net v0.41.0 h1:vBTly1HeNPEn3wtREYfy4GZ/NECgw2Cnl+nK6Nz3uvw=
23-
golang.org/x/net v0.41.0/go.mod h1:B/K4NNqkfmg07DQYrbwvSluqCJOOXwUjeb/5lOisjbA=
24-
golang.org/x/text v0.26.0 h1:P42AVeLghgTYr4+xUnTRKDMqpar+PtX7KWuNQL21L8M=
25-
golang.org/x/text v0.26.0/go.mod h1:QK15LZJUUQVJxhz7wXgxSy/CJaTFjd0G+YLonydOVQA=
22+
golang.org/x/net v0.42.0 h1:jzkYrhi3YQWD6MLBJcsklgQsoAcw89EcZbJw8Z614hs=
23+
golang.org/x/net v0.42.0/go.mod h1:FF1RA5d3u7nAYA4z2TkclSCKh68eSXtiFwcWQpPXdt8=
24+
golang.org/x/text v0.27.0 h1:4fGWRpyh641NLlecmyl4LOe6yDdfaYNrGb2zdfo4JV4=
25+
golang.org/x/text v0.27.0/go.mod h1:1D28KMCvyooCX9hBiosv5Tz/+YLxj0j7XhWjpSUF7CU=
2626
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
2727
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
2828
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=

merge.go

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -140,9 +140,24 @@ func (f *File) UnmergeCell(sheet, topLeftCell, bottomRightCell string) error {
140140
}
141141

142142
// GetMergeCells provides a function to get all merged cells from a specific
143-
// worksheet.
144-
func (f *File) GetMergeCells(sheet string) ([]MergeCell, error) {
145-
var mergeCells []MergeCell
143+
// worksheet. If the `withoutValues` parameter is set to true, it will not
144+
// return the cell values of merged cells, only the range reference will be
145+
// returned. For example get all merged cells on Sheet1:
146+
//
147+
// mergeCells, err := f.GetMergeCells("Sheet1")
148+
//
149+
// If you want to get merged cells without cell values, you can use the
150+
// following code:
151+
//
152+
// mergeCells, err := f.GetMergeCells("Sheet1", true)
153+
func (f *File) GetMergeCells(sheet string, withoutValues ...bool) ([]MergeCell, error) {
154+
var (
155+
mergeCells []MergeCell
156+
withoutVal bool
157+
)
158+
if len(withoutValues) > 0 {
159+
withoutVal = withoutValues[0]
160+
}
146161
ws, err := f.workSheetReader(sheet)
147162
if err != nil {
148163
return mergeCells, err
@@ -153,9 +168,11 @@ func (f *File) GetMergeCells(sheet string) ([]MergeCell, error) {
153168
}
154169
mergeCells = make([]MergeCell, 0, len(ws.MergeCells.Cells))
155170
for i := range ws.MergeCells.Cells {
156-
ref := ws.MergeCells.Cells[i].Ref
157-
cell := strings.Split(ref, ":")[0]
158-
val, _ := f.GetCellValue(sheet, cell)
171+
ref, val := ws.MergeCells.Cells[i].Ref, ""
172+
if !withoutVal {
173+
cell := strings.Split(ref, ":")[0]
174+
val, _ = f.GetCellValue(sheet, cell)
175+
}
159176
mergeCells = append(mergeCells, []string{ref, val})
160177
}
161178
}

merge_test.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,16 +138,24 @@ func TestGetMergeCells(t *testing.T) {
138138
sheet1 := f.GetSheetName(0)
139139

140140
mergeCells, err := f.GetMergeCells(sheet1)
141-
if !assert.Len(t, mergeCells, len(wants)) {
142-
t.FailNow()
143-
}
144141
assert.NoError(t, err)
142+
assert.Len(t, mergeCells, len(wants))
145143

146144
for i, m := range mergeCells {
147145
assert.Equal(t, wants[i].value, m.GetCellValue())
148146
assert.Equal(t, wants[i].start, m.GetStartAxis())
149147
assert.Equal(t, wants[i].end, m.GetEndAxis())
150148
}
149+
// Test get merged cells without cell values
150+
mergeCells, err = f.GetMergeCells(sheet1, true)
151+
assert.NoError(t, err)
152+
assert.Len(t, mergeCells, len(wants))
153+
for i, m := range mergeCells {
154+
assert.Empty(t, m.GetCellValue())
155+
assert.Equal(t, wants[i].start, m.GetStartAxis())
156+
assert.Equal(t, wants[i].end, m.GetEndAxis())
157+
}
158+
151159
// Test get merged cells with invalid sheet name
152160
_, err = f.GetMergeCells("Sheet:1")
153161
assert.EqualError(t, err, ErrSheetNameInvalid.Error())

0 commit comments

Comments
 (0)