From 520aa679f34bafbc00626151075b0b123eceb516 Mon Sep 17 00:00:00 2001 From: xuri Date: Sun, 18 Oct 2020 00:01:33 +0800 Subject: Fix #706, #713 improve AddPicture performance, fix missing worksheet when rename with same names --- col.go | 23 ++--------------------- col_test.go | 4 ++++ drawing.go | 2 +- excelize_test.go | 11 ----------- picture.go | 2 +- shape.go | 2 +- sheet.go | 3 +++ sheet_test.go | 27 +++++++++++++++++++++++++-- 8 files changed, 37 insertions(+), 37 deletions(-) diff --git a/col.go b/col.go index 19ce99b..f7a77da 100644 --- a/col.go +++ b/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 diff --git a/col_test.go b/col_test.go index 02c5ca2..532f428 100644 --- a/col_test.go +++ b/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)) +} diff --git a/drawing.go b/drawing.go index 666b23d..96403b3 100644 --- a/drawing.go +++ b/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{} diff --git a/excelize_test.go b/excelize_test.go index 890bcf6..9c1b1e6 100644 --- a/excelize_test.go +++ b/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" diff --git a/picture.go b/picture.go index 9a64637..2f685ff 100644 --- a/picture.go +++ b/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{} diff --git a/shape.go b/shape.go index 0a5164b..2600e90 100644 --- a/shape.go +++ b/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) diff --git a/sheet.go b/sheet.go index a44e391..aaa72cc 100644 --- a/sheet.go +++ b/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 { diff --git a/sheet_test.go b/sheet_test.go index 890a4e5..56f5f46 100644 --- a/sheet_test.go +++ b/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)) +} -- cgit v1.2.1