comment_test.go 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. assert.EqualValues(t, len(NewFile().GetComments()), 0)
  37. }
  38. func TestDecodeVMLDrawingReader(t *testing.T) {
  39. f := NewFile()
  40. path := "xl/drawings/vmlDrawing1.xml"
  41. f.XLSX[path] = MacintoshCyrillicCharset
  42. f.decodeVMLDrawingReader(path)
  43. }
  44. func TestCommentsReader(t *testing.T) {
  45. f := NewFile()
  46. path := "xl/comments1.xml"
  47. f.XLSX[path] = MacintoshCyrillicCharset
  48. f.commentsReader(path)
  49. }
  50. func TestCountComments(t *testing.T) {
  51. f := NewFile()
  52. f.Comments["xl/comments1.xml"] = nil
  53. assert.Equal(t, f.countComments(), 1)
  54. }