Fixed #418, #420, #421, init adjust calculation chain support

Update testing case
This commit is contained in:
xuri 2019-06-08 00:00:55 +08:00
parent 3997dee1f5
commit 421f945f51
No known key found for this signature in database
GPG Key ID: BA5E5BB1C948EDF7
6 changed files with 59 additions and 13 deletions

View File

@ -27,8 +27,7 @@ const (
// row: Index number of the row we're inserting/deleting before // row: Index number of the row we're inserting/deleting before
// offset: Number of rows/column to insert/delete negative values indicate deletion // offset: Number of rows/column to insert/delete negative values indicate deletion
// //
// TODO: adjustCalcChain, adjustPageBreaks, adjustComments, // TODO: adjustPageBreaks, adjustComments, adjustDataValidations, adjustProtectedCells
// adjustDataValidations, adjustProtectedCells
// //
func (f *File) adjustHelper(sheet string, dir adjustDirection, num, offset int) error { func (f *File) adjustHelper(sheet string, dir adjustDirection, num, offset int) error {
xlsx, err := f.workSheetReader(sheet) 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 { if err = f.adjustAutoFilter(xlsx, dir, num, offset); err != nil {
return err return err
} }
if err = f.adjustCalcChain(dir, num, offset); err != nil {
return err
}
checkSheet(xlsx) checkSheet(xlsx)
checkRow(xlsx) checkRow(xlsx)
return nil return nil
@ -243,3 +244,28 @@ func (f *File) adjustMergeCells(xlsx *xlsxWorksheet, dir adjustDirection, num, o
} }
return nil 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
}

View File

@ -67,3 +67,19 @@ func TestAdjustHelper(t *testing.T) {
// testing adjustHelper on not exists worksheet. // testing adjustHelper on not exists worksheet.
assert.EqualError(t, f.adjustHelper("SheetN", rows, 0, 0), "sheet SheetN is not exist") 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"))
}

View File

@ -439,6 +439,10 @@ func (f *File) RemoveRow(sheet string, row int) error {
// //
// err := f.InsertRow("Sheet1", 3) // 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 { func (f *File) InsertRow(sheet string, row int) error {
if row < 1 { if row < 1 {
return newInvalidRowNumberError(row) return newInvalidRowNumberError(row)

View File

@ -527,7 +527,7 @@ func (f *File) SetSheetVisible(name string, visible bool) error {
} }
} }
for k, v := range content.Sheets.Sheet { for k, v := range content.Sheets.Sheet {
xlsx, err := f.workSheetReader(f.GetSheetMap()[k]) xlsx, err := f.workSheetReader(v.Name)
if err != nil { if err != nil {
return err return err
} }

View File

@ -1979,8 +1979,8 @@ func (f *File) setFont(formatStyle *formatStyle) *xlsxFont {
formatStyle.Font.Color = "#000000" formatStyle.Font.Color = "#000000"
} }
fnt := xlsxFont{ fnt := xlsxFont{
B: formatStyle.Font.Bold, B: &formatStyle.Font.Bold,
I: formatStyle.Font.Italic, I: &formatStyle.Font.Italic,
Sz: &attrValFloat{Val: formatStyle.Font.Size}, Sz: &attrValFloat{Val: formatStyle.Font.Size},
Color: &xlsxColor{RGB: getPaletteColor(formatStyle.Font.Color)}, Color: &xlsxColor{RGB: getPaletteColor(formatStyle.Font.Color)},
Name: &attrValString{Val: formatStyle.Font.Family}, Name: &attrValString{Val: formatStyle.Font.Family},

View File

@ -88,13 +88,13 @@ type xlsxFont struct {
Name *attrValString `xml:"name"` Name *attrValString `xml:"name"`
Charset *attrValInt `xml:"charset"` Charset *attrValInt `xml:"charset"`
Family *attrValInt `xml:"family"` Family *attrValInt `xml:"family"`
B bool `xml:"b,omitempty"` B *bool `xml:"b,omitempty"`
I bool `xml:"i,omitempty"` I *bool `xml:"i,omitempty"`
Strike bool `xml:"strike,omitempty"` Strike *bool `xml:"strike,omitempty"`
Outline bool `xml:"outline,omitempty"` Outline *bool `xml:"outline,omitempty"`
Shadow bool `xml:"shadow,omitempty"` Shadow *bool `xml:"shadow,omitempty"`
Condense bool `xml:"condense,omitempty"` Condense *bool `xml:"condense,omitempty"`
Extend bool `xml:"extend,omitempty"` Extend *bool `xml:"extend,omitempty"`
Color *xlsxColor `xml:"color"` Color *xlsxColor `xml:"color"`
Sz *attrValFloat `xml:"sz"` Sz *attrValFloat `xml:"sz"`
U *attrValString `xml:"u"` U *attrValString `xml:"u"`