@@ -131,6 +131,7 @@ func (f *File) adjustColDimensions(ws *xlsxWorksheet, col, offset int) error {
131
131
if cellCol , cellRow , _ := CellNameToCoordinates (v .R ); col <= cellCol {
132
132
if newCol := cellCol + offset ; newCol > 0 {
133
133
ws .SheetData .Row [rowIdx ].C [colIdx ].R , _ = CoordinatesToCellName (newCol , cellRow )
134
+ _ = f .adjustFormula (ws .SheetData .Row [rowIdx ].C [colIdx ].F , columns , offset , false )
134
135
}
135
136
}
136
137
}
@@ -152,21 +153,46 @@ func (f *File) adjustRowDimensions(ws *xlsxWorksheet, row, offset int) error {
152
153
for i := 0 ; i < len (ws .SheetData .Row ); i ++ {
153
154
r := & ws .SheetData .Row [i ]
154
155
if newRow := r .R + offset ; r .R >= row && newRow > 0 {
155
- f .adjustSingleRowDimensions (r , newRow )
156
+ f .adjustSingleRowDimensions (r , newRow , offset , false )
156
157
}
157
158
}
158
159
return nil
159
160
}
160
161
161
162
// adjustSingleRowDimensions provides a function to adjust single row dimensions.
162
- func (f * File ) adjustSingleRowDimensions (r * xlsxRow , num int ) {
163
+ func (f * File ) adjustSingleRowDimensions (r * xlsxRow , num , offset int , si bool ) {
163
164
r .R = num
164
165
for i , col := range r .C {
165
166
colName , _ , _ := SplitCellName (col .R )
166
167
r .C [i ].R , _ = JoinCellName (colName , num )
168
+ _ = f .adjustFormula (col .F , rows , offset , si )
167
169
}
168
170
}
169
171
172
+ // adjustFormula provides a function to adjust shared formula reference.
173
+ func (f * File ) adjustFormula (formula * xlsxF , dir adjustDirection , offset int , si bool ) error {
174
+ if formula != nil && formula .Ref != "" {
175
+ coordinates , err := rangeRefToCoordinates (formula .Ref )
176
+ if err != nil {
177
+ return err
178
+ }
179
+ if dir == columns {
180
+ coordinates [0 ] += offset
181
+ coordinates [2 ] += offset
182
+ } else {
183
+ coordinates [1 ] += offset
184
+ coordinates [3 ] += offset
185
+ }
186
+ if formula .Ref , err = f .coordinatesToRangeRef (coordinates ); err != nil {
187
+ return err
188
+ }
189
+ if si && formula .Si != nil {
190
+ formula .Si = intPtr (* formula .Si + 1 )
191
+ }
192
+ }
193
+ return nil
194
+ }
195
+
170
196
// adjustHyperlinks provides a function to update hyperlinks when inserting or
171
197
// deleting rows or columns.
172
198
func (f * File ) adjustHyperlinks (ws * xlsxWorksheet , sheet string , dir adjustDirection , num , offset int ) {
0 commit comments