98 lines
4.0 KiB
Go
98 lines
4.0 KiB
Go
// Copyright 2016 - 2022 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.15 or later.
|
|
|
|
package excelize
|
|
|
|
import (
|
|
"encoding/xml"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestAddComments(t *testing.T) {
|
|
f, err := prepareTestBook1()
|
|
if !assert.NoError(t, err) {
|
|
t.FailNow()
|
|
}
|
|
|
|
s := strings.Repeat("c", 32768)
|
|
assert.NoError(t, f.AddComment("Sheet1", "A30", `{"author":"`+s+`","text":"`+s+`"}`))
|
|
assert.NoError(t, f.AddComment("Sheet2", "B7", `{"author":"Excelize: ","text":"This is a comment."}`))
|
|
|
|
// Test add comment on not exists worksheet.
|
|
assert.EqualError(t, f.AddComment("SheetN", "B7", `{"author":"Excelize: ","text":"This is a comment."}`), "sheet SheetN does not exist")
|
|
// Test add comment on with illegal cell reference
|
|
assert.EqualError(t, f.AddComment("Sheet1", "A", `{"author":"Excelize: ","text":"This is a comment."}`), newCellNameToCoordinatesError("A", newInvalidCellNameError("A")).Error())
|
|
if assert.NoError(t, f.SaveAs(filepath.Join("test", "TestAddComments.xlsx"))) {
|
|
assert.Len(t, f.GetComments(), 2)
|
|
}
|
|
|
|
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 := f.GetComments()
|
|
assert.EqualValues(t, 2, len(comments["Sheet1"]))
|
|
assert.EqualValues(t, 1, len(comments["Sheet2"]))
|
|
assert.EqualValues(t, len(NewFile().GetComments()), 0)
|
|
}
|
|
|
|
func TestDeleteComment(t *testing.T) {
|
|
f, err := prepareTestBook1()
|
|
if !assert.NoError(t, err) {
|
|
t.FailNow()
|
|
}
|
|
|
|
assert.NoError(t, f.AddComment("Sheet2", "A40", `{"author":"Excelize: ","text":"This is a comment1."}`))
|
|
assert.NoError(t, f.AddComment("Sheet2", "A41", `{"author":"Excelize: ","text":"This is a comment2."}`))
|
|
assert.NoError(t, f.AddComment("Sheet2", "C41", `{"author":"Excelize: ","text":"This is a comment3."}`))
|
|
assert.NoError(t, f.AddComment("Sheet2", "C41", `{"author":"Excelize: ","text":"This is a comment3-1."}`))
|
|
assert.NoError(t, f.AddComment("Sheet2", "C42", `{"author":"Excelize: ","text":"This is a comment4."}`))
|
|
assert.NoError(t, f.AddComment("Sheet2", "C41", `{"author":"Excelize: ","text":"This is a comment3-2."}`))
|
|
|
|
assert.NoError(t, f.DeleteComment("Sheet2", "A40"))
|
|
|
|
assert.EqualValues(t, 5, len(f.GetComments()["Sheet2"]))
|
|
assert.EqualValues(t, len(NewFile().GetComments()), 0)
|
|
|
|
// 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"))
|
|
assert.EqualValues(t, 0, len(f.GetComments()["Sheet2"]))
|
|
// 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"))
|
|
}
|
|
|
|
func TestDecodeVMLDrawingReader(t *testing.T) {
|
|
f := NewFile()
|
|
path := "xl/drawings/vmlDrawing1.xml"
|
|
f.Pkg.Store(path, MacintoshCyrillicCharset)
|
|
f.decodeVMLDrawingReader(path)
|
|
}
|
|
|
|
func TestCommentsReader(t *testing.T) {
|
|
f := NewFile()
|
|
path := "xl/comments1.xml"
|
|
f.Pkg.Store(path, MacintoshCyrillicCharset)
|
|
f.commentsReader(path)
|
|
}
|
|
|
|
func TestCountComments(t *testing.T) {
|
|
f := NewFile()
|
|
f.Comments["xl/comments1.xml"] = nil
|
|
assert.Equal(t, f.countComments(), 1)
|
|
}
|