7 type adjustDirection bool
10 columns adjustDirection = false
11 rows adjustDirection = true
14 // adjustHelper provides a function to adjust rows and columns dimensions,
15 // hyperlinks, merged cells and auto filter when inserting or deleting rows or
18 // sheet: Worksheet name that we're editing
19 // column: Index number of the column we're inserting/deleting before
20 // row: Index number of the row we're inserting/deleting before
21 // offset: Number of rows/column to insert/delete negative values indicate deletion
23 // TODO: adjustCalcChain, adjustPageBreaks, adjustComments,
24 // adjustDataValidations, adjustProtectedCells
26 func (f *File) adjustHelper(sheet string, dir adjustDirection, num, offset int) {
27 xlsx := f.workSheetReader(sheet)
30 f.adjustRowDimensions(xlsx, num, offset)
32 f.adjustColDimensions(xlsx, num, offset)
34 f.adjustHyperlinks(xlsx, sheet, dir, num, offset)
35 f.adjustMergeCells(xlsx, dir, num, offset)
36 f.adjustAutoFilter(xlsx, dir, num, offset)
42 // adjustColDimensions provides a function to update column dimensions when
43 // inserting or deleting rows or columns.
44 func (f *File) adjustColDimensions(xlsx *xlsxWorksheet, col, offset int) {
45 for rowIdx := range xlsx.SheetData.Row {
46 for colIdx, v := range xlsx.SheetData.Row[rowIdx].C {
47 cellCol, cellRow, _ := CellNameToCoordinates(v.R)
49 if newCol := cellCol + offset; newCol > 0 {
50 xlsx.SheetData.Row[rowIdx].C[colIdx].R, _ = CoordinatesToCellName(newCol, cellRow)
57 // adjustRowDimensions provides a function to update row dimensions when
58 // inserting or deleting rows or columns.
59 func (f *File) adjustRowDimensions(xlsx *xlsxWorksheet, row, offset int) {
60 for i, r := range xlsx.SheetData.Row {
61 if newRow := r.R + offset; r.R >= row && newRow > 0 {
62 f.ajustSingleRowDimensions(&xlsx.SheetData.Row[i], newRow)
67 // ajustSingleRowDimensions provides a function to ajust single row dimensions.
68 func (f *File) ajustSingleRowDimensions(r *xlsxRow, num int) {
70 for i, col := range r.C {
71 colName, _, _ := SplitCellName(col.R)
72 r.C[i].R, _ = JoinCellName(colName, num)
76 // adjustHyperlinks provides a function to update hyperlinks when inserting or
77 // deleting rows or columns.
78 func (f *File) adjustHyperlinks(xlsx *xlsxWorksheet, sheet string, dir adjustDirection, num, offset int) {
80 if xlsx.Hyperlinks == nil || len(xlsx.Hyperlinks.Hyperlink) == 0 {
86 for rowIdx, linkData := range xlsx.Hyperlinks.Hyperlink {
87 colNum, rowNum, _ := CellNameToCoordinates(linkData.Ref)
89 if (dir == rows && num == rowNum) || (dir == columns && num == colNum) {
90 f.deleteSheetRelationships(sheet, linkData.RID)
91 if len(xlsx.Hyperlinks.Hyperlink) > 1 {
92 xlsx.Hyperlinks.Hyperlink = append(xlsx.Hyperlinks.Hyperlink[:rowIdx],
93 xlsx.Hyperlinks.Hyperlink[rowIdx+1:]...)
101 if xlsx.Hyperlinks == nil {
105 for i := range xlsx.Hyperlinks.Hyperlink {
106 link := &xlsx.Hyperlinks.Hyperlink[i] // get reference
107 colNum, rowNum, _ := CellNameToCoordinates(link.Ref)
111 link.Ref, _ = CoordinatesToCellName(colNum, rowNum+offset)
115 link.Ref, _ = CoordinatesToCellName(colNum+offset, rowNum)
121 // adjustAutoFilter provides a function to update the auto filter when
122 // inserting or deleting rows or columns.
123 func (f *File) adjustAutoFilter(xlsx *xlsxWorksheet, dir adjustDirection, num, offset int) {
124 if xlsx.AutoFilter == nil {
128 rng := strings.Split(xlsx.AutoFilter.Ref, ":")
132 firstCol, firstRow, err := CellNameToCoordinates(firstCell)
137 lastCol, lastRow, err := CellNameToCoordinates(lastCell)
142 if (dir == rows && firstRow == num && offset < 0) || (dir == columns && firstCol == num && lastCol == num) {
143 xlsx.AutoFilter = nil
144 for rowIdx := range xlsx.SheetData.Row {
145 rowData := &xlsx.SheetData.Row[rowIdx]
146 if rowData.R > firstRow && rowData.R <= lastRow {
147 rowData.Hidden = false
155 firstCell, _ = CoordinatesToCellName(firstCol, firstRow+offset)
158 lastCell, _ = CoordinatesToCellName(lastCol, lastRow+offset)
162 lastCell, _ = CoordinatesToCellName(lastCol+offset, lastRow)
166 xlsx.AutoFilter.Ref = firstCell + ":" + lastCell
169 // adjustMergeCells provides a function to update merged cells when inserting
170 // or deleting rows or columns.
171 func (f *File) adjustMergeCells(xlsx *xlsxWorksheet, dir adjustDirection, num, offset int) {
172 if xlsx.MergeCells == nil {
176 for i, areaData := range xlsx.MergeCells.Cells {
177 rng := strings.Split(areaData.Ref, ":")
181 firstCol, firstRow, err := CellNameToCoordinates(firstCell)
186 lastCol, lastRow, err := CellNameToCoordinates(lastCell)
191 adjust := func(v int) int {
203 firstRow = adjust(firstRow)
204 lastRow = adjust(lastRow)
206 firstCol = adjust(firstCol)
207 lastCol = adjust(lastCol)
210 if firstCol == lastCol && firstRow == lastRow {
211 if len(xlsx.MergeCells.Cells) > 1 {
212 xlsx.MergeCells.Cells = append(xlsx.MergeCells.Cells[:i], xlsx.MergeCells.Cells[i+1:]...)
213 xlsx.MergeCells.Count = len(xlsx.MergeCells.Cells)
215 xlsx.MergeCells = nil
219 if firstCell, err = CoordinatesToCellName(firstCol, firstRow); err != nil {
223 if lastCell, err = CoordinatesToCellName(lastCol, lastRow); err != nil {
227 areaData.Ref = firstCell + ":" + lastCell