2021-03-30 23:02:22 +08:00
|
|
|
// Copyright 2016 - 2021 The excelize Authors. All rights reserved. Use of
|
2019-06-05 22:06:15 +08:00
|
|
|
// this source code is governed by a BSD-style license that can be found in
|
|
|
|
// the LICENSE file.
|
|
|
|
//
|
|
|
|
// Package excelize providing a set of functions that allow you to write to
|
2021-03-30 23:02:22 +08:00
|
|
|
// and read from XLSX / XLSM / XLTM 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
|
2021-04-04 15:29:43 +08:00
|
|
|
// library needs Go version 1.15 or later.
|
2019-06-05 22:06:15 +08:00
|
|
|
|
|
|
|
package excelize
|
|
|
|
|
|
|
|
import (
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2019-12-22 00:02:09 +08:00
|
|
|
var MacintoshCyrillicCharset = []byte{0x8F, 0xF0, 0xE8, 0xE2, 0xE5, 0xF2, 0x20, 0xEC, 0xE8, 0xF0}
|
|
|
|
|
2019-06-05 22:06:15 +08:00
|
|
|
func TestSetDocProps(t *testing.T) {
|
|
|
|
f, err := OpenFile(filepath.Join("test", "Book1.xlsx"))
|
|
|
|
if !assert.NoError(t, err) {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
assert.NoError(t, f.SetDocProps(&DocProperties{
|
|
|
|
Category: "category",
|
|
|
|
ContentStatus: "Draft",
|
|
|
|
Created: "2019-06-04T22:00:10Z",
|
|
|
|
Creator: "Go Excelize",
|
|
|
|
Description: "This file created by Go Excelize",
|
|
|
|
Identifier: "xlsx",
|
|
|
|
Keywords: "Spreadsheet",
|
|
|
|
LastModifiedBy: "Go Author",
|
|
|
|
Modified: "2019-06-04T22:00:10Z",
|
|
|
|
Revision: "0",
|
|
|
|
Subject: "Test Subject",
|
|
|
|
Title: "Test Title",
|
|
|
|
Language: "en-US",
|
|
|
|
Version: "1.0.0",
|
|
|
|
}))
|
|
|
|
assert.NoError(t, f.SaveAs(filepath.Join("test", "TestSetDocProps.xlsx")))
|
2021-07-05 00:03:56 +08:00
|
|
|
f.Pkg.Store("docProps/core.xml", nil)
|
2019-12-20 00:30:48 +08:00
|
|
|
assert.NoError(t, f.SetDocProps(&DocProperties{}))
|
2019-12-22 00:02:09 +08:00
|
|
|
|
2021-03-30 23:02:22 +08:00
|
|
|
// Test unsupported charset
|
2019-12-22 00:02:09 +08:00
|
|
|
f = NewFile()
|
2021-07-05 00:03:56 +08:00
|
|
|
f.Pkg.Store("docProps/core.xml", MacintoshCyrillicCharset)
|
2019-12-22 00:02:09 +08:00
|
|
|
assert.EqualError(t, f.SetDocProps(&DocProperties{}), "xml decode error: XML syntax error on line 1: invalid UTF-8")
|
2019-06-05 22:06:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetDocProps(t *testing.T) {
|
|
|
|
f, err := OpenFile(filepath.Join("test", "Book1.xlsx"))
|
|
|
|
if !assert.NoError(t, err) {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
props, err := f.GetDocProps()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, props.Creator, "Microsoft Office User")
|
2021-07-05 00:03:56 +08:00
|
|
|
f.Pkg.Store("docProps/core.xml", nil)
|
2019-06-05 22:06:15 +08:00
|
|
|
_, err = f.GetDocProps()
|
2019-12-20 00:30:48 +08:00
|
|
|
assert.NoError(t, err)
|
2019-12-22 00:02:09 +08:00
|
|
|
|
2021-03-30 23:02:22 +08:00
|
|
|
// Test unsupported charset
|
2019-12-22 00:02:09 +08:00
|
|
|
f = NewFile()
|
2021-07-05 00:03:56 +08:00
|
|
|
f.Pkg.Store("docProps/core.xml", MacintoshCyrillicCharset)
|
2019-12-22 00:02:09 +08:00
|
|
|
_, err = f.GetDocProps()
|
|
|
|
assert.EqualError(t, err, "xml decode error: XML syntax error on line 1: invalid UTF-8")
|
2019-06-05 22:06:15 +08:00
|
|
|
}
|