docProps_test.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. // Copyright 2016 - 2020 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.10 or later.
  9. package excelize
  10. import (
  11. "path/filepath"
  12. "testing"
  13. "github.com/stretchr/testify/assert"
  14. )
  15. var MacintoshCyrillicCharset = []byte{0x8F, 0xF0, 0xE8, 0xE2, 0xE5, 0xF2, 0x20, 0xEC, 0xE8, 0xF0}
  16. func TestSetDocProps(t *testing.T) {
  17. f, err := OpenFile(filepath.Join("test", "Book1.xlsx"))
  18. if !assert.NoError(t, err) {
  19. t.FailNow()
  20. }
  21. assert.NoError(t, f.SetDocProps(&DocProperties{
  22. Category: "category",
  23. ContentStatus: "Draft",
  24. Created: "2019-06-04T22:00:10Z",
  25. Creator: "Go Excelize",
  26. Description: "This file created by Go Excelize",
  27. Identifier: "xlsx",
  28. Keywords: "Spreadsheet",
  29. LastModifiedBy: "Go Author",
  30. Modified: "2019-06-04T22:00:10Z",
  31. Revision: "0",
  32. Subject: "Test Subject",
  33. Title: "Test Title",
  34. Language: "en-US",
  35. Version: "1.0.0",
  36. }))
  37. assert.NoError(t, f.SaveAs(filepath.Join("test", "TestSetDocProps.xlsx")))
  38. f.XLSX["docProps/core.xml"] = nil
  39. assert.NoError(t, f.SetDocProps(&DocProperties{}))
  40. // Test unsupport charset
  41. f = NewFile()
  42. f.XLSX["docProps/core.xml"] = MacintoshCyrillicCharset
  43. assert.EqualError(t, f.SetDocProps(&DocProperties{}), "xml decode error: XML syntax error on line 1: invalid UTF-8")
  44. }
  45. func TestGetDocProps(t *testing.T) {
  46. f, err := OpenFile(filepath.Join("test", "Book1.xlsx"))
  47. if !assert.NoError(t, err) {
  48. t.FailNow()
  49. }
  50. props, err := f.GetDocProps()
  51. assert.NoError(t, err)
  52. assert.Equal(t, props.Creator, "Microsoft Office User")
  53. f.XLSX["docProps/core.xml"] = nil
  54. _, err = f.GetDocProps()
  55. assert.NoError(t, err)
  56. // Test unsupport charset
  57. f = NewFile()
  58. f.XLSX["docProps/core.xml"] = MacintoshCyrillicCharset
  59. _, err = f.GetDocProps()
  60. assert.EqualError(t, err, "xml decode error: XML syntax error on line 1: invalid UTF-8")
  61. }