docProps_test.go 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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 / XLSM / XLTM files. Supports reading and writing
  7. // spreadsheet documents generated by Microsoft Excel™ 2007 and later. Supports
  8. // complex components by high compatibility, and provided streaming API for
  9. // generating or reading data from a worksheet with huge amounts of data. This
  10. // library needs Go version 1.15 or later.
  11. package excelize
  12. import (
  13. "path/filepath"
  14. "testing"
  15. "github.com/stretchr/testify/assert"
  16. )
  17. var MacintoshCyrillicCharset = []byte{0x8F, 0xF0, 0xE8, 0xE2, 0xE5, 0xF2, 0x20, 0xEC, 0xE8, 0xF0}
  18. func TestSetDocProps(t *testing.T) {
  19. f, err := OpenFile(filepath.Join("test", "Book1.xlsx"))
  20. if !assert.NoError(t, err) {
  21. t.FailNow()
  22. }
  23. assert.NoError(t, f.SetDocProps(&DocProperties{
  24. Category: "category",
  25. ContentStatus: "Draft",
  26. Created: "2019-06-04T22:00:10Z",
  27. Creator: "Go Excelize",
  28. Description: "This file created by Go Excelize",
  29. Identifier: "xlsx",
  30. Keywords: "Spreadsheet",
  31. LastModifiedBy: "Go Author",
  32. Modified: "2019-06-04T22:00:10Z",
  33. Revision: "0",
  34. Subject: "Test Subject",
  35. Title: "Test Title",
  36. Language: "en-US",
  37. Version: "1.0.0",
  38. }))
  39. assert.NoError(t, f.SaveAs(filepath.Join("test", "TestSetDocProps.xlsx")))
  40. f.Pkg.Store("docProps/core.xml", nil)
  41. assert.NoError(t, f.SetDocProps(&DocProperties{}))
  42. // Test unsupported charset
  43. f = NewFile()
  44. f.Pkg.Store("docProps/core.xml", MacintoshCyrillicCharset)
  45. assert.EqualError(t, f.SetDocProps(&DocProperties{}), "xml decode error: XML syntax error on line 1: invalid UTF-8")
  46. }
  47. func TestGetDocProps(t *testing.T) {
  48. f, err := OpenFile(filepath.Join("test", "Book1.xlsx"))
  49. if !assert.NoError(t, err) {
  50. t.FailNow()
  51. }
  52. props, err := f.GetDocProps()
  53. assert.NoError(t, err)
  54. assert.Equal(t, props.Creator, "Microsoft Office User")
  55. f.Pkg.Store("docProps/core.xml", nil)
  56. _, err = f.GetDocProps()
  57. assert.NoError(t, err)
  58. // Test unsupported charset
  59. f = NewFile()
  60. f.Pkg.Store("docProps/core.xml", MacintoshCyrillicCharset)
  61. _, err = f.GetDocProps()
  62. assert.EqualError(t, err, "xml decode error: XML syntax error on line 1: invalid UTF-8")
  63. }