Browse Source

Fix #706, #713 improve AddPicture performance, fix missing worksheet when rename with same names

xuri 5 years ago
parent
commit
520aa679f3
8 changed files with 37 additions and 37 deletions
  1. 2 21
      col.go
  2. 4 0
      col_test.go
  3. 1 1
      drawing.go
  4. 0 11
      excelize_test.go
  5. 1 1
      picture.go
  6. 1 1
      shape.go
  7. 3 0
      sheet.go
  8. 25 2
      sheet_test.go

+ 2 - 21
col.go

@@ -559,26 +559,7 @@ func flatCols(col xlsxCol, cols []xlsxCol, replacer func(fc, c xlsxCol) xlsxCol)
 //    width           # Width of object frame.
 //    height          # Height of object frame.
 //
-//    xAbs            # Absolute distance to left side of object.
-//    yAbs            # Absolute distance to top side of object.
-//
-func (f *File) positionObjectPixels(sheet string, col, row, x1, y1, width, height int) (int, int, int, int, int, int, int, int) {
-	xAbs := 0
-	yAbs := 0
-
-	// Calculate the absolute x offset of the top-left vertex.
-	for colID := 1; colID <= col; colID++ {
-		xAbs += f.getColWidth(sheet, colID)
-	}
-	xAbs += x1
-
-	// Calculate the absolute y offset of the top-left vertex.
-	// Store the column change to allow optimisations.
-	for rowID := 1; rowID <= row; rowID++ {
-		yAbs += f.getRowHeight(sheet, rowID)
-	}
-	yAbs += y1
-
+func (f *File) positionObjectPixels(sheet string, col, row, x1, y1, width, height int) (int, int, int, int, int, int) {
 	// Adjust start column for offsets that are greater than the col width.
 	for x1 >= f.getColWidth(sheet, col) {
 		x1 -= f.getColWidth(sheet, col)
@@ -613,7 +594,7 @@ func (f *File) positionObjectPixels(sheet string, col, row, x1, y1, width, heigh
 	// The end vertices are whatever is left from the width and height.
 	x2 := width
 	y2 := height
-	return col, row, xAbs, yAbs, colEnd, rowEnd, x2, y2
+	return col, row, colEnd, rowEnd, x2, y2
 }
 
 // getColWidth provides a function to get column width in pixels by given

+ 4 - 0
col_test.go

@@ -362,3 +362,7 @@ func TestRemoveCol(t *testing.T) {
 
 	assert.NoError(t, f.SaveAs(filepath.Join("test", "TestRemoveCol.xlsx")))
 }
+
+func TestConvertColWidthToPixels(t *testing.T) {
+	assert.Equal(t, -11.0, convertColWidthToPixels(-1))
+}

+ 1 - 1
drawing.go

@@ -1178,7 +1178,7 @@ func (f *File) addDrawingChart(sheet, drawingXML, cell string, width, height, rI
 
 	width = int(float64(width) * formatSet.XScale)
 	height = int(float64(height) * formatSet.YScale)
-	colStart, rowStart, _, _, colEnd, rowEnd, x2, y2 :=
+	colStart, rowStart, colEnd, rowEnd, x2, y2 :=
 		f.positionObjectPixels(sheet, colIdx, rowIdx, formatSet.OffsetX, formatSet.OffsetY, width, height)
 	content, cNvPrID := f.drawingParser(drawingXML)
 	twoCellAnchor := xdrCellAnchor{}

+ 0 - 11
excelize_test.go

@@ -954,17 +954,6 @@ func TestGetSheetComments(t *testing.T) {
 	assert.Equal(t, "", f.getSheetComments("sheet0"))
 }
 
-func TestSetActiveSheet(t *testing.T) {
-	f := NewFile()
-	f.WorkBook.BookViews = nil
-	f.SetActiveSheet(1)
-	f.WorkBook.BookViews = &xlsxBookViews{WorkBookView: []xlsxWorkBookView{}}
-	f.Sheet["xl/worksheets/sheet1.xml"].SheetViews = &xlsxSheetViews{SheetView: []xlsxSheetView{}}
-	f.SetActiveSheet(1)
-	f.Sheet["xl/worksheets/sheet1.xml"].SheetViews = nil
-	f.SetActiveSheet(1)
-}
-
 func TestSetSheetVisible(t *testing.T) {
 	f := NewFile()
 	f.WorkBook.Sheets.Sheet[0].Name = "SheetN"

+ 1 - 1
picture.go

@@ -259,7 +259,7 @@ func (f *File) addDrawingPicture(sheet, drawingXML, cell, file string, width, he
 	}
 	col--
 	row--
-	colStart, rowStart, _, _, colEnd, rowEnd, x2, y2 :=
+	colStart, rowStart, colEnd, rowEnd, x2, y2 :=
 		f.positionObjectPixels(sheet, col, row, formatSet.OffsetX, formatSet.OffsetY, width, height)
 	content, cNvPrID := f.drawingParser(drawingXML)
 	twoCellAnchor := xdrCellAnchor{}

+ 1 - 1
shape.go

@@ -324,7 +324,7 @@ func (f *File) addDrawingShape(sheet, drawingXML, cell string, formatSet *format
 	width := int(float64(formatSet.Width) * formatSet.Format.XScale)
 	height := int(float64(formatSet.Height) * formatSet.Format.YScale)
 
-	colStart, rowStart, _, _, colEnd, rowEnd, x2, y2 :=
+	colStart, rowStart, colEnd, rowEnd, x2, y2 :=
 		f.positionObjectPixels(sheet, colIdx, rowIdx, formatSet.Format.OffsetX, formatSet.Format.OffsetY,
 			width, height)
 	content, cNvPrID := f.drawingParser(drawingXML)

+ 3 - 0
sheet.go

@@ -308,6 +308,9 @@ func (f *File) getActiveSheetID() int {
 func (f *File) SetSheetName(oldName, newName string) {
 	oldName = trimSheetName(oldName)
 	newName = trimSheetName(newName)
+	if newName == oldName {
+		return
+	}
 	content := f.workbookReader()
 	for k, v := range content.Sheets.Sheet {
 		if v.Name == oldName {

+ 25 - 2
sheet_test.go

@@ -300,7 +300,8 @@ func TestRemovePageBreak(t *testing.T) {
 }
 
 func TestGetSheetName(t *testing.T) {
-	f, _ := OpenFile(filepath.Join("test", "Book1.xlsx"))
+	f, err := OpenFile(filepath.Join("test", "Book1.xlsx"))
+	assert.NoError(t, err)
 	assert.Equal(t, "Sheet1", f.GetSheetName(0))
 	assert.Equal(t, "Sheet2", f.GetSheetName(1))
 	assert.Equal(t, "", f.GetSheetName(-1))
@@ -312,10 +313,32 @@ func TestGetSheetMap(t *testing.T) {
 		1: "Sheet1",
 		2: "Sheet2",
 	}
-	f, _ := OpenFile(filepath.Join("test", "Book1.xlsx"))
+	f, err := OpenFile(filepath.Join("test", "Book1.xlsx"))
+	assert.NoError(t, err)
 	sheetMap := f.GetSheetMap()
 	for idx, name := range sheetMap {
 		assert.Equal(t, expectedMap[idx], name)
 	}
 	assert.Equal(t, len(sheetMap), 2)
 }
+
+func TestSetActiveSheet(t *testing.T) {
+	f := NewFile()
+	f.WorkBook.BookViews = nil
+	f.SetActiveSheet(1)
+	f.WorkBook.BookViews = &xlsxBookViews{WorkBookView: []xlsxWorkBookView{}}
+	f.Sheet["xl/worksheets/sheet1.xml"].SheetViews = &xlsxSheetViews{SheetView: []xlsxSheetView{}}
+	f.SetActiveSheet(1)
+	f.Sheet["xl/worksheets/sheet1.xml"].SheetViews = nil
+	f.SetActiveSheet(1)
+	f = NewFile()
+	f.SetActiveSheet(-1)
+	assert.Equal(t, f.GetActiveSheetIndex(), 0)
+}
+
+func TestSetSheetName(t *testing.T) {
+	f := NewFile()
+	// Test set workksheet with the same name.
+	f.SetSheetName("Sheet1", "Sheet1")
+	assert.Equal(t, "Sheet1", f.GetSheetName(0))
+}