diff options
-rw-r--r-- | adjust.go | 32 | ||||
-rw-r--r-- | adjust_test.go | 16 | ||||
-rw-r--r-- | rows.go | 4 | ||||
-rw-r--r-- | sheet.go | 2 | ||||
-rwxr-xr-x | styles.go | 4 | ||||
-rwxr-xr-x | xmlStyles.go | 14 |
6 files changed, 59 insertions, 13 deletions
@@ -27,8 +27,7 @@ const ( // row: Index number of the row we're inserting/deleting before // offset: Number of rows/column to insert/delete negative values indicate deletion // -// TODO: adjustCalcChain, adjustPageBreaks, adjustComments, -// adjustDataValidations, adjustProtectedCells +// TODO: adjustPageBreaks, adjustComments, adjustDataValidations, adjustProtectedCells // func (f *File) adjustHelper(sheet string, dir adjustDirection, num, offset int) error { xlsx, err := f.workSheetReader(sheet) @@ -47,7 +46,9 @@ func (f *File) adjustHelper(sheet string, dir adjustDirection, num, offset int) if err = f.adjustAutoFilter(xlsx, dir, num, offset); err != nil { return err } - + if err = f.adjustCalcChain(dir, num, offset); err != nil { + return err + } checkSheet(xlsx) checkRow(xlsx) return nil @@ -243,3 +244,28 @@ func (f *File) adjustMergeCells(xlsx *xlsxWorksheet, dir adjustDirection, num, o } return nil } + +// adjustCalcChain provides a function to update the calculation chain when +// inserting or deleting rows or columns. +func (f *File) adjustCalcChain(dir adjustDirection, num, offset int) error { + if f.CalcChain == nil { + return nil + } + for index, c := range f.CalcChain.C { + colNum, rowNum, err := CellNameToCoordinates(c.R) + if err != nil { + return err + } + if dir == rows && num <= rowNum { + if newRow := rowNum + offset; newRow > 0 { + f.CalcChain.C[index].R, _ = CoordinatesToCellName(colNum, newRow) + } + } + if dir == columns && num <= colNum { + if newCol := colNum + offset; newCol > 0 { + f.CalcChain.C[index].R, _ = CoordinatesToCellName(newCol, rowNum) + } + } + } + return nil +} diff --git a/adjust_test.go b/adjust_test.go index 7b708ab..28432bc 100644 --- a/adjust_test.go +++ b/adjust_test.go @@ -67,3 +67,19 @@ func TestAdjustHelper(t *testing.T) { // testing adjustHelper on not exists worksheet. assert.EqualError(t, f.adjustHelper("SheetN", rows, 0, 0), "sheet SheetN is not exist") } + +func TestAdjustCalcChain(t *testing.T) { + f := NewFile() + f.CalcChain = &xlsxCalcChain{ + C: []xlsxCalcChainC{ + {R: "B2"}, + }, + } + assert.NoError(t, f.InsertCol("Sheet1", "A")) + assert.NoError(t, f.InsertRow("Sheet1", 1)) + + f.CalcChain.C[0].R = "invalid coordinates" + assert.EqualError(t, f.InsertCol("Sheet1", "A"), `cannot convert cell "invalid coordinates" to coordinates: invalid cell name "invalid coordinates"`) + f.CalcChain = nil + assert.NoError(t, f.InsertCol("Sheet1", "A")) +} @@ -439,6 +439,10 @@ func (f *File) RemoveRow(sheet string, row int) error { // // err := f.InsertRow("Sheet1", 3) // +// Use this method with caution, which will affect changes in references such +// as formulas, charts, and so on. If there is any referenced value of the +// worksheet, it will cause a file error when you open it. The excelize only +// partially updates these references currently. func (f *File) InsertRow(sheet string, row int) error { if row < 1 { return newInvalidRowNumberError(row) @@ -527,7 +527,7 @@ func (f *File) SetSheetVisible(name string, visible bool) error { } } for k, v := range content.Sheets.Sheet { - xlsx, err := f.workSheetReader(f.GetSheetMap()[k]) + xlsx, err := f.workSheetReader(v.Name) if err != nil { return err } @@ -1979,8 +1979,8 @@ func (f *File) setFont(formatStyle *formatStyle) *xlsxFont { formatStyle.Font.Color = "#000000" } fnt := xlsxFont{ - B: formatStyle.Font.Bold, - I: formatStyle.Font.Italic, + B: &formatStyle.Font.Bold, + I: &formatStyle.Font.Italic, Sz: &attrValFloat{Val: formatStyle.Font.Size}, Color: &xlsxColor{RGB: getPaletteColor(formatStyle.Font.Color)}, Name: &attrValString{Val: formatStyle.Font.Family}, diff --git a/xmlStyles.go b/xmlStyles.go index 46587da..49abe3c 100755 --- a/xmlStyles.go +++ b/xmlStyles.go @@ -88,13 +88,13 @@ type xlsxFont struct { Name *attrValString `xml:"name"` Charset *attrValInt `xml:"charset"` Family *attrValInt `xml:"family"` - B bool `xml:"b,omitempty"` - I bool `xml:"i,omitempty"` - Strike bool `xml:"strike,omitempty"` - Outline bool `xml:"outline,omitempty"` - Shadow bool `xml:"shadow,omitempty"` - Condense bool `xml:"condense,omitempty"` - Extend bool `xml:"extend,omitempty"` + B *bool `xml:"b,omitempty"` + I *bool `xml:"i,omitempty"` + Strike *bool `xml:"strike,omitempty"` + Outline *bool `xml:"outline,omitempty"` + Shadow *bool `xml:"shadow,omitempty"` + Condense *bool `xml:"condense,omitempty"` + Extend *bool `xml:"extend,omitempty"` Color *xlsxColor `xml:"color"` Sz *attrValFloat `xml:"sz"` U *attrValString `xml:"u"` |