Skip to content

Commit 4180c20

Browse files
committed
This closes qax-os#2130, remove shared formula in calculation chain when remove row
- Add unit tests - Update dependencies modules
1 parent 2c3d13f commit 4180c20

File tree

5 files changed

+47
-49
lines changed

5 files changed

+47
-49
lines changed

cell.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ func (f *File) removeFormula(c *xlsxC, ws *xlsxWorksheet, sheet string) error {
194194
for col, cell := range row.C {
195195
if cell.F != nil && cell.F.Si != nil && *cell.F.Si == *si {
196196
ws.SheetData.Row[r].C[col].F = nil
197-
ws.deleteSharedFormula(c)
197+
ws.formulaSI.Delete(si)
198198
_ = f.deleteCalcChain(sheetID, cell.R)
199199
}
200200
}

go.mod

+6-6
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ go 1.23.0
55
require (
66
github.com/richardlehane/mscfb v1.0.4
77
github.com/stretchr/testify v1.10.0
8-
github.com/tiendc/go-deepcopy v1.5.2
9-
github.com/xuri/efp v0.0.0-20250227110027-3491fafc2b79
10-
github.com/xuri/nfp v0.0.0-20250226145837-86d5fc24b2ba
11-
golang.org/x/crypto v0.36.0
8+
github.com/tiendc/go-deepcopy v1.6.0
9+
github.com/xuri/efp v0.0.1
10+
github.com/xuri/nfp v0.0.1
11+
golang.org/x/crypto v0.38.0
1212
golang.org/x/image v0.25.0
13-
golang.org/x/net v0.38.0
14-
golang.org/x/text v0.23.0
13+
golang.org/x/net v0.40.0
14+
golang.org/x/text v0.25.0
1515
)
1616

1717
require (

go.sum

+12-12
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,20 @@ github.com/richardlehane/msoleps v1.0.4 h1:WuESlvhX3gH2IHcd8UqyCuFY5yiq/GR/yqaSM
99
github.com/richardlehane/msoleps v1.0.4/go.mod h1:BWev5JBpU9Ko2WAgmZEuiz4/u3ZYTKbjLycmwiWUfWg=
1010
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
1111
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
12-
github.com/tiendc/go-deepcopy v1.5.2 h1:fzTSgAOzxw4MFuDzvyxRDUsdwA7qs7FBTvgXVj28NpQ=
13-
github.com/tiendc/go-deepcopy v1.5.2/go.mod h1:toXoeQoUqXOOS/X4sKuiAoSk6elIdqc0pN7MTgOOo2I=
14-
github.com/xuri/efp v0.0.0-20250227110027-3491fafc2b79 h1:78nKszZqigiBRBVcoe/AuPzyLTWW5B+ltBaUX1rlIXA=
15-
github.com/xuri/efp v0.0.0-20250227110027-3491fafc2b79/go.mod h1:ybY/Jr0T0GTCnYjKqmdwxyxn2BQf2RcQIIvex5QldPI=
16-
github.com/xuri/nfp v0.0.0-20250226145837-86d5fc24b2ba h1:DhIu6n3qU0joqG9f4IO6a/Gkerd+flXrmlJ+0yX2W8U=
17-
github.com/xuri/nfp v0.0.0-20250226145837-86d5fc24b2ba/go.mod h1:WwHg+CVyzlv/TX9xqBFXEZAuxOPxn2k1GNHwG41IIUQ=
18-
golang.org/x/crypto v0.36.0 h1:AnAEvhDddvBdpY+uR+MyHmuZzzNqXSe/GvuDeob5L34=
19-
golang.org/x/crypto v0.36.0/go.mod h1:Y4J0ReaxCR1IMaabaSMugxJES1EpwhBHhv2bDHklZvc=
12+
github.com/tiendc/go-deepcopy v1.6.0 h1:0UtfV/imoCwlLxVsyfUd4hNHnB3drXsfle+wzSCA5Wo=
13+
github.com/tiendc/go-deepcopy v1.6.0/go.mod h1:toXoeQoUqXOOS/X4sKuiAoSk6elIdqc0pN7MTgOOo2I=
14+
github.com/xuri/efp v0.0.1 h1:fws5Rv3myXyYni8uwj2qKjVaRP30PdjeYe2Y6FDsCL8=
15+
github.com/xuri/efp v0.0.1/go.mod h1:ybY/Jr0T0GTCnYjKqmdwxyxn2BQf2RcQIIvex5QldPI=
16+
github.com/xuri/nfp v0.0.1 h1:MDamSGatIvp8uOmDP8FnmjuQpu90NzdJxo7242ANR9Q=
17+
github.com/xuri/nfp v0.0.1/go.mod h1:WwHg+CVyzlv/TX9xqBFXEZAuxOPxn2k1GNHwG41IIUQ=
18+
golang.org/x/crypto v0.38.0 h1:jt+WWG8IZlBnVbomuhg2Mdq0+BBQaHbtqHEFEigjUV8=
19+
golang.org/x/crypto v0.38.0/go.mod h1:MvrbAqul58NNYPKnOra203SB9vpuZW0e+RRZV+Ggqjw=
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.38.0 h1:vRMAPTMaeGqVhG5QyLJHqNDwecKTomGeqbnfZyKlBI8=
23-
golang.org/x/net v0.38.0/go.mod h1:ivrbrMbzFq5J41QOQh0siUuly180yBYtLp+CKbEaFx8=
24-
golang.org/x/text v0.23.0 h1:D71I7dUrlY+VX0gQShAThNGHFxZ13dGLBHQLVl1mJlY=
25-
golang.org/x/text v0.23.0/go.mod h1:/BLNzu4aZCJ1+kcD0DNRotWKage4q2rGVAg4o22unh4=
22+
golang.org/x/net v0.40.0 h1:79Xs7wF06Gbdcg4kdCCIQArK11Z1hr5POQ6+fIYHNuY=
23+
golang.org/x/net v0.40.0/go.mod h1:y0hY0exeL2Pku80/zKK7tpntoX23cqL3Oa6njdgRtds=
24+
golang.org/x/text v0.25.0 h1:qVyWApTSYLk/drJRO5mDlNYskwQznZmkpV2c8q9zls4=
25+
golang.org/x/text v0.25.0/go.mod h1:WEdwpYrmk1qmdHvhkSTNPm3app7v4rsT8F2UD6+VHIA=
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=

rows.go

+10
Original file line numberDiff line numberDiff line change
@@ -643,6 +643,16 @@ func (f *File) RemoveRow(sheet string, row int) error {
643643
if row > len(ws.SheetData.Row) {
644644
return f.adjustHelper(sheet, rows, row, -1)
645645
}
646+
for rowIdx := range ws.SheetData.Row {
647+
v := &ws.SheetData.Row[rowIdx]
648+
if v.R == row {
649+
for _, c := range v.C {
650+
if err := f.removeFormula(&c, ws, sheet); err != nil {
651+
return err
652+
}
653+
}
654+
}
655+
}
646656
keep := 0
647657
for rowIdx := range ws.SheetData.Row {
648658
v := &ws.SheetData.Row[rowIdx]

rows_test.go

+18-30
Original file line numberDiff line numberDiff line change
@@ -315,41 +315,27 @@ func TestRemoveRow(t *testing.T) {
315315
assert.EqualError(t, f.RemoveRow(sheet1, 0), newInvalidRowNumberError(0).Error())
316316

317317
assert.NoError(t, f.RemoveRow(sheet1, 4))
318-
if !assert.Len(t, r.SheetData.Row, rowCount-1) {
319-
t.FailNow()
320-
}
318+
assert.Len(t, r.SheetData.Row, rowCount-1)
321319

322320
assert.NoError(t, f.MergeCell(sheet1, "B3", "B5"))
323321

324322
assert.NoError(t, f.RemoveRow(sheet1, 2))
325-
if !assert.Len(t, r.SheetData.Row, rowCount-2) {
326-
t.FailNow()
327-
}
323+
assert.Len(t, r.SheetData.Row, rowCount-2)
328324

329325
assert.NoError(t, f.RemoveRow(sheet1, 4))
330-
if !assert.Len(t, r.SheetData.Row, rowCount-3) {
331-
t.FailNow()
332-
}
326+
assert.Len(t, r.SheetData.Row, rowCount-3)
333327

334328
err = f.AutoFilter(sheet1, "A2:A2", []AutoFilterOptions{{Column: "A", Expression: "x != blanks"}})
335-
if !assert.NoError(t, err) {
336-
t.FailNow()
337-
}
329+
assert.NoError(t, err)
338330

339331
assert.NoError(t, f.RemoveRow(sheet1, 1))
340-
if !assert.Len(t, r.SheetData.Row, rowCount-4) {
341-
t.FailNow()
342-
}
332+
assert.Len(t, r.SheetData.Row, rowCount-4)
343333

344334
assert.NoError(t, f.RemoveRow(sheet1, 2))
345-
if !assert.Len(t, r.SheetData.Row, rowCount-5) {
346-
t.FailNow()
347-
}
335+
assert.Len(t, r.SheetData.Row, rowCount-5)
348336

349337
assert.NoError(t, f.RemoveRow(sheet1, 1))
350-
if !assert.Len(t, r.SheetData.Row, rowCount-6) {
351-
t.FailNow()
352-
}
338+
assert.Len(t, r.SheetData.Row, rowCount-6)
353339

354340
assert.NoError(t, f.RemoveRow(sheet1, 10))
355341
assert.NoError(t, f.SaveAs(filepath.Join("test", "TestRemoveRow.xlsx")))
@@ -367,6 +353,14 @@ func TestRemoveRow(t *testing.T) {
367353
assert.EqualError(t, f.RemoveRow("SheetN", 1), "sheet SheetN does not exist")
368354
// Test remove row with invalid sheet name
369355
assert.EqualError(t, f.RemoveRow("Sheet:1", 1), ErrSheetNameInvalid.Error())
356+
357+
f = NewFile()
358+
formulaType, ref := STCellFormulaTypeShared, "C1:C5"
359+
assert.NoError(t, f.SetCellFormula("Sheet1", "C1", "=A1+B1",
360+
FormulaOpts{Ref: &ref, Type: &formulaType}))
361+
f.CalcChain = nil
362+
f.Pkg.Store(defaultXMLPathCalcChain, MacintoshCyrillicCharset)
363+
assert.EqualError(t, f.RemoveRow("Sheet1", 1), "XML syntax error on line 1: invalid UTF-8")
370364
}
371365

372366
func TestInsertRows(t *testing.T) {
@@ -383,19 +377,13 @@ func TestInsertRows(t *testing.T) {
383377
assert.NoError(t, f.SetCellHyperLink(sheet1, "A5", "https://github.com/xuri/excelize", "External"))
384378

385379
assert.NoError(t, f.InsertRows(sheet1, 1, 1))
386-
if !assert.Len(t, r.SheetData.Row, rowCount+1) {
387-
t.FailNow()
388-
}
380+
assert.Len(t, r.SheetData.Row, rowCount+1)
389381

390382
assert.NoError(t, f.InsertRows(sheet1, 4, 1))
391-
if !assert.Len(t, r.SheetData.Row, rowCount+2) {
392-
t.FailNow()
393-
}
383+
assert.Len(t, r.SheetData.Row, rowCount+2)
394384

395385
assert.NoError(t, f.InsertRows(sheet1, 4, 2))
396-
if !assert.Len(t, r.SheetData.Row, rowCount+4) {
397-
t.FailNow()
398-
}
386+
assert.Len(t, r.SheetData.Row, rowCount+4)
399387
// Test insert rows with invalid sheet name
400388
assert.EqualError(t, f.InsertRows("Sheet:1", 1, 1), ErrSheetNameInvalid.Error())
401389

0 commit comments

Comments
 (0)