Skip to content

Commit

Permalink
Fixed qax-os#418, qax-os#420, qax-os#421, init adjust calculation cha…
Browse files Browse the repository at this point in the history
…in support

Update testing case
  • Loading branch information
xuri committed Jun 7, 2019
1 parent 3997dee commit 421f945
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 13 deletions.
32 changes: 29 additions & 3 deletions adjust.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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
}
16 changes: 16 additions & 0 deletions adjust_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
}
4 changes: 4 additions & 0 deletions rows.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion sheet.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions styles.go
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down
14 changes: 7 additions & 7 deletions xmlStyles.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down

0 comments on commit 421f945

Please sign in to comment.