2022-01-09 00:20:42 +08:00
// Copyright 2016 - 2022 The excelize Authors. All rights reserved. Use of
2019-12-22 00:02:09 +08:00
// this source code is governed by a BSD-style license that can be found in
// the LICENSE file.
//
2022-02-17 00:09:11 +08:00
// 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.
2019-12-22 00:02:09 +08:00
package excelize
import (
2022-03-05 14:48:34 +08:00
"encoding/xml"
2019-12-22 00:02:09 +08:00
"path/filepath"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
func TestAddComments ( t * testing . T ) {
f , err := prepareTestBook1 ( )
if ! assert . NoError ( t , err ) {
t . FailNow ( )
}
2022-11-02 08:42:00 +08:00
s := strings . Repeat ( "c" , TotalCellChars + 1 )
assert . NoError ( t , f . AddComment ( "Sheet1" , Comment { Cell : "A30" , Author : s , Text : s , Runs : [ ] RichTextRun { { Text : s } , { Text : s } } } ) )
assert . NoError ( t , f . AddComment ( "Sheet2" , Comment { Cell : "B7" , Author : "Excelize" , Text : s [ : TotalCellChars - 1 ] , Runs : [ ] RichTextRun { { Text : "Excelize: " , Font : & Font { Bold : true } } , { Text : "This is a comment." } } } ) )
2019-12-22 00:02:09 +08:00
// Test add comment on not exists worksheet.
2022-11-02 08:42:00 +08:00
assert . EqualError ( t , f . AddComment ( "SheetN" , Comment { Cell : "B7" , Author : "Excelize" , Runs : [ ] RichTextRun { { Text : "Excelize: " , Font : & Font { Bold : true } } , { Text : "This is a comment." } } } ) , "sheet SheetN does not exist" )
2022-09-18 00:07:15 +08:00
// Test add comment on with illegal cell reference
2022-11-02 08:42:00 +08:00
assert . EqualError ( t , f . AddComment ( "Sheet1" , Comment { Cell : "A" , Author : "Excelize" , Runs : [ ] RichTextRun { { Text : "Excelize: " , Font : & Font { Bold : true } } , { Text : "This is a comment." } } } ) , newCellNameToCoordinatesError ( "A" , newInvalidCellNameError ( "A" ) ) . Error ( ) )
2022-11-12 00:02:11 +08:00
comments , err := f . GetComments ( )
assert . NoError ( t , err )
2019-12-22 00:02:09 +08:00
if assert . NoError ( t , f . SaveAs ( filepath . Join ( "test" , "TestAddComments.xlsx" ) ) ) {
2022-11-12 00:02:11 +08:00
assert . Len ( t , comments , 2 )
2019-12-22 00:02:09 +08:00
}
2020-03-31 00:02:00 +08:00
f . Comments [ "xl/comments2.xml" ] = nil
2022-03-05 14:48:34 +08:00
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> ` ) )
2022-11-12 00:02:11 +08:00
comments , err = f . GetComments ( )
assert . NoError ( t , err )
2020-03-31 00:02:00 +08:00
assert . EqualValues ( t , 2 , len ( comments [ "Sheet1" ] ) )
assert . EqualValues ( t , 1 , len ( comments [ "Sheet2" ] ) )
2022-11-12 00:02:11 +08:00
comments , err = NewFile ( ) . GetComments ( )
assert . NoError ( t , err )
assert . EqualValues ( t , len ( comments ) , 0 )
// Test add comments with unsupported charset.
f . Comments [ "xl/comments2.xml" ] = nil
f . Pkg . Store ( "xl/comments2.xml" , MacintoshCyrillicCharset )
_ , err = f . GetComments ( )
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" )
2019-12-22 00:02:09 +08:00
}
2022-08-19 23:24:13 +08:00
func TestDeleteComment ( t * testing . T ) {
f , err := prepareTestBook1 ( )
if ! assert . NoError ( t , err ) {
t . FailNow ( )
}
2022-11-02 08:42:00 +08:00
assert . NoError ( t , f . AddComment ( "Sheet2" , Comment { Cell : "A40" , Text : "Excelize: This is a comment1." } ) )
assert . NoError ( t , f . AddComment ( "Sheet2" , Comment { Cell : "A41" , Runs : [ ] RichTextRun { { Text : "Excelize: " , Font : & Font { Bold : true } } , { Text : "This is a comment2." } } } ) )
assert . NoError ( t , f . AddComment ( "Sheet2" , Comment { Cell : "C41" , Runs : [ ] RichTextRun { { Text : "Excelize: " , Font : & Font { Bold : true } } , { Text : "This is a comment3." } } } ) )
assert . NoError ( t , f . AddComment ( "Sheet2" , Comment { Cell : "C41" , Runs : [ ] RichTextRun { { Text : "Excelize: " , Font : & Font { Bold : true } } , { Text : "This is a comment3-1." } } } ) )
assert . NoError ( t , f . AddComment ( "Sheet2" , Comment { Cell : "C42" , Runs : [ ] RichTextRun { { Text : "Excelize: " , Font : & Font { Bold : true } } , { Text : "This is a comment4." } } } ) )
assert . NoError ( t , f . AddComment ( "Sheet2" , Comment { Cell : "C41" , Runs : [ ] RichTextRun { { Text : "Excelize: " , Font : & Font { Bold : true } } , { Text : "This is a comment2." } } } ) )
2022-08-19 23:24:13 +08:00
assert . NoError ( t , f . DeleteComment ( "Sheet2" , "A40" ) )
2022-11-12 00:02:11 +08:00
comments , err := f . GetComments ( )
assert . NoError ( t , err )
assert . EqualValues ( t , 5 , len ( comments [ "Sheet2" ] ) )
comments , err = NewFile ( ) . GetComments ( )
assert . NoError ( t , err )
assert . EqualValues ( t , len ( comments ) , 0 )
2022-08-19 23:24:13 +08:00
// Test delete all comments in a worksheet
assert . NoError ( t , f . DeleteComment ( "Sheet2" , "A41" ) )
assert . NoError ( t , f . DeleteComment ( "Sheet2" , "C41" ) )
2022-09-10 13:05:34 +08:00
assert . NoError ( t , f . DeleteComment ( "Sheet2" , "C42" ) )
2022-11-12 00:02:11 +08:00
comments , err = f . GetComments ( )
assert . NoError ( t , err )
assert . EqualValues ( t , 0 , len ( comments [ "Sheet2" ] ) )
2022-08-19 23:24:13 +08:00
// Test delete comment on not exists worksheet
2022-08-28 00:16:41 +08:00
assert . EqualError ( t , f . DeleteComment ( "SheetN" , "A1" ) , "sheet SheetN does not exist" )
2022-08-19 23:24:13 +08:00
// Test delete comment with worksheet part
f . Pkg . Delete ( "xl/worksheets/sheet1.xml" )
assert . NoError ( t , f . DeleteComment ( "Sheet1" , "A22" ) )
2022-11-12 00:02:11 +08:00
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" )
2022-08-19 23:24:13 +08:00
}
2019-12-22 00:02:09 +08:00
func TestDecodeVMLDrawingReader ( t * testing . T ) {
f := NewFile ( )
path := "xl/drawings/vmlDrawing1.xml"
2021-07-05 00:03:56 +08:00
f . Pkg . Store ( path , MacintoshCyrillicCharset )
2019-12-22 00:02:09 +08:00
f . decodeVMLDrawingReader ( path )
}
func TestCommentsReader ( t * testing . T ) {
f := NewFile ( )
2022-11-12 00:02:11 +08:00
// Test read comments with unsupported charset.
2019-12-22 00:02:09 +08:00
path := "xl/comments1.xml"
2021-07-05 00:03:56 +08:00
f . Pkg . Store ( path , MacintoshCyrillicCharset )
2022-11-12 00:02:11 +08:00
_ , err := f . commentsReader ( path )
assert . EqualError ( t , err , "XML syntax error on line 1: invalid UTF-8" )
2019-12-22 00:02:09 +08:00
}
func TestCountComments ( t * testing . T ) {
f := NewFile ( )
f . Comments [ "xl/comments1.xml" ] = nil
assert . Equal ( t , f . countComments ( ) , 1 )
}