Browse Source

Fixed #418, #420, #421, init adjust calculation chain support
Update testing case

xuri 6 years ago
parent
commit
421f945f51
6 changed files with 59 additions and 13 deletions
  1. 29 3
      adjust.go
  2. 16 0
      adjust_test.go
  3. 4 0
      rows.go
  4. 1 1
      sheet.go
  5. 2 2
      styles.go
  6. 7 7
      xmlStyles.go

+ 29 - 3
adjust.go

@@ -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
+}

+ 16 - 0
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"))
+}

+ 4 - 0
rows.go

@@ -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)

+ 1 - 1
sheet.go

@@ -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
 		}

+ 2 - 2
styles.go

@@ -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},

+ 7 - 7
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"`