From db99373b25a3f5c986d8f7d26ec983853574a17d Mon Sep 17 00:00:00 2001 From: xuri Date: Tue, 4 Jun 2019 23:30:55 +0800 Subject: Resolve #415, init set and get doc properties support --- docProps.go | 140 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 140 insertions(+) create mode 100755 docProps.go (limited to 'docProps.go') diff --git a/docProps.go b/docProps.go new file mode 100755 index 0000000..0f44ac4 --- /dev/null +++ b/docProps.go @@ -0,0 +1,140 @@ +// Copyright 2016 - 2019 The excelize Authors. All rights reserved. Use of +// 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 +// and read from XLSX files. Support reads and writes XLSX file generated by +// Microsoft Excelâ„¢ 2007 and later. Support save file without losing original +// charts of XLSX. This library needs Go version 1.8 or later. + +package excelize + +import ( + "encoding/xml" + "reflect" +) + +// SetDocProps provides a function to set document core properties. The +// properties that can be set are: +// +// Property | Description +// ----------------+----------------------------------------------------------------------------- +// Title | The name given to the resource. +// | +// Subject | The topic of the content of the resource. +// | +// Creator | An entity primarily responsible for making the content of the resource. +// | +// Keywords | A delimited set of keywords to support searching and indexing. This is +// | typically a list of terms that are not available elsewhere in the properties. +// | +// Description | An explanation of the content of the resource. +// | +// LastModifiedBy | The user who performed the last modification. The identification is +// | environment-specific. +// | +// Language | The language of the intellectual content of the resource. +// | +// Identifier | An unambiguous reference to the resource within a given context. +// | +// Revision | The topic of the content of the resource. +// | +// ContentStatus | The status of the content. For example: Values might include "Draft", +// | "Reviewed", and "Final" +// | +// Category | A categorization of the content of this package. +// | +// Version | The version number. This value is set by the user or by the application. +// +// For example: +// +// err := f.SetDocProps(&excelize.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", +// }) +// +func (f *File) SetDocProps(docProperties *DocProperties) error { + core := decodeCoreProperties{} + err := xml.Unmarshal(namespaceStrictToTransitional(f.readXML("docProps/core.xml")), &core) + if err != nil { + return err + } + newProps := xlsxCoreProperties{ + Dc: NameSpaceDublinCore, + Dcterms: NameSpaceDublinCoreTerms, + Dcmitype: NameSpaceDublinCoreMetadataIntiative, + XSI: NameSpaceXMLSchemaInstance, + Title: core.Title, + Subject: core.Subject, + Creator: core.Creator, + Keywords: core.Keywords, + Description: core.Description, + LastModifiedBy: core.LastModifiedBy, + Language: core.Language, + Identifier: core.Identifier, + Revision: core.Revision, + ContentStatus: core.ContentStatus, + Category: core.Category, + Version: core.Version, + } + newProps.Created.Text = core.Created.Text + newProps.Created.Type = core.Created.Type + newProps.Modified.Text = core.Modified.Text + newProps.Modified.Type = core.Modified.Type + + fields := []string{"Category", "ContentStatus", "Creator", "Description", "Identifier", "Keywords", "LastModifiedBy", "Revision", "Subject", "Title", "Language", "Version"} + immutable := reflect.ValueOf(*docProperties) + mutable := reflect.ValueOf(&newProps).Elem() + for _, field := range fields { + val := immutable.FieldByName(field).String() + if val != "" { + mutable.FieldByName(field).SetString(val) + } + } + if docProperties.Created != "" { + newProps.Created.Text = docProperties.Created + } + if docProperties.Modified != "" { + newProps.Modified.Text = docProperties.Modified + } + output, err := xml.Marshal(&newProps) + f.saveFileList("docProps/core.xml", output) + return err +} + +// GetDocProps provides a function to get document core properties. +func (f *File) GetDocProps() (*DocProperties, error) { + core := decodeCoreProperties{} + err := xml.Unmarshal(namespaceStrictToTransitional(f.readXML("docProps/core.xml")), &core) + if err != nil { + return nil, err + } + return &DocProperties{ + Category: core.Category, + ContentStatus: core.ContentStatus, + Created: core.Created.Text, + Creator: core.Creator, + Description: core.Description, + Identifier: core.Identifier, + Keywords: core.Keywords, + LastModifiedBy: core.LastModifiedBy, + Modified: core.Modified.Text, + Revision: core.Revision, + Subject: core.Subject, + Title: core.Title, + Language: core.Language, + Version: core.Version, + }, nil +} -- cgit v1.2.1 From cff16fa8118291fd885f3f3f75fa07e28bba5eec Mon Sep 17 00:00:00 2001 From: xuri Date: Wed, 5 Jun 2019 22:06:15 +0800 Subject: - Supplemental worksheet struct fields and field order adjustment - Testing case for set and get doc properties - Update charts struct XML tags --- docProps.go | 0 1 file changed, 0 insertions(+), 0 deletions(-) mode change 100755 => 100644 docProps.go (limited to 'docProps.go') diff --git a/docProps.go b/docProps.go old mode 100755 new mode 100644 -- cgit v1.2.1 From dc8210d4a7d18f6425f6f18dc383b26778883715 Mon Sep 17 00:00:00 2001 From: xuri Date: Sun, 30 Jun 2019 19:50:47 +0800 Subject: Update GoDoc and typo fixed --- docProps.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'docProps.go') diff --git a/docProps.go b/docProps.go index 0f44ac4..ff19fda 100644 --- a/docProps.go +++ b/docProps.go @@ -31,7 +31,7 @@ import ( // Description | An explanation of the content of the resource. // | // LastModifiedBy | The user who performed the last modification. The identification is -// | environment-specific. +// | environment-specific. // | // Language | The language of the intellectual content of the resource. // | @@ -40,7 +40,7 @@ import ( // Revision | The topic of the content of the resource. // | // ContentStatus | The status of the content. For example: Values might include "Draft", -// | "Reviewed", and "Final" +// | "Reviewed" and "Final" // | // Category | A categorization of the content of this package. // | -- cgit v1.2.1 From 9c70d0ac868f66badf2663cc7b4b3c46d5411131 Mon Sep 17 00:00:00 2001 From: xuri Date: Sun, 11 Aug 2019 00:36:14 +0800 Subject: Documentation updated, Go 1.10+ required --- docProps.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'docProps.go') diff --git a/docProps.go b/docProps.go index ff19fda..166512f 100644 --- a/docProps.go +++ b/docProps.go @@ -5,7 +5,7 @@ // Package excelize providing a set of functions that allow you to write to // and read from XLSX files. Support reads and writes XLSX file generated by // Microsoft Excelâ„¢ 2007 and later. Support save file without losing original -// charts of XLSX. This library needs Go version 1.8 or later. +// charts of XLSX. This library needs Go version 1.10 or later. package excelize -- cgit v1.2.1 From b1b3c0d15158abc71267da5893de020f047c3872 Mon Sep 17 00:00:00 2001 From: Alex Geer Date: Thu, 19 Dec 2019 19:30:48 +0300 Subject: =?UTF-8?q?Fix=20#539=20Fixed=20error=20opening=20excel=20file=20c?= =?UTF-8?q?reated=20in=20encoding=20d=E2=80=A6=20(#540)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Fixed issue #539 Fixed error opening excel file created in encoding different from UTF-8, added logging of possible errors when decoding XML if the function does not provide exit with an error * Added test for CharsetReader * Fixed #discussion_r359397878 Discussion: https://github.com/360EntSecGroup-Skylar/excelize/pull/540#discussion_r359397878 * Fixed go fmt * go mod tidy and removed unused imports * The code has been refactored --- docProps.go | 66 ++++++++++++++++++++++++++++++++++++++----------------------- 1 file changed, 41 insertions(+), 25 deletions(-) (limited to 'docProps.go') diff --git a/docProps.go b/docProps.go index 166512f..884eb63 100644 --- a/docProps.go +++ b/docProps.go @@ -10,7 +10,10 @@ package excelize import ( + "bytes" "encoding/xml" + "fmt" + "io" "reflect" ) @@ -65,13 +68,23 @@ import ( // Version: "1.0.0", // }) // -func (f *File) SetDocProps(docProperties *DocProperties) error { - core := decodeCoreProperties{} - err := xml.Unmarshal(namespaceStrictToTransitional(f.readXML("docProps/core.xml")), &core) - if err != nil { - return err +func (f *File) SetDocProps(docProperties *DocProperties) (err error) { + var ( + core *decodeCoreProperties + newProps *xlsxCoreProperties + fields []string + output []byte + immutable, mutable reflect.Value + field, val string + ) + + core = new(decodeCoreProperties) + if err = f.xmlNewDecoder(bytes.NewReader(namespaceStrictToTransitional(f.readXML("docProps/core.xml")))). + Decode(core); err != nil && err != io.EOF { + err = fmt.Errorf("xml decode error: %s", err) + return } - newProps := xlsxCoreProperties{ + newProps, err = &xlsxCoreProperties{ Dc: NameSpaceDublinCore, Dcterms: NameSpaceDublinCoreTerms, Dcmitype: NameSpaceDublinCoreMetadataIntiative, @@ -88,18 +101,16 @@ func (f *File) SetDocProps(docProperties *DocProperties) error { ContentStatus: core.ContentStatus, Category: core.Category, Version: core.Version, + }, nil + newProps.Created.Text, newProps.Created.Type, newProps.Modified.Text, newProps.Modified.Type = + core.Created.Text, core.Created.Type, core.Modified.Text, core.Modified.Type + fields = []string{ + "Category", "ContentStatus", "Creator", "Description", "Identifier", "Keywords", + "LastModifiedBy", "Revision", "Subject", "Title", "Language", "Version", } - newProps.Created.Text = core.Created.Text - newProps.Created.Type = core.Created.Type - newProps.Modified.Text = core.Modified.Text - newProps.Modified.Type = core.Modified.Type - - fields := []string{"Category", "ContentStatus", "Creator", "Description", "Identifier", "Keywords", "LastModifiedBy", "Revision", "Subject", "Title", "Language", "Version"} - immutable := reflect.ValueOf(*docProperties) - mutable := reflect.ValueOf(&newProps).Elem() - for _, field := range fields { - val := immutable.FieldByName(field).String() - if val != "" { + immutable, mutable = reflect.ValueOf(*docProperties), reflect.ValueOf(newProps).Elem() + for _, field = range fields { + if val = immutable.FieldByName(field).String(); val != "" { mutable.FieldByName(field).SetString(val) } } @@ -109,19 +120,22 @@ func (f *File) SetDocProps(docProperties *DocProperties) error { if docProperties.Modified != "" { newProps.Modified.Text = docProperties.Modified } - output, err := xml.Marshal(&newProps) + output, err = xml.Marshal(newProps) f.saveFileList("docProps/core.xml", output) - return err + + return } // GetDocProps provides a function to get document core properties. -func (f *File) GetDocProps() (*DocProperties, error) { - core := decodeCoreProperties{} - err := xml.Unmarshal(namespaceStrictToTransitional(f.readXML("docProps/core.xml")), &core) - if err != nil { - return nil, err +func (f *File) GetDocProps() (ret *DocProperties, err error) { + var core = new(decodeCoreProperties) + + if err = f.xmlNewDecoder(bytes.NewReader(namespaceStrictToTransitional(f.readXML("docProps/core.xml")))). + Decode(core); err != nil && err != io.EOF { + err = fmt.Errorf("xml decode error: %s", err) + return } - return &DocProperties{ + ret, err = &DocProperties{ Category: core.Category, ContentStatus: core.ContentStatus, Created: core.Created.Text, @@ -137,4 +151,6 @@ func (f *File) GetDocProps() (*DocProperties, error) { Language: core.Language, Version: core.Version, }, nil + + return } -- cgit v1.2.1 From 09485b3f9f0aefc58d51462aed65c2416205c591 Mon Sep 17 00:00:00 2001 From: xuri Date: Sun, 29 Dec 2019 16:02:31 +0800 Subject: Improve code coverage unit tests --- docProps.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'docProps.go') diff --git a/docProps.go b/docProps.go index 884eb63..a61ee71 100644 --- a/docProps.go +++ b/docProps.go @@ -1,4 +1,4 @@ -// Copyright 2016 - 2019 The excelize Authors. All rights reserved. Use of +// Copyright 2016 - 2020 The excelize Authors. All rights reserved. Use of // this source code is governed by a BSD-style license that can be found in // the LICENSE file. // -- cgit v1.2.1