This closes #1148, resolve limitations when adding VBA project to the workbook
Added two exported functions `SetWorkbookPrOptions` and `GetWorkbookPrOptions` to support setting and getting the code name property of the workbook Re-order fields of the workbook properties group to improve the compatibility Go Modules dependencies upgrade Put workbook related operating in new `workbook.go` source code Library introduction docs block updatedpull/2/head
parent
ad09698515
commit
f87c39c41d
@ -0,0 +1,147 @@
|
|||||||
|
// Copyright 2016 - 2022 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 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
|
||||||
|
// data. This library needs Go version 1.15 or later.
|
||||||
|
|
||||||
|
package excelize
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/xml"
|
||||||
|
"io"
|
||||||
|
"log"
|
||||||
|
"path/filepath"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
// WorkbookPrOption is an option of a view of a workbook. See SetWorkbookPrOptions().
|
||||||
|
type WorkbookPrOption interface {
|
||||||
|
setWorkbookPrOption(pr *xlsxWorkbookPr)
|
||||||
|
}
|
||||||
|
|
||||||
|
// WorkbookPrOptionPtr is a writable WorkbookPrOption. See GetWorkbookPrOptions().
|
||||||
|
type WorkbookPrOptionPtr interface {
|
||||||
|
WorkbookPrOption
|
||||||
|
getWorkbookPrOption(pr *xlsxWorkbookPr)
|
||||||
|
}
|
||||||
|
|
||||||
|
// setWorkbook update workbook property of the spreadsheet. Maximum 31
|
||||||
|
// characters are allowed in sheet title.
|
||||||
|
func (f *File) setWorkbook(name string, sheetID, rid int) {
|
||||||
|
content := f.workbookReader()
|
||||||
|
content.Sheets.Sheet = append(content.Sheets.Sheet, xlsxSheet{
|
||||||
|
Name: trimSheetName(name),
|
||||||
|
SheetID: sheetID,
|
||||||
|
ID: "rId" + strconv.Itoa(rid),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// getWorkbookPath provides a function to get the path of the workbook.xml in
|
||||||
|
// the spreadsheet.
|
||||||
|
func (f *File) getWorkbookPath() (path string) {
|
||||||
|
if rels := f.relsReader("_rels/.rels"); rels != nil {
|
||||||
|
rels.Lock()
|
||||||
|
defer rels.Unlock()
|
||||||
|
for _, rel := range rels.Relationships {
|
||||||
|
if rel.Type == SourceRelationshipOfficeDocument {
|
||||||
|
path = strings.TrimPrefix(rel.Target, "/")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// getWorkbookRelsPath provides a function to get the path of the workbook.xml.rels
|
||||||
|
// in the spreadsheet.
|
||||||
|
func (f *File) getWorkbookRelsPath() (path string) {
|
||||||
|
wbPath := f.getWorkbookPath()
|
||||||
|
wbDir := filepath.Dir(wbPath)
|
||||||
|
if wbDir == "." {
|
||||||
|
path = "_rels/" + filepath.Base(wbPath) + ".rels"
|
||||||
|
return
|
||||||
|
}
|
||||||
|
path = strings.TrimPrefix(filepath.Dir(wbPath)+"/_rels/"+filepath.Base(wbPath)+".rels", "/")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// workbookReader provides a function to get the pointer to the workbook.xml
|
||||||
|
// structure after deserialization.
|
||||||
|
func (f *File) workbookReader() *xlsxWorkbook {
|
||||||
|
var err error
|
||||||
|
if f.WorkBook == nil {
|
||||||
|
wbPath := f.getWorkbookPath()
|
||||||
|
f.WorkBook = new(xlsxWorkbook)
|
||||||
|
if _, ok := f.xmlAttr[wbPath]; !ok {
|
||||||
|
d := f.xmlNewDecoder(bytes.NewReader(namespaceStrictToTransitional(f.readXML(wbPath))))
|
||||||
|
f.xmlAttr[wbPath] = append(f.xmlAttr[wbPath], getRootElement(d)...)
|
||||||
|
f.addNameSpaces(wbPath, SourceRelationship)
|
||||||
|
}
|
||||||
|
if err = f.xmlNewDecoder(bytes.NewReader(namespaceStrictToTransitional(f.readXML(wbPath)))).
|
||||||
|
Decode(f.WorkBook); err != nil && err != io.EOF {
|
||||||
|
log.Printf("xml decode error: %s", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return f.WorkBook
|
||||||
|
}
|
||||||
|
|
||||||
|
// workBookWriter provides a function to save workbook.xml after serialize
|
||||||
|
// structure.
|
||||||
|
func (f *File) workBookWriter() {
|
||||||
|
if f.WorkBook != nil {
|
||||||
|
output, _ := xml.Marshal(f.WorkBook)
|
||||||
|
f.saveFileList(f.getWorkbookPath(), replaceRelationshipsBytes(f.replaceNameSpaceBytes(f.getWorkbookPath(), output)))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetWorkbookPrOptions provides a function to sets workbook properties.
|
||||||
|
//
|
||||||
|
// Available options:
|
||||||
|
// CodeName(string)
|
||||||
|
func (f *File) SetWorkbookPrOptions(opts ...WorkbookPrOption) error {
|
||||||
|
wb := f.workbookReader()
|
||||||
|
pr := wb.WorkbookPr
|
||||||
|
if pr == nil {
|
||||||
|
pr = new(xlsxWorkbookPr)
|
||||||
|
wb.WorkbookPr = pr
|
||||||
|
}
|
||||||
|
for _, opt := range opts {
|
||||||
|
opt.setWorkbookPrOption(pr)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// setWorkbookPrOption implements the WorkbookPrOption interface.
|
||||||
|
func (o CodeName) setWorkbookPrOption(pr *xlsxWorkbookPr) {
|
||||||
|
pr.CodeName = string(o)
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetWorkbookPrOptions provides a function to gets workbook properties.
|
||||||
|
//
|
||||||
|
// Available options:
|
||||||
|
// CodeName(string)
|
||||||
|
func (f *File) GetWorkbookPrOptions(opts ...WorkbookPrOptionPtr) error {
|
||||||
|
wb := f.workbookReader()
|
||||||
|
pr := wb.WorkbookPr
|
||||||
|
for _, opt := range opts {
|
||||||
|
opt.getWorkbookPrOption(pr)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// getWorkbookPrOption implements the WorkbookPrOption interface and get the
|
||||||
|
// code name of thw workbook.
|
||||||
|
func (o *CodeName) getWorkbookPrOption(pr *xlsxWorkbookPr) {
|
||||||
|
if pr == nil {
|
||||||
|
*o = ""
|
||||||
|
return
|
||||||
|
}
|
||||||
|
*o = CodeName(pr.CodeName)
|
||||||
|
}
|
@ -0,0 +1,43 @@
|
|||||||
|
package excelize
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func ExampleFile_SetWorkbookPrOptions() {
|
||||||
|
f := NewFile()
|
||||||
|
if err := f.SetWorkbookPrOptions(
|
||||||
|
CodeName("code"),
|
||||||
|
); err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
}
|
||||||
|
// Output:
|
||||||
|
}
|
||||||
|
|
||||||
|
func ExampleFile_GetWorkbookPrOptions() {
|
||||||
|
f := NewFile()
|
||||||
|
var codeName CodeName
|
||||||
|
if err := f.GetWorkbookPrOptions(&codeName); err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
}
|
||||||
|
fmt.Println("Defaults:")
|
||||||
|
fmt.Printf("- codeName: %q\n", codeName)
|
||||||
|
// Output:
|
||||||
|
// Defaults:
|
||||||
|
// - codeName: ""
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestWorkbookPr(t *testing.T) {
|
||||||
|
f := NewFile()
|
||||||
|
wb := f.workbookReader()
|
||||||
|
wb.WorkbookPr = nil
|
||||||
|
var codeName CodeName
|
||||||
|
assert.NoError(t, f.GetWorkbookPrOptions(&codeName))
|
||||||
|
assert.Equal(t, "", string(codeName))
|
||||||
|
assert.NoError(t, f.SetWorkbookPrOptions(CodeName("code")))
|
||||||
|
assert.NoError(t, f.GetWorkbookPrOptions(&codeName))
|
||||||
|
assert.Equal(t, "code", string(codeName))
|
||||||
|
}
|
Loading…
Reference in new issue