2023-01-02 11:47:31 +08:00
|
|
|
// Copyright 2016 - 2023 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.
|
|
|
|
//
|
2022-02-17 00:09:11 +08:00
|
|
|
// Package excelize providing a set of functions that allow you to write to and
|
|
|
|
// read from XLAM / XLSM / XLSX / XLTM / XLTX 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
|
2023-01-02 11:47:31 +08:00
|
|
|
// data. This library needs Go version 1.16 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}
|
|
|
|
|
2021-12-26 14:55:53 +08:00
|
|
|
func TestSetAppProps(t *testing.T) {
|
|
|
|
f, err := OpenFile(filepath.Join("test", "Book1.xlsx"))
|
|
|
|
if !assert.NoError(t, err) {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
assert.NoError(t, f.SetAppProps(&AppProperties{
|
|
|
|
Application: "Microsoft Excel",
|
|
|
|
ScaleCrop: true,
|
|
|
|
DocSecurity: 3,
|
|
|
|
Company: "Company Name",
|
|
|
|
LinksUpToDate: true,
|
|
|
|
HyperlinksChanged: true,
|
|
|
|
AppVersion: "16.0000",
|
|
|
|
}))
|
|
|
|
assert.NoError(t, f.SaveAs(filepath.Join("test", "TestSetAppProps.xlsx")))
|
2022-01-09 00:20:42 +08:00
|
|
|
f.Pkg.Store(defaultXMLPathDocPropsApp, nil)
|
2021-12-26 14:55:53 +08:00
|
|
|
assert.NoError(t, f.SetAppProps(&AppProperties{}))
|
|
|
|
assert.NoError(t, f.Close())
|
|
|
|
|
|
|
|
// Test unsupported charset
|
|
|
|
f = NewFile()
|
2022-01-09 00:20:42 +08:00
|
|
|
f.Pkg.Store(defaultXMLPathDocPropsApp, MacintoshCyrillicCharset)
|
2022-11-12 00:02:11 +08:00
|
|
|
assert.EqualError(t, f.SetAppProps(&AppProperties{}), "XML syntax error on line 1: invalid UTF-8")
|
2021-12-26 14:55:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetAppProps(t *testing.T) {
|
|
|
|
f, err := OpenFile(filepath.Join("test", "Book1.xlsx"))
|
|
|
|
if !assert.NoError(t, err) {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
props, err := f.GetAppProps()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, props.Application, "Microsoft Macintosh Excel")
|
2022-01-09 00:20:42 +08:00
|
|
|
f.Pkg.Store(defaultXMLPathDocPropsApp, nil)
|
2021-12-26 14:55:53 +08:00
|
|
|
_, err = f.GetAppProps()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NoError(t, f.Close())
|
|
|
|
|
Breaking change: changed the function signature for 11 exported functions
* Change
`func (f *File) NewConditionalStyle(style string) (int, error)`
to
`func (f *File) NewConditionalStyle(style *Style) (int, error)`
* Change
`func (f *File) NewStyle(style interface{}) (int, error)`
to
`func (f *File) NewStyle(style *Style) (int, error)`
* Change
`func (f *File) AddChart(sheet, cell, opts string, combo ...string) error`
to
`func (f *File) AddChart(sheet, cell string, chart *ChartOptions, combo ...*ChartOptions) error`
* Change
`func (f *File) AddChartSheet(sheet, opts string, combo ...string) error`
to
`func (f *File) AddChartSheet(sheet string, chart *ChartOptions, combo ...*ChartOptions) error`
* Change
`func (f *File) AddShape(sheet, cell, opts string) error`
to
`func (f *File) AddShape(sheet, cell string, opts *Shape) error`
* Change
`func (f *File) AddPictureFromBytes(sheet, cell, opts, name, extension string, file []byte) error`
to
`func (f *File) AddPictureFromBytes(sheet, cell, name, extension string, file []byte, opts *PictureOptions) error`
* Change
`func (f *File) AddTable(sheet, hCell, vCell, opts string) error`
to
`func (f *File) AddTable(sheet, reference string, opts *TableOptions) error`
* Change
`func (sw *StreamWriter) AddTable(hCell, vCell, opts string) error`
to
`func (sw *StreamWriter) AddTable(reference string, opts *TableOptions) error`
* Change
`func (f *File) AutoFilter(sheet, hCell, vCell, opts string) error`
to
`func (f *File) AutoFilter(sheet, reference string, opts *AutoFilterOptions) error`
* Change
`func (f *File) SetPanes(sheet, panes string) error`
to
`func (f *File) SetPanes(sheet string, panes *Panes) error`
* Change
`func (sw *StreamWriter) AddTable(hCell, vCell, opts string) error`
to
`func (sw *StreamWriter) AddTable(reference string, opts *TableOptions) error`
* Change
`func (f *File) SetConditionalFormat(sheet, reference, opts string) error`
to
`func (f *File) SetConditionalFormat(sheet, reference string, opts []ConditionalFormatOptions) error`
* Add exported types:
* AutoFilterListOptions
* AutoFilterOptions
* Chart
* ChartAxis
* ChartDimension
* ChartLegend
* ChartLine
* ChartMarker
* ChartPlotArea
* ChartSeries
* ChartTitle
* ConditionalFormatOptions
* PaneOptions
* Panes
* PictureOptions
* Shape
* ShapeColor
* ShapeLine
* ShapeParagraph
* TableOptions
* This added support for set sheet visible as very hidden
* Return error when missing required parameters for set defined name
* Update unit test and comments
2022-12-30 00:50:08 +08:00
|
|
|
// Test get application properties with unsupported charset
|
2021-12-26 14:55:53 +08:00
|
|
|
f = NewFile()
|
2022-01-09 00:20:42 +08:00
|
|
|
f.Pkg.Store(defaultXMLPathDocPropsApp, MacintoshCyrillicCharset)
|
2021-12-26 14:55:53 +08:00
|
|
|
_, err = f.GetAppProps()
|
2022-11-12 00:02:11 +08:00
|
|
|
assert.EqualError(t, err, "XML syntax error on line 1: invalid UTF-8")
|
2021-12-26 14:55:53 +08:00
|
|
|
}
|
|
|
|
|
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")))
|
2022-01-09 00:20:42 +08:00
|
|
|
f.Pkg.Store(defaultXMLPathDocPropsCore, nil)
|
2019-12-20 00:30:48 +08:00
|
|
|
assert.NoError(t, f.SetDocProps(&DocProperties{}))
|
2021-09-18 23:20:24 +08:00
|
|
|
assert.NoError(t, f.Close())
|
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()
|
2022-01-09 00:20:42 +08:00
|
|
|
f.Pkg.Store(defaultXMLPathDocPropsCore, MacintoshCyrillicCharset)
|
2022-11-12 00:02:11 +08:00
|
|
|
assert.EqualError(t, f.SetDocProps(&DocProperties{}), "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")
|
2022-01-09 00:20:42 +08:00
|
|
|
f.Pkg.Store(defaultXMLPathDocPropsCore, nil)
|
2019-06-05 22:06:15 +08:00
|
|
|
_, err = f.GetDocProps()
|
2019-12-20 00:30:48 +08:00
|
|
|
assert.NoError(t, err)
|
2021-09-18 23:20:24 +08:00
|
|
|
assert.NoError(t, f.Close())
|
2019-12-22 00:02:09 +08:00
|
|
|
|
Breaking change: changed the function signature for 11 exported functions
* Change
`func (f *File) NewConditionalStyle(style string) (int, error)`
to
`func (f *File) NewConditionalStyle(style *Style) (int, error)`
* Change
`func (f *File) NewStyle(style interface{}) (int, error)`
to
`func (f *File) NewStyle(style *Style) (int, error)`
* Change
`func (f *File) AddChart(sheet, cell, opts string, combo ...string) error`
to
`func (f *File) AddChart(sheet, cell string, chart *ChartOptions, combo ...*ChartOptions) error`
* Change
`func (f *File) AddChartSheet(sheet, opts string, combo ...string) error`
to
`func (f *File) AddChartSheet(sheet string, chart *ChartOptions, combo ...*ChartOptions) error`
* Change
`func (f *File) AddShape(sheet, cell, opts string) error`
to
`func (f *File) AddShape(sheet, cell string, opts *Shape) error`
* Change
`func (f *File) AddPictureFromBytes(sheet, cell, opts, name, extension string, file []byte) error`
to
`func (f *File) AddPictureFromBytes(sheet, cell, name, extension string, file []byte, opts *PictureOptions) error`
* Change
`func (f *File) AddTable(sheet, hCell, vCell, opts string) error`
to
`func (f *File) AddTable(sheet, reference string, opts *TableOptions) error`
* Change
`func (sw *StreamWriter) AddTable(hCell, vCell, opts string) error`
to
`func (sw *StreamWriter) AddTable(reference string, opts *TableOptions) error`
* Change
`func (f *File) AutoFilter(sheet, hCell, vCell, opts string) error`
to
`func (f *File) AutoFilter(sheet, reference string, opts *AutoFilterOptions) error`
* Change
`func (f *File) SetPanes(sheet, panes string) error`
to
`func (f *File) SetPanes(sheet string, panes *Panes) error`
* Change
`func (sw *StreamWriter) AddTable(hCell, vCell, opts string) error`
to
`func (sw *StreamWriter) AddTable(reference string, opts *TableOptions) error`
* Change
`func (f *File) SetConditionalFormat(sheet, reference, opts string) error`
to
`func (f *File) SetConditionalFormat(sheet, reference string, opts []ConditionalFormatOptions) error`
* Add exported types:
* AutoFilterListOptions
* AutoFilterOptions
* Chart
* ChartAxis
* ChartDimension
* ChartLegend
* ChartLine
* ChartMarker
* ChartPlotArea
* ChartSeries
* ChartTitle
* ConditionalFormatOptions
* PaneOptions
* Panes
* PictureOptions
* Shape
* ShapeColor
* ShapeLine
* ShapeParagraph
* TableOptions
* This added support for set sheet visible as very hidden
* Return error when missing required parameters for set defined name
* Update unit test and comments
2022-12-30 00:50:08 +08:00
|
|
|
// Test get workbook properties with unsupported charset
|
2019-12-22 00:02:09 +08:00
|
|
|
f = NewFile()
|
2022-01-09 00:20:42 +08:00
|
|
|
f.Pkg.Store(defaultXMLPathDocPropsCore, MacintoshCyrillicCharset)
|
2019-12-22 00:02:09 +08:00
|
|
|
_, err = f.GetDocProps()
|
2022-11-12 00:02:11 +08:00
|
|
|
assert.EqualError(t, err, "XML syntax error on line 1: invalid UTF-8")
|
2019-06-05 22:06:15 +08:00
|
|
|
}
|