Skip to content

Commit

Permalink
Update unit test for SetLegacyDrawingHF function and simplify code
Browse files Browse the repository at this point in the history
  • Loading branch information
xuri committed Nov 4, 2024
1 parent 05d4e21 commit e735fdd
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 23 deletions.
27 changes: 13 additions & 14 deletions vml.go
Original file line number Diff line number Diff line change
Expand Up @@ -1097,18 +1097,18 @@ func (f *File) SetLegacyDrawingHF(sheet string, g *HeaderFooterGraphics) error {
Stroke: &xlsxStroke{JoinStyle: "miter"},
VFormulas: &vFormulas{
Formulas: []vFormula{
vFormula{Equation: "if lineDrawn pixelLineWidth 0"},
vFormula{Equation: "sum @0 1 0"},
vFormula{Equation: "sum 0 0 @1"},
vFormula{Equation: "prod @2 1 2"},
vFormula{Equation: "prod @3 21600 pixelWidth"},
vFormula{Equation: "prod @3 21600 pixelHeight"},
vFormula{Equation: "sum @0 0 1"},
vFormula{Equation: "prod @6 1 2"},
vFormula{Equation: "prod @7 21600 pixelWidth"},
vFormula{Equation: "sum @8 21600 0"},
vFormula{Equation: "prod @7 21600 pixelHeight"},
vFormula{Equation: "sum @10 21600 0"},
{Equation: "if lineDrawn pixelLineWidth 0"},
{Equation: "sum @0 1 0"},
{Equation: "sum 0 0 @1"},
{Equation: "prod @2 1 2"},
{Equation: "prod @3 21600 pixelWidth"},
{Equation: "prod @3 21600 pixelHeight"},
{Equation: "sum @0 0 1"},
{Equation: "prod @6 1 2"},
{Equation: "prod @7 21600 pixelWidth"},
{Equation: "sum @8 21600 0"},
{Equation: "prod @7 21600 pixelHeight"},
{Equation: "sum @10 21600 0"},
},
},
VPath: &vPath{ExtrusionOK: "f", GradientShapeOK: "t", ConnectType: "rect"},
Expand Down Expand Up @@ -1144,8 +1144,7 @@ func (f *File) SetLegacyDrawingHF(sheet string, g *HeaderFooterGraphics) error {
drawingID := f.addRels(sheetRels, SourceRelationshipDrawingVML, sheetRelationshipsDrawingVML, "")
f.addSheetNameSpace(sheet, SourceRelationship)
f.addSheetLegacyDrawingHF(sheet, drawingID)
err := f.setContentTypePartImageExtensions()
if err != nil {
if err := f.setContentTypePartImageExtensions(); err != nil {
return err
}
return f.setContentTypePartVMLExtensions()
Expand Down
30 changes: 21 additions & 9 deletions vml_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -413,22 +413,34 @@ func TestExtractFormControl(t *testing.T) {
assert.EqualError(t, err, "XML syntax error on line 1: invalid UTF-8")
}

func ExampleFile_SetLegacyDrawingHF() {
func TestSetLegacyDrawingHF(t *testing.T) {
f := NewFile()
sheet := "Sheet1"
headerFooterOptions := HeaderFooterOptions{
OddHeader: "&LExcelize&R&G",
}
f.SetHeaderFooter(sheet, &headerFooterOptions)
file, _ := os.ReadFile("test/images/excel.png")
f.SetLegacyDrawingHF(sheet, &HeaderFooterGraphics{
assert.NoError(t, f.SetHeaderFooter(sheet, &headerFooterOptions))
file, err := os.ReadFile(filepath.Join("test", "images", "excel.png"))
assert.NoError(t, err)
assert.NoError(t, f.SetLegacyDrawingHF(sheet, &HeaderFooterGraphics{
Extension: ".png",
File: file,
Width: "50pt",
Height: "32pt",
}))
assert.NoError(t, f.SetCellValue(sheet, "A1", "Example"))
assert.NoError(t, f.SaveAs(filepath.Join("test", "TestSetLegacyDrawingHF.xlsx")))
assert.NoError(t, f.Close())

// Test set legacy drawing header/footer with unsupported charset content types
f = NewFile()
f.ContentTypes = nil
f.Pkg.Store(defaultXMLPathContentTypes, MacintoshCyrillicCharset)
assert.EqualError(t, f.SetLegacyDrawingHF(sheet, &HeaderFooterGraphics{
Extension: ".png",
File: file,
Width: "50pt",
Height: "32pt",
})
f.SetCellValue(sheet, "A1", "Example")
out, _ := os.CreateTemp("", "header-graphics-*.xlsx")
f.Write(out)
f.Close()
}), "XML syntax error on line 1: invalid UTF-8")
assert.NoError(t, f.Close())
}

0 comments on commit e735fdd

Please sign in to comment.