comment_test.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. // Copyright 2016 - 2021 The excelize Authors. All rights reserved. Use of
  2. // this source code is governed by a BSD-style license that can be found in
  3. // the LICENSE file.
  4. //
  5. // Package excelize providing a set of functions that allow you to write to
  6. // and read from XLSX files. Support reads and writes XLSX file generated by
  7. // Microsoft Excel™ 2007 and later. Support save file without losing original
  8. // charts of XLSX. This library needs Go version 1.15 or later.
  9. package excelize
  10. import (
  11. "path/filepath"
  12. "strings"
  13. "testing"
  14. "github.com/stretchr/testify/assert"
  15. )
  16. func TestAddComments(t *testing.T) {
  17. f, err := prepareTestBook1()
  18. if !assert.NoError(t, err) {
  19. t.FailNow()
  20. }
  21. s := strings.Repeat("c", 32768)
  22. assert.NoError(t, f.AddComment("Sheet1", "A30", `{"author":"`+s+`","text":"`+s+`"}`))
  23. assert.NoError(t, f.AddComment("Sheet2", "B7", `{"author":"Excelize: ","text":"This is a comment."}`))
  24. // Test add comment on not exists worksheet.
  25. assert.EqualError(t, f.AddComment("SheetN", "B7", `{"author":"Excelize: ","text":"This is a comment."}`), "sheet SheetN is not exist")
  26. // Test add comment on with illegal cell coordinates
  27. assert.EqualError(t, f.AddComment("Sheet1", "A", `{"author":"Excelize: ","text":"This is a comment."}`), `cannot convert cell "A" to coordinates: invalid cell name "A"`)
  28. if assert.NoError(t, f.SaveAs(filepath.Join("test", "TestAddComments.xlsx"))) {
  29. assert.Len(t, f.GetComments(), 2)
  30. }
  31. f.Comments["xl/comments2.xml"] = nil
  32. f.XLSX["xl/comments2.xml"] = []byte(`<?xml version="1.0" encoding="UTF-8" standalone="yes"?><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>`)
  33. comments := f.GetComments()
  34. assert.EqualValues(t, 2, len(comments["Sheet1"]))
  35. assert.EqualValues(t, 1, len(comments["Sheet2"]))
  36. }
  37. func TestDecodeVMLDrawingReader(t *testing.T) {
  38. f := NewFile()
  39. path := "xl/drawings/vmlDrawing1.xml"
  40. f.XLSX[path] = MacintoshCyrillicCharset
  41. f.decodeVMLDrawingReader(path)
  42. }
  43. func TestCommentsReader(t *testing.T) {
  44. f := NewFile()
  45. path := "xl/comments1.xml"
  46. f.XLSX[path] = MacintoshCyrillicCharset
  47. f.commentsReader(path)
  48. }
  49. func TestCountComments(t *testing.T) {
  50. f := NewFile()
  51. f.Comments["xl/comments1.xml"] = nil
  52. assert.Equal(t, f.countComments(), 1)
  53. }