Skip to content

Commit

Permalink
Fix #706, #713 improve AddPicture performance, fix missing worksheet …
Browse files Browse the repository at this point in the history
…when rename with same names
  • Loading branch information
xuri committed Oct 17, 2020
1 parent 02530e8 commit 520aa67
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 37 deletions.
23 changes: 2 additions & 21 deletions col.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions col_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
2 changes: 1 addition & 1 deletion drawing.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand Down
11 changes: 0 additions & 11 deletions excelize_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion picture.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand Down
2 changes: 1 addition & 1 deletion shape.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions sheet.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
27 changes: 25 additions & 2 deletions sheet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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))
}

0 comments on commit 520aa67

Please sign in to comment.