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

This commit is contained in:
xuri 2020-10-18 00:01:33 +08:00
parent 02530e8c8a
commit 520aa679f3
No known key found for this signature in database
GPG Key ID: BA5E5BB1C948EDF7
8 changed files with 37 additions and 37 deletions

23
col.go
View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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