forked from p30928647/excelize
256 lines
10 KiB
Go
256 lines
10 KiB
Go
// Copyright 2016 - 2023 The excelize Authors. All rights reserved. Use of
|
|
// this source code is governed by a BSD-style license that can be found in
|
|
// the LICENSE file.
|
|
//
|
|
// Package excelize providing a set of functions that allow you to write to and
|
|
// read from XLAM / XLSM / XLSX / XLTM / XLTX files. Supports reading and
|
|
// writing spreadsheet documents generated by Microsoft Excel™ 2007 and later.
|
|
// Supports complex components by high compatibility, and provided streaming
|
|
// API for generating or reading data from a worksheet with huge amounts of
|
|
// data. This library needs Go version 1.16 or later.
|
|
|
|
package excelize
|
|
|
|
import (
|
|
"encoding/xml"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestAddComment(t *testing.T) {
|
|
f, err := prepareTestBook1()
|
|
if !assert.NoError(t, err) {
|
|
t.FailNow()
|
|
}
|
|
|
|
s := strings.Repeat("c", TotalCellChars+1)
|
|
assert.NoError(t, f.AddComment("Sheet1", Comment{Cell: "A30", Author: s, Text: s, Paragraph: []RichTextRun{{Text: s}, {Text: s}}}))
|
|
assert.NoError(t, f.AddComment("Sheet2", Comment{Cell: "B7", Author: "Excelize", Text: s[:TotalCellChars-1], Paragraph: []RichTextRun{{Text: "Excelize: ", Font: &Font{Bold: true}}, {Text: "This is a comment."}}}))
|
|
|
|
// Test add comment on not exists worksheet
|
|
assert.EqualError(t, f.AddComment("SheetN", Comment{Cell: "B7", Author: "Excelize", Paragraph: []RichTextRun{{Text: "Excelize: ", Font: &Font{Bold: true}}, {Text: "This is a comment."}}}), "sheet SheetN does not exist")
|
|
// Test add comment on with illegal cell reference
|
|
assert.EqualError(t, f.AddComment("Sheet1", Comment{Cell: "A", Author: "Excelize", Paragraph: []RichTextRun{{Text: "Excelize: ", Font: &Font{Bold: true}}, {Text: "This is a comment."}}}), newCellNameToCoordinatesError("A", newInvalidCellNameError("A")).Error())
|
|
comments, err := f.GetComments("Sheet1")
|
|
assert.NoError(t, err)
|
|
assert.Len(t, comments, 2)
|
|
comments, err = f.GetComments("Sheet2")
|
|
assert.NoError(t, err)
|
|
assert.Len(t, comments, 1)
|
|
assert.NoError(t, f.SaveAs(filepath.Join("test", "TestAddComments.xlsx")))
|
|
|
|
f.Comments["xl/comments2.xml"] = nil
|
|
f.Pkg.Store("xl/comments2.xml", []byte(xml.Header+`<comments xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"><authors><author>Excelize: </author></authors><commentList><comment ref="B7" authorId="0"><text><t>Excelize: </t></text></comment></commentList></comments>`))
|
|
comments, err = f.GetComments("Sheet1")
|
|
assert.NoError(t, err)
|
|
assert.Len(t, comments, 2)
|
|
comments, err = f.GetComments("Sheet2")
|
|
assert.NoError(t, err)
|
|
assert.Len(t, comments, 1)
|
|
comments, err = NewFile().GetComments("Sheet1")
|
|
assert.NoError(t, err)
|
|
assert.Len(t, comments, 0)
|
|
|
|
// Test add comments with invalid sheet name
|
|
assert.EqualError(t, f.AddComment("Sheet:1", Comment{Cell: "A1", Author: "Excelize", Text: "This is a comment."}), ErrSheetNameInvalid.Error())
|
|
|
|
// Test add comments with unsupported charset
|
|
f.Comments["xl/comments2.xml"] = nil
|
|
f.Pkg.Store("xl/comments2.xml", MacintoshCyrillicCharset)
|
|
_, err = f.GetComments("Sheet2")
|
|
assert.EqualError(t, err, "XML syntax error on line 1: invalid UTF-8")
|
|
|
|
// Test add comments with unsupported charset
|
|
f.Comments["xl/comments2.xml"] = nil
|
|
f.Pkg.Store("xl/comments2.xml", MacintoshCyrillicCharset)
|
|
assert.EqualError(t, f.AddComment("Sheet2", Comment{Cell: "A30", Text: "Comment"}), "XML syntax error on line 1: invalid UTF-8")
|
|
|
|
// Test add comments with unsupported charset style sheet
|
|
f.Styles = nil
|
|
f.Pkg.Store(defaultXMLPathStyles, MacintoshCyrillicCharset)
|
|
assert.EqualError(t, f.AddComment("Sheet2", Comment{Cell: "A30", Text: "Comment"}), "XML syntax error on line 1: invalid UTF-8")
|
|
|
|
// Test get comments on not exists worksheet
|
|
comments, err = f.GetComments("SheetN")
|
|
assert.Len(t, comments, 0)
|
|
assert.EqualError(t, err, "sheet SheetN does not exist")
|
|
}
|
|
|
|
func TestDeleteComment(t *testing.T) {
|
|
f, err := prepareTestBook1()
|
|
if !assert.NoError(t, err) {
|
|
t.FailNow()
|
|
}
|
|
|
|
assert.NoError(t, f.AddComment("Sheet2", Comment{Cell: "A40", Text: "Excelize: This is a comment1."}))
|
|
assert.NoError(t, f.AddComment("Sheet2", Comment{Cell: "A41", Paragraph: []RichTextRun{{Text: "Excelize: ", Font: &Font{Bold: true}}, {Text: "This is a comment2."}}}))
|
|
assert.NoError(t, f.AddComment("Sheet2", Comment{Cell: "C41", Paragraph: []RichTextRun{{Text: "Excelize: ", Font: &Font{Bold: true}}, {Text: "This is a comment3."}}}))
|
|
assert.NoError(t, f.AddComment("Sheet2", Comment{Cell: "C41", Paragraph: []RichTextRun{{Text: "Excelize: ", Font: &Font{Bold: true}}, {Text: "This is a comment3-1."}}}))
|
|
assert.NoError(t, f.AddComment("Sheet2", Comment{Cell: "C42", Paragraph: []RichTextRun{{Text: "Excelize: ", Font: &Font{Bold: true}}, {Text: "This is a comment4."}}}))
|
|
assert.NoError(t, f.AddComment("Sheet2", Comment{Cell: "C41", Paragraph: []RichTextRun{{Text: "Excelize: ", Font: &Font{Bold: true}}, {Text: "This is a comment2."}}}))
|
|
|
|
assert.NoError(t, f.DeleteComment("Sheet2", "A40"))
|
|
|
|
comments, err := f.GetComments("Sheet2")
|
|
assert.NoError(t, err)
|
|
assert.Len(t, comments, 5)
|
|
|
|
comments, err = NewFile().GetComments("Sheet1")
|
|
assert.NoError(t, err)
|
|
assert.Len(t, comments, 0)
|
|
|
|
// Test delete comment with invalid sheet name
|
|
assert.EqualError(t, f.DeleteComment("Sheet:1", "A1"), ErrSheetNameInvalid.Error())
|
|
// Test delete all comments in a worksheet
|
|
assert.NoError(t, f.DeleteComment("Sheet2", "A41"))
|
|
assert.NoError(t, f.DeleteComment("Sheet2", "C41"))
|
|
assert.NoError(t, f.DeleteComment("Sheet2", "C42"))
|
|
comments, err = f.GetComments("Sheet2")
|
|
assert.NoError(t, err)
|
|
assert.EqualValues(t, 0, len(comments))
|
|
// Test delete comment on not exists worksheet
|
|
assert.EqualError(t, f.DeleteComment("SheetN", "A1"), "sheet SheetN does not exist")
|
|
// Test delete comment with worksheet part
|
|
f.Pkg.Delete("xl/worksheets/sheet1.xml")
|
|
assert.NoError(t, f.DeleteComment("Sheet1", "A22"))
|
|
|
|
f.Comments["xl/comments2.xml"] = nil
|
|
f.Pkg.Store("xl/comments2.xml", MacintoshCyrillicCharset)
|
|
assert.EqualError(t, f.DeleteComment("Sheet2", "A41"), "XML syntax error on line 1: invalid UTF-8")
|
|
}
|
|
|
|
func TestDecodeVMLDrawingReader(t *testing.T) {
|
|
f := NewFile()
|
|
path := "xl/drawings/vmlDrawing1.xml"
|
|
f.Pkg.Store(path, MacintoshCyrillicCharset)
|
|
_, err := f.decodeVMLDrawingReader(path)
|
|
assert.EqualError(t, err, "XML syntax error on line 1: invalid UTF-8")
|
|
}
|
|
|
|
func TestCommentsReader(t *testing.T) {
|
|
f := NewFile()
|
|
// Test read comments with unsupported charset
|
|
path := "xl/comments1.xml"
|
|
f.Pkg.Store(path, MacintoshCyrillicCharset)
|
|
_, err := f.commentsReader(path)
|
|
assert.EqualError(t, err, "XML syntax error on line 1: invalid UTF-8")
|
|
}
|
|
|
|
func TestCountComments(t *testing.T) {
|
|
f := NewFile()
|
|
f.Comments["xl/comments1.xml"] = nil
|
|
assert.Equal(t, f.countComments(), 1)
|
|
}
|
|
|
|
func TestAddDrawingVML(t *testing.T) {
|
|
// Test addDrawingVML with illegal cell reference
|
|
f := NewFile()
|
|
assert.EqualError(t, f.addDrawingVML(0, "", &vmlOptions{Cell: "*"}), newCellNameToCoordinatesError("*", newInvalidCellNameError("*")).Error())
|
|
|
|
f.Pkg.Store("xl/drawings/vmlDrawing1.vml", MacintoshCyrillicCharset)
|
|
assert.EqualError(t, f.addDrawingVML(0, "xl/drawings/vmlDrawing1.vml", &vmlOptions{Cell: "A1"}), "XML syntax error on line 1: invalid UTF-8")
|
|
}
|
|
|
|
func TestFormControl(t *testing.T) {
|
|
f := NewFile()
|
|
assert.NoError(t, f.AddFormControl("Sheet1", FormControl{
|
|
Cell: "D1",
|
|
Type: FormControlButton,
|
|
Macro: "Button1_Click",
|
|
}))
|
|
assert.NoError(t, f.AddFormControl("Sheet1", FormControl{
|
|
Cell: "A1",
|
|
Type: FormControlButton,
|
|
Macro: "Button1_Click",
|
|
Width: 140,
|
|
Height: 60,
|
|
Text: "Button 1\r\n",
|
|
Paragraph: []RichTextRun{
|
|
{
|
|
Font: &Font{
|
|
Bold: true,
|
|
Italic: true,
|
|
Underline: "single",
|
|
Family: "Times New Roman",
|
|
Size: 14,
|
|
Color: "777777",
|
|
},
|
|
Text: "C1=A1+B1",
|
|
},
|
|
},
|
|
}))
|
|
assert.NoError(t, f.AddFormControl("Sheet1", FormControl{
|
|
Cell: "A5",
|
|
Type: FormControlCheckbox,
|
|
Text: "Check Box 1",
|
|
Checked: true,
|
|
}))
|
|
assert.NoError(t, f.AddFormControl("Sheet1", FormControl{
|
|
Cell: "A6",
|
|
Type: FormControlCheckbox,
|
|
Text: "Check Box 2",
|
|
}))
|
|
assert.NoError(t, f.AddFormControl("Sheet1", FormControl{
|
|
Cell: "A7",
|
|
Type: FormControlRadio,
|
|
Text: "Option Button 1",
|
|
Checked: true,
|
|
}))
|
|
assert.NoError(t, f.AddFormControl("Sheet1", FormControl{
|
|
Cell: "A8",
|
|
Type: FormControlRadio,
|
|
Text: "Option Button 2",
|
|
}))
|
|
assert.NoError(t, f.SetSheetProps("Sheet1", &SheetPropsOptions{CodeName: stringPtr("Sheet1")}))
|
|
file, err := os.ReadFile(filepath.Join("test", "vbaProject.bin"))
|
|
assert.NoError(t, err)
|
|
assert.NoError(t, f.AddVBAProject(file))
|
|
assert.NoError(t, f.SaveAs(filepath.Join("test", "TestAddFormControl.xlsm")))
|
|
assert.NoError(t, f.Close())
|
|
f, err = OpenFile(filepath.Join("test", "TestAddFormControl.xlsm"))
|
|
assert.NoError(t, err)
|
|
assert.NoError(t, f.AddFormControl("Sheet1", FormControl{
|
|
Cell: "D4",
|
|
Type: FormControlButton,
|
|
Macro: "Button1_Click",
|
|
Text: "Button 2",
|
|
}))
|
|
// Test add unsupported form control
|
|
assert.Equal(t, f.AddFormControl("Sheet1", FormControl{
|
|
Cell: "A1",
|
|
Type: 0x37,
|
|
Macro: "Button1_Click",
|
|
}), ErrParameterInvalid)
|
|
// Test add form control on not exists worksheet
|
|
assert.Equal(t, f.AddFormControl("SheetN", FormControl{
|
|
Cell: "A1",
|
|
Type: FormControlButton,
|
|
Macro: "Button1_Click",
|
|
}), newNoExistSheetError("SheetN"))
|
|
assert.NoError(t, f.Close())
|
|
// Test delete form control
|
|
f, err = OpenFile(filepath.Join("test", "TestAddFormControl.xlsm"))
|
|
assert.NoError(t, err)
|
|
assert.NoError(t, f.DeleteFormControl("Sheet1", "D1"))
|
|
assert.NoError(t, f.DeleteFormControl("Sheet1", "A1"))
|
|
// Test delete form control on not exists worksheet
|
|
assert.Equal(t, f.DeleteFormControl("SheetN", "A1"), newNoExistSheetError("SheetN"))
|
|
// Test delete form control on not exists worksheet
|
|
assert.Equal(t, f.DeleteFormControl("Sheet1", "A"), newCellNameToCoordinatesError("A", newInvalidCellNameError("A")))
|
|
assert.NoError(t, f.SaveAs(filepath.Join("test", "TestDeleteFormControl.xlsm")))
|
|
assert.NoError(t, f.Close())
|
|
// Test delete form control with expected element
|
|
f, err = OpenFile(filepath.Join("test", "TestAddFormControl.xlsm"))
|
|
assert.NoError(t, err)
|
|
f.Pkg.Store("xl/drawings/vmlDrawing1.vml", MacintoshCyrillicCharset)
|
|
assert.Error(t, f.DeleteFormControl("Sheet1", "A1"), "XML syntax error on line 1: invalid UTF-8")
|
|
assert.NoError(t, f.Close())
|
|
// Test delete form control on a worksheet without form control
|
|
f = NewFile()
|
|
assert.NoError(t, f.DeleteFormControl("Sheet1", "A1"))
|
|
}
|