OSDN Git Service

This closes #979, fix the data validation deletion issue and tidy the internal functi...
[excelize/excelize.git] / adjust.go
1 // Copyright 2016 - 2021 The excelize Authors. All rights reserved. Use of
2 // this source code is governed by a BSD-style license that can be found in
3 // the LICENSE file.
4 //
5 // Package excelize providing a set of functions that allow you to write to
6 // and read from XLSX / XLSM / XLTM files. Supports reading and writing
7 // spreadsheet documents generated by Microsoft Excelâ„¢ 2007 and later. Supports
8 // complex components by high compatibility, and provided streaming API for
9 // generating or reading data from a worksheet with huge amounts of data. This
10 // library needs Go version 1.15 or later.
11
12 package excelize
13
14 type adjustDirection bool
15
16 const (
17         columns adjustDirection = false
18         rows    adjustDirection = true
19 )
20
21 // adjustHelper provides a function to adjust rows and columns dimensions,
22 // hyperlinks, merged cells and auto filter when inserting or deleting rows or
23 // columns.
24 //
25 // sheet: Worksheet name that we're editing
26 // column: Index number of the column we're inserting/deleting before
27 // row: Index number of the row we're inserting/deleting before
28 // offset: Number of rows/column to insert/delete negative values indicate deletion
29 //
30 // TODO: adjustPageBreaks, adjustComments, adjustDataValidations, adjustProtectedCells
31 //
32 func (f *File) adjustHelper(sheet string, dir adjustDirection, num, offset int) error {
33         ws, err := f.workSheetReader(sheet)
34         if err != nil {
35                 return err
36         }
37         sheetID := f.getSheetID(sheet)
38         if dir == rows {
39                 f.adjustRowDimensions(ws, num, offset)
40         } else {
41                 f.adjustColDimensions(ws, num, offset)
42         }
43         f.adjustHyperlinks(ws, sheet, dir, num, offset)
44         if err = f.adjustMergeCells(ws, dir, num, offset); err != nil {
45                 return err
46         }
47         if err = f.adjustAutoFilter(ws, dir, num, offset); err != nil {
48                 return err
49         }
50         if err = f.adjustCalcChain(dir, num, offset, sheetID); err != nil {
51                 return err
52         }
53         checkSheet(ws)
54         _ = checkRow(ws)
55
56         if ws.MergeCells != nil && len(ws.MergeCells.Cells) == 0 {
57                 ws.MergeCells = nil
58         }
59
60         return nil
61 }
62
63 // adjustColDimensions provides a function to update column dimensions when
64 // inserting or deleting rows or columns.
65 func (f *File) adjustColDimensions(ws *xlsxWorksheet, col, offset int) {
66         for rowIdx := range ws.SheetData.Row {
67                 for colIdx, v := range ws.SheetData.Row[rowIdx].C {
68                         cellCol, cellRow, _ := CellNameToCoordinates(v.R)
69                         if col <= cellCol {
70                                 if newCol := cellCol + offset; newCol > 0 {
71                                         ws.SheetData.Row[rowIdx].C[colIdx].R, _ = CoordinatesToCellName(newCol, cellRow)
72                                 }
73                         }
74                 }
75         }
76 }
77
78 // adjustRowDimensions provides a function to update row dimensions when
79 // inserting or deleting rows or columns.
80 func (f *File) adjustRowDimensions(ws *xlsxWorksheet, row, offset int) {
81         for i := range ws.SheetData.Row {
82                 r := &ws.SheetData.Row[i]
83                 if newRow := r.R + offset; r.R >= row && newRow > 0 {
84                         f.ajustSingleRowDimensions(r, newRow)
85                 }
86         }
87 }
88
89 // ajustSingleRowDimensions provides a function to ajust single row dimensions.
90 func (f *File) ajustSingleRowDimensions(r *xlsxRow, num int) {
91         r.R = num
92         for i, col := range r.C {
93                 colName, _, _ := SplitCellName(col.R)
94                 r.C[i].R, _ = JoinCellName(colName, num)
95         }
96 }
97
98 // adjustHyperlinks provides a function to update hyperlinks when inserting or
99 // deleting rows or columns.
100 func (f *File) adjustHyperlinks(ws *xlsxWorksheet, sheet string, dir adjustDirection, num, offset int) {
101         // short path
102         if ws.Hyperlinks == nil || len(ws.Hyperlinks.Hyperlink) == 0 {
103                 return
104         }
105
106         // order is important
107         if offset < 0 {
108                 for i := len(ws.Hyperlinks.Hyperlink) - 1; i >= 0; i-- {
109                         linkData := ws.Hyperlinks.Hyperlink[i]
110                         colNum, rowNum, _ := CellNameToCoordinates(linkData.Ref)
111
112                         if (dir == rows && num == rowNum) || (dir == columns && num == colNum) {
113                                 f.deleteSheetRelationships(sheet, linkData.RID)
114                                 if len(ws.Hyperlinks.Hyperlink) > 1 {
115                                         ws.Hyperlinks.Hyperlink = append(ws.Hyperlinks.Hyperlink[:i],
116                                                 ws.Hyperlinks.Hyperlink[i+1:]...)
117                                 } else {
118                                         ws.Hyperlinks = nil
119                                 }
120                         }
121                 }
122         }
123         if ws.Hyperlinks == nil {
124                 return
125         }
126         for i := range ws.Hyperlinks.Hyperlink {
127                 link := &ws.Hyperlinks.Hyperlink[i] // get reference
128                 colNum, rowNum, _ := CellNameToCoordinates(link.Ref)
129                 if dir == rows {
130                         if rowNum >= num {
131                                 link.Ref, _ = CoordinatesToCellName(colNum, rowNum+offset)
132                         }
133                 } else {
134                         if colNum >= num {
135                                 link.Ref, _ = CoordinatesToCellName(colNum+offset, rowNum)
136                         }
137                 }
138         }
139 }
140
141 // adjustAutoFilter provides a function to update the auto filter when
142 // inserting or deleting rows or columns.
143 func (f *File) adjustAutoFilter(ws *xlsxWorksheet, dir adjustDirection, num, offset int) error {
144         if ws.AutoFilter == nil {
145                 return nil
146         }
147
148         coordinates, err := f.areaRefToCoordinates(ws.AutoFilter.Ref)
149         if err != nil {
150                 return err
151         }
152         x1, y1, x2, y2 := coordinates[0], coordinates[1], coordinates[2], coordinates[3]
153
154         if (dir == rows && y1 == num && offset < 0) || (dir == columns && x1 == num && x2 == num) {
155                 ws.AutoFilter = nil
156                 for rowIdx := range ws.SheetData.Row {
157                         rowData := &ws.SheetData.Row[rowIdx]
158                         if rowData.R > y1 && rowData.R <= y2 {
159                                 rowData.Hidden = false
160                         }
161                 }
162                 return nil
163         }
164
165         coordinates = f.adjustAutoFilterHelper(dir, coordinates, num, offset)
166         x1, y1, x2, y2 = coordinates[0], coordinates[1], coordinates[2], coordinates[3]
167
168         if ws.AutoFilter.Ref, err = f.coordinatesToAreaRef([]int{x1, y1, x2, y2}); err != nil {
169                 return err
170         }
171         return nil
172 }
173
174 // adjustAutoFilterHelper provides a function for adjusting auto filter to
175 // compare and calculate cell axis by the given adjust direction, operation
176 // axis and offset.
177 func (f *File) adjustAutoFilterHelper(dir adjustDirection, coordinates []int, num, offset int) []int {
178         if dir == rows {
179                 if coordinates[1] >= num {
180                         coordinates[1] += offset
181                 }
182                 if coordinates[3] >= num {
183                         coordinates[3] += offset
184                 }
185         } else {
186                 if coordinates[2] >= num {
187                         coordinates[2] += offset
188                 }
189         }
190         return coordinates
191 }
192
193 // adjustMergeCells provides a function to update merged cells when inserting
194 // or deleting rows or columns.
195 func (f *File) adjustMergeCells(ws *xlsxWorksheet, dir adjustDirection, num, offset int) error {
196         if ws.MergeCells == nil {
197                 return nil
198         }
199
200         for i := 0; i < len(ws.MergeCells.Cells); i++ {
201                 areaData := ws.MergeCells.Cells[i]
202                 coordinates, err := f.areaRefToCoordinates(areaData.Ref)
203                 if err != nil {
204                         return err
205                 }
206                 x1, y1, x2, y2 := coordinates[0], coordinates[1], coordinates[2], coordinates[3]
207                 if dir == rows {
208                         if y1 == num && y2 == num && offset < 0 {
209                                 f.deleteMergeCell(ws, i)
210                                 i--
211                         }
212                         y1 = f.adjustMergeCellsHelper(y1, num, offset)
213                         y2 = f.adjustMergeCellsHelper(y2, num, offset)
214                 } else {
215                         if x1 == num && x2 == num && offset < 0 {
216                                 f.deleteMergeCell(ws, i)
217                                 i--
218                         }
219                         x1 = f.adjustMergeCellsHelper(x1, num, offset)
220                         x2 = f.adjustMergeCellsHelper(x2, num, offset)
221                 }
222                 if x1 == x2 && y1 == y2 {
223                         f.deleteMergeCell(ws, i)
224                         i--
225                 }
226                 if areaData.Ref, err = f.coordinatesToAreaRef([]int{x1, y1, x2, y2}); err != nil {
227                         return err
228                 }
229         }
230         return nil
231 }
232
233 // adjustMergeCellsHelper provides a function for adjusting merge cells to
234 // compare and calculate cell axis by the given pivot, operation axis and
235 // offset.
236 func (f *File) adjustMergeCellsHelper(pivot, num, offset int) int {
237         if pivot >= num {
238                 pivot += offset
239                 if pivot < 1 {
240                         return 1
241                 }
242                 return pivot
243         }
244         return pivot
245 }
246
247 // deleteMergeCell provides a function to delete merged cell by given index.
248 func (f *File) deleteMergeCell(ws *xlsxWorksheet, idx int) {
249         if len(ws.MergeCells.Cells) > idx {
250                 ws.MergeCells.Cells = append(ws.MergeCells.Cells[:idx], ws.MergeCells.Cells[idx+1:]...)
251                 ws.MergeCells.Count = len(ws.MergeCells.Cells)
252         }
253 }
254
255 // adjustCalcChain provides a function to update the calculation chain when
256 // inserting or deleting rows or columns.
257 func (f *File) adjustCalcChain(dir adjustDirection, num, offset, sheetID int) error {
258         if f.CalcChain == nil {
259                 return nil
260         }
261         for index, c := range f.CalcChain.C {
262                 if c.I != sheetID {
263                         continue
264                 }
265                 colNum, rowNum, err := CellNameToCoordinates(c.R)
266                 if err != nil {
267                         return err
268                 }
269                 if dir == rows && num <= rowNum {
270                         if newRow := rowNum + offset; newRow > 0 {
271                                 f.CalcChain.C[index].R, _ = CoordinatesToCellName(colNum, newRow)
272                         }
273                 }
274                 if dir == columns && num <= colNum {
275                         if newCol := colNum + offset; newCol > 0 {
276                                 f.CalcChain.C[index].R, _ = CoordinatesToCellName(newCol, rowNum)
277                         }
278                 }
279         }
280         return nil
281 }