@@ -35,44 +35,44 @@ const (
35
35
// TODO: adjustPageBreaks, adjustComments, adjustDataValidations, adjustProtectedCells
36
36
//
37
37
func (f * File ) adjustHelper (sheet string , dir adjustDirection , num , offset int ) error {
38
- xlsx , err := f .workSheetReader (sheet )
38
+ ws , err := f .workSheetReader (sheet )
39
39
if err != nil {
40
40
return err
41
41
}
42
42
if dir == rows {
43
- f .adjustRowDimensions (xlsx , num , offset )
43
+ f .adjustRowDimensions (ws , num , offset )
44
44
} else {
45
- f .adjustColDimensions (xlsx , num , offset )
45
+ f .adjustColDimensions (ws , num , offset )
46
46
}
47
- f .adjustHyperlinks (xlsx , sheet , dir , num , offset )
48
- if err = f .adjustMergeCells (xlsx , dir , num , offset ); err != nil {
47
+ f .adjustHyperlinks (ws , sheet , dir , num , offset )
48
+ if err = f .adjustMergeCells (ws , dir , num , offset ); err != nil {
49
49
return err
50
50
}
51
- if err = f .adjustAutoFilter (xlsx , dir , num , offset ); err != nil {
51
+ if err = f .adjustAutoFilter (ws , dir , num , offset ); err != nil {
52
52
return err
53
53
}
54
54
if err = f .adjustCalcChain (dir , num , offset ); err != nil {
55
55
return err
56
56
}
57
- checkSheet (xlsx )
58
- _ = checkRow (xlsx )
57
+ checkSheet (ws )
58
+ _ = checkRow (ws )
59
59
60
- if xlsx .MergeCells != nil && len (xlsx .MergeCells .Cells ) == 0 {
61
- xlsx .MergeCells = nil
60
+ if ws .MergeCells != nil && len (ws .MergeCells .Cells ) == 0 {
61
+ ws .MergeCells = nil
62
62
}
63
63
64
64
return nil
65
65
}
66
66
67
67
// adjustColDimensions provides a function to update column dimensions when
68
68
// inserting or deleting rows or columns.
69
- func (f * File ) adjustColDimensions (xlsx * xlsxWorksheet , col , offset int ) {
70
- for rowIdx := range xlsx .SheetData .Row {
71
- for colIdx , v := range xlsx .SheetData .Row [rowIdx ].C {
69
+ func (f * File ) adjustColDimensions (ws * xlsxWorksheet , col , offset int ) {
70
+ for rowIdx := range ws .SheetData .Row {
71
+ for colIdx , v := range ws .SheetData .Row [rowIdx ].C {
72
72
cellCol , cellRow , _ := CellNameToCoordinates (v .R )
73
73
if col <= cellCol {
74
74
if newCol := cellCol + offset ; newCol > 0 {
75
- xlsx .SheetData .Row [rowIdx ].C [colIdx ].R , _ = CoordinatesToCellName (newCol , cellRow )
75
+ ws .SheetData .Row [rowIdx ].C [colIdx ].R , _ = CoordinatesToCellName (newCol , cellRow )
76
76
}
77
77
}
78
78
}
@@ -81,9 +81,9 @@ func (f *File) adjustColDimensions(xlsx *xlsxWorksheet, col, offset int) {
81
81
82
82
// adjustRowDimensions provides a function to update row dimensions when
83
83
// inserting or deleting rows or columns.
84
- func (f * File ) adjustRowDimensions (xlsx * xlsxWorksheet , row , offset int ) {
85
- for i := range xlsx .SheetData .Row {
86
- r := & xlsx .SheetData .Row [i ]
84
+ func (f * File ) adjustRowDimensions (ws * xlsxWorksheet , row , offset int ) {
85
+ for i := range ws .SheetData .Row {
86
+ r := & ws .SheetData .Row [i ]
87
87
if newRow := r .R + offset ; r .R >= row && newRow > 0 {
88
88
f .ajustSingleRowDimensions (r , newRow )
89
89
}
@@ -101,38 +101,35 @@ func (f *File) ajustSingleRowDimensions(r *xlsxRow, num int) {
101
101
102
102
// adjustHyperlinks provides a function to update hyperlinks when inserting or
103
103
// deleting rows or columns.
104
- func (f * File ) adjustHyperlinks (xlsx * xlsxWorksheet , sheet string , dir adjustDirection , num , offset int ) {
104
+ func (f * File ) adjustHyperlinks (ws * xlsxWorksheet , sheet string , dir adjustDirection , num , offset int ) {
105
105
// short path
106
- if xlsx .Hyperlinks == nil || len (xlsx .Hyperlinks .Hyperlink ) == 0 {
106
+ if ws .Hyperlinks == nil || len (ws .Hyperlinks .Hyperlink ) == 0 {
107
107
return
108
108
}
109
109
110
110
// order is important
111
111
if offset < 0 {
112
- for i := len (xlsx .Hyperlinks .Hyperlink ) - 1 ; i >= 0 ; i -- {
113
- linkData := xlsx .Hyperlinks .Hyperlink [i ]
112
+ for i := len (ws .Hyperlinks .Hyperlink ) - 1 ; i >= 0 ; i -- {
113
+ linkData := ws .Hyperlinks .Hyperlink [i ]
114
114
colNum , rowNum , _ := CellNameToCoordinates (linkData .Ref )
115
115
116
116
if (dir == rows && num == rowNum ) || (dir == columns && num == colNum ) {
117
117
f .deleteSheetRelationships (sheet , linkData .RID )
118
- if len (xlsx .Hyperlinks .Hyperlink ) > 1 {
119
- xlsx .Hyperlinks .Hyperlink = append (xlsx .Hyperlinks .Hyperlink [:i ],
120
- xlsx .Hyperlinks .Hyperlink [i + 1 :]... )
118
+ if len (ws .Hyperlinks .Hyperlink ) > 1 {
119
+ ws .Hyperlinks .Hyperlink = append (ws .Hyperlinks .Hyperlink [:i ],
120
+ ws .Hyperlinks .Hyperlink [i + 1 :]... )
121
121
} else {
122
- xlsx .Hyperlinks = nil
122
+ ws .Hyperlinks = nil
123
123
}
124
124
}
125
125
}
126
126
}
127
-
128
- if xlsx .Hyperlinks == nil {
127
+ if ws .Hyperlinks == nil {
129
128
return
130
129
}
131
-
132
- for i := range xlsx .Hyperlinks .Hyperlink {
133
- link := & xlsx .Hyperlinks .Hyperlink [i ] // get reference
130
+ for i := range ws .Hyperlinks .Hyperlink {
131
+ link := & ws .Hyperlinks .Hyperlink [i ] // get reference
134
132
colNum , rowNum , _ := CellNameToCoordinates (link .Ref )
135
-
136
133
if dir == rows {
137
134
if rowNum >= num {
138
135
link .Ref , _ = CoordinatesToCellName (colNum , rowNum + offset )
@@ -147,21 +144,21 @@ func (f *File) adjustHyperlinks(xlsx *xlsxWorksheet, sheet string, dir adjustDir
147
144
148
145
// adjustAutoFilter provides a function to update the auto filter when
149
146
// inserting or deleting rows or columns.
150
- func (f * File ) adjustAutoFilter (xlsx * xlsxWorksheet , dir adjustDirection , num , offset int ) error {
151
- if xlsx .AutoFilter == nil {
147
+ func (f * File ) adjustAutoFilter (ws * xlsxWorksheet , dir adjustDirection , num , offset int ) error {
148
+ if ws .AutoFilter == nil {
152
149
return nil
153
150
}
154
151
155
- coordinates , err := f .areaRefToCoordinates (xlsx .AutoFilter .Ref )
152
+ coordinates , err := f .areaRefToCoordinates (ws .AutoFilter .Ref )
156
153
if err != nil {
157
154
return err
158
155
}
159
156
x1 , y1 , x2 , y2 := coordinates [0 ], coordinates [1 ], coordinates [2 ], coordinates [3 ]
160
157
161
158
if (dir == rows && y1 == num && offset < 0 ) || (dir == columns && x1 == num && x2 == num ) {
162
- xlsx .AutoFilter = nil
163
- for rowIdx := range xlsx .SheetData .Row {
164
- rowData := & xlsx .SheetData .Row [rowIdx ]
159
+ ws .AutoFilter = nil
160
+ for rowIdx := range ws .SheetData .Row {
161
+ rowData := & ws .SheetData .Row [rowIdx ]
165
162
if rowData .R > y1 && rowData .R <= y2 {
166
163
rowData .Hidden = false
167
164
}
@@ -172,7 +169,7 @@ func (f *File) adjustAutoFilter(xlsx *xlsxWorksheet, dir adjustDirection, num, o
172
169
coordinates = f .adjustAutoFilterHelper (dir , coordinates , num , offset )
173
170
x1 , y1 , x2 , y2 = coordinates [0 ], coordinates [1 ], coordinates [2 ], coordinates [3 ]
174
171
175
- if xlsx .AutoFilter .Ref , err = f .coordinatesToAreaRef ([]int {x1 , y1 , x2 , y2 }); err != nil {
172
+ if ws .AutoFilter .Ref , err = f .coordinatesToAreaRef ([]int {x1 , y1 , x2 , y2 }); err != nil {
176
173
return err
177
174
}
178
175
return nil
@@ -251,35 +248,35 @@ func (f *File) coordinatesToAreaRef(coordinates []int) (string, error) {
251
248
252
249
// adjustMergeCells provides a function to update merged cells when inserting
253
250
// or deleting rows or columns.
254
- func (f * File ) adjustMergeCells (xlsx * xlsxWorksheet , dir adjustDirection , num , offset int ) error {
255
- if xlsx .MergeCells == nil {
251
+ func (f * File ) adjustMergeCells (ws * xlsxWorksheet , dir adjustDirection , num , offset int ) error {
252
+ if ws .MergeCells == nil {
256
253
return nil
257
254
}
258
255
259
- for i := 0 ; i < len (xlsx .MergeCells .Cells ); i ++ {
260
- areaData := xlsx .MergeCells .Cells [i ]
256
+ for i := 0 ; i < len (ws .MergeCells .Cells ); i ++ {
257
+ areaData := ws .MergeCells .Cells [i ]
261
258
coordinates , err := f .areaRefToCoordinates (areaData .Ref )
262
259
if err != nil {
263
260
return err
264
261
}
265
262
x1 , y1 , x2 , y2 := coordinates [0 ], coordinates [1 ], coordinates [2 ], coordinates [3 ]
266
263
if dir == rows {
267
264
if y1 == num && y2 == num && offset < 0 {
268
- f .deleteMergeCell (xlsx , i )
265
+ f .deleteMergeCell (ws , i )
269
266
i --
270
267
}
271
268
y1 = f .adjustMergeCellsHelper (y1 , num , offset )
272
269
y2 = f .adjustMergeCellsHelper (y2 , num , offset )
273
270
} else {
274
271
if x1 == num && x2 == num && offset < 0 {
275
- f .deleteMergeCell (xlsx , i )
272
+ f .deleteMergeCell (ws , i )
276
273
i --
277
274
}
278
275
x1 = f .adjustMergeCellsHelper (x1 , num , offset )
279
276
x2 = f .adjustMergeCellsHelper (x2 , num , offset )
280
277
}
281
278
if x1 == x2 && y1 == y2 {
282
- f .deleteMergeCell (xlsx , i )
279
+ f .deleteMergeCell (ws , i )
283
280
i --
284
281
}
285
282
if areaData .Ref , err = f .coordinatesToAreaRef ([]int {x1 , y1 , x2 , y2 }); err != nil {
@@ -304,10 +301,10 @@ func (f *File) adjustMergeCellsHelper(pivot, num, offset int) int {
304
301
}
305
302
306
303
// deleteMergeCell provides a function to delete merged cell by given index.
307
- func (f * File ) deleteMergeCell (sheet * xlsxWorksheet , idx int ) {
308
- if len (sheet .MergeCells .Cells ) > idx {
309
- sheet .MergeCells .Cells = append (sheet .MergeCells .Cells [:idx ], sheet .MergeCells .Cells [idx + 1 :]... )
310
- sheet .MergeCells .Count = len (sheet .MergeCells .Cells )
304
+ func (f * File ) deleteMergeCell (ws * xlsxWorksheet , idx int ) {
305
+ if len (ws .MergeCells .Cells ) > idx {
306
+ ws .MergeCells .Cells = append (ws .MergeCells .Cells [:idx ], ws .MergeCells .Cells [idx + 1 :]... )
307
+ ws .MergeCells .Count = len (ws .MergeCells .Cells )
311
308
}
312
309
}
313
310
0 commit comments