Skip to content

Commit 520aa67

Browse files
committed
Fix qax-os#706, qax-os#713 improve AddPicture performance, fix missing worksheet when rename with same names
1 parent 02530e8 commit 520aa67

File tree

8 files changed

+37
-37
lines changed

8 files changed

+37
-37
lines changed

col.go

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -559,26 +559,7 @@ func flatCols(col xlsxCol, cols []xlsxCol, replacer func(fc, c xlsxCol) xlsxCol)
559559
// width # Width of object frame.
560560
// height # Height of object frame.
561561
//
562-
// xAbs # Absolute distance to left side of object.
563-
// yAbs # Absolute distance to top side of object.
564-
//
565-
func (f *File) positionObjectPixels(sheet string, col, row, x1, y1, width, height int) (int, int, int, int, int, int, int, int) {
566-
xAbs := 0
567-
yAbs := 0
568-
569-
// Calculate the absolute x offset of the top-left vertex.
570-
for colID := 1; colID <= col; colID++ {
571-
xAbs += f.getColWidth(sheet, colID)
572-
}
573-
xAbs += x1
574-
575-
// Calculate the absolute y offset of the top-left vertex.
576-
// Store the column change to allow optimisations.
577-
for rowID := 1; rowID <= row; rowID++ {
578-
yAbs += f.getRowHeight(sheet, rowID)
579-
}
580-
yAbs += y1
581-
562+
func (f *File) positionObjectPixels(sheet string, col, row, x1, y1, width, height int) (int, int, int, int, int, int) {
582563
// Adjust start column for offsets that are greater than the col width.
583564
for x1 >= f.getColWidth(sheet, col) {
584565
x1 -= f.getColWidth(sheet, col)
@@ -613,7 +594,7 @@ func (f *File) positionObjectPixels(sheet string, col, row, x1, y1, width, heigh
613594
// The end vertices are whatever is left from the width and height.
614595
x2 := width
615596
y2 := height
616-
return col, row, xAbs, yAbs, colEnd, rowEnd, x2, y2
597+
return col, row, colEnd, rowEnd, x2, y2
617598
}
618599

619600
// getColWidth provides a function to get column width in pixels by given

col_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,3 +362,7 @@ func TestRemoveCol(t *testing.T) {
362362

363363
assert.NoError(t, f.SaveAs(filepath.Join("test", "TestRemoveCol.xlsx")))
364364
}
365+
366+
func TestConvertColWidthToPixels(t *testing.T) {
367+
assert.Equal(t, -11.0, convertColWidthToPixels(-1))
368+
}

drawing.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1178,7 +1178,7 @@ func (f *File) addDrawingChart(sheet, drawingXML, cell string, width, height, rI
11781178

11791179
width = int(float64(width) * formatSet.XScale)
11801180
height = int(float64(height) * formatSet.YScale)
1181-
colStart, rowStart, _, _, colEnd, rowEnd, x2, y2 :=
1181+
colStart, rowStart, colEnd, rowEnd, x2, y2 :=
11821182
f.positionObjectPixels(sheet, colIdx, rowIdx, formatSet.OffsetX, formatSet.OffsetY, width, height)
11831183
content, cNvPrID := f.drawingParser(drawingXML)
11841184
twoCellAnchor := xdrCellAnchor{}

excelize_test.go

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -954,17 +954,6 @@ func TestGetSheetComments(t *testing.T) {
954954
assert.Equal(t, "", f.getSheetComments("sheet0"))
955955
}
956956

957-
func TestSetActiveSheet(t *testing.T) {
958-
f := NewFile()
959-
f.WorkBook.BookViews = nil
960-
f.SetActiveSheet(1)
961-
f.WorkBook.BookViews = &xlsxBookViews{WorkBookView: []xlsxWorkBookView{}}
962-
f.Sheet["xl/worksheets/sheet1.xml"].SheetViews = &xlsxSheetViews{SheetView: []xlsxSheetView{}}
963-
f.SetActiveSheet(1)
964-
f.Sheet["xl/worksheets/sheet1.xml"].SheetViews = nil
965-
f.SetActiveSheet(1)
966-
}
967-
968957
func TestSetSheetVisible(t *testing.T) {
969958
f := NewFile()
970959
f.WorkBook.Sheets.Sheet[0].Name = "SheetN"

picture.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ func (f *File) addDrawingPicture(sheet, drawingXML, cell, file string, width, he
259259
}
260260
col--
261261
row--
262-
colStart, rowStart, _, _, colEnd, rowEnd, x2, y2 :=
262+
colStart, rowStart, colEnd, rowEnd, x2, y2 :=
263263
f.positionObjectPixels(sheet, col, row, formatSet.OffsetX, formatSet.OffsetY, width, height)
264264
content, cNvPrID := f.drawingParser(drawingXML)
265265
twoCellAnchor := xdrCellAnchor{}

shape.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ func (f *File) addDrawingShape(sheet, drawingXML, cell string, formatSet *format
324324
width := int(float64(formatSet.Width) * formatSet.Format.XScale)
325325
height := int(float64(formatSet.Height) * formatSet.Format.YScale)
326326

327-
colStart, rowStart, _, _, colEnd, rowEnd, x2, y2 :=
327+
colStart, rowStart, colEnd, rowEnd, x2, y2 :=
328328
f.positionObjectPixels(sheet, colIdx, rowIdx, formatSet.Format.OffsetX, formatSet.Format.OffsetY,
329329
width, height)
330330
content, cNvPrID := f.drawingParser(drawingXML)

sheet.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,9 @@ func (f *File) getActiveSheetID() int {
308308
func (f *File) SetSheetName(oldName, newName string) {
309309
oldName = trimSheetName(oldName)
310310
newName = trimSheetName(newName)
311+
if newName == oldName {
312+
return
313+
}
311314
content := f.workbookReader()
312315
for k, v := range content.Sheets.Sheet {
313316
if v.Name == oldName {

sheet_test.go

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,8 @@ func TestRemovePageBreak(t *testing.T) {
300300
}
301301

302302
func TestGetSheetName(t *testing.T) {
303-
f, _ := OpenFile(filepath.Join("test", "Book1.xlsx"))
303+
f, err := OpenFile(filepath.Join("test", "Book1.xlsx"))
304+
assert.NoError(t, err)
304305
assert.Equal(t, "Sheet1", f.GetSheetName(0))
305306
assert.Equal(t, "Sheet2", f.GetSheetName(1))
306307
assert.Equal(t, "", f.GetSheetName(-1))
@@ -312,10 +313,32 @@ func TestGetSheetMap(t *testing.T) {
312313
1: "Sheet1",
313314
2: "Sheet2",
314315
}
315-
f, _ := OpenFile(filepath.Join("test", "Book1.xlsx"))
316+
f, err := OpenFile(filepath.Join("test", "Book1.xlsx"))
317+
assert.NoError(t, err)
316318
sheetMap := f.GetSheetMap()
317319
for idx, name := range sheetMap {
318320
assert.Equal(t, expectedMap[idx], name)
319321
}
320322
assert.Equal(t, len(sheetMap), 2)
321323
}
324+
325+
func TestSetActiveSheet(t *testing.T) {
326+
f := NewFile()
327+
f.WorkBook.BookViews = nil
328+
f.SetActiveSheet(1)
329+
f.WorkBook.BookViews = &xlsxBookViews{WorkBookView: []xlsxWorkBookView{}}
330+
f.Sheet["xl/worksheets/sheet1.xml"].SheetViews = &xlsxSheetViews{SheetView: []xlsxSheetView{}}
331+
f.SetActiveSheet(1)
332+
f.Sheet["xl/worksheets/sheet1.xml"].SheetViews = nil
333+
f.SetActiveSheet(1)
334+
f = NewFile()
335+
f.SetActiveSheet(-1)
336+
assert.Equal(t, f.GetActiveSheetIndex(), 0)
337+
}
338+
339+
func TestSetSheetName(t *testing.T) {
340+
f := NewFile()
341+
// Test set workksheet with the same name.
342+
f.SetSheetName("Sheet1", "Sheet1")
343+
assert.Equal(t, "Sheet1", f.GetSheetName(0))
344+
}

0 commit comments

Comments
 (0)