summaryrefslogtreecommitdiff
path: root/sheet.go
diff options
context:
space:
mode:
authorRi Xu <xuri.me@gmail.com>2016-12-23 17:47:25 +0800
committerRi Xu <xuri.me@gmail.com>2016-12-23 17:47:25 +0800
commitb84bfa7eab84a8e065bd5acedeae3d0ba8dc5f8b (patch)
tree2f60fefa79f398f8d413f693993c083311ee4e24 /sheet.go
parenta08c8eb1aecea4a7d82fc70eb2aa4f886a6ed632 (diff)
- Update maximum 31 characters allowed in sheet title;
- Fix issue XML tag `headerFooter` and `sheetPr` element self-close errors cause file corruption; - Fix issue `Section` and `Pane` element order make file corruption in some case; - Change sheet `rId` calculation method in `/xl/workbook.xml`, fix makes file corruption in some case; - Compatibility improved: add `xlsxTabColor` struct and some XML element for worksheet
Diffstat (limited to 'sheet.go')
-rw-r--r--sheet.go26
1 files changed, 15 insertions, 11 deletions
diff --git a/sheet.go b/sheet.go
index 35fd5ed..351a537 100644
--- a/sheet.go
+++ b/sheet.go
@@ -19,9 +19,9 @@ func (f *File) NewSheet(index int, name string) {
// Create new sheet /xl/worksheets/sheet%d.xml
f.setSheet(index)
// Update xl/_rels/workbook.xml.rels
- f.addXlsxWorkbookRels(index)
+ rid := f.addXlsxWorkbookRels(index)
// Update xl/workbook.xml
- f.setWorkbook(index, name)
+ f.setWorkbook(name, rid)
}
// Read and update property of contents type of XLSX.
@@ -54,17 +54,17 @@ func (f *File) setSheet(index int) {
f.saveFileList(path, replaceRelationshipsID(replaceWorkSheetsRelationshipsNameSpace(string(output))))
}
-// Update workbook property of XLSX.
-func (f *File) setWorkbook(index int, name string) {
+// Update workbook property of XLSX. Maximum 31 characters allowed in sheet title.
+func (f *File) setWorkbook(name string, rid int) {
var content xlsxWorkbook
+ if len(name) > 31 {
+ name = name[0:31]
+ }
xml.Unmarshal([]byte(f.readXML(`xl/workbook.xml`)), &content)
-
- rels := f.readXlsxWorkbookRels()
- rID := len(rels.Relationships)
content.Sheets.Sheet = append(content.Sheets.Sheet, xlsxSheet{
Name: name,
- SheetID: strconv.Itoa(index),
- ID: "rId" + strconv.Itoa(rID),
+ SheetID: strconv.Itoa(rid),
+ ID: `rId` + strconv.Itoa(rid),
})
output, err := xml.Marshal(content)
if err != nil {
@@ -81,7 +81,7 @@ func (f *File) readXlsxWorkbookRels() xlsxWorkbookRels {
}
// Update workbook relationships property of XLSX.
-func (f *File) addXlsxWorkbookRels(sheet int) {
+func (f *File) addXlsxWorkbookRels(sheet int) int {
content := f.readXlsxWorkbookRels()
rID := len(content.Relationships) + 1
ID := bytes.Buffer{}
@@ -101,6 +101,7 @@ func (f *File) addXlsxWorkbookRels(sheet int) {
fmt.Println(err)
}
f.saveFileList(`xl/_rels/workbook.xml.rels`, string(output))
+ return rID
}
// Update docProps/app.xml file of XML.
@@ -128,6 +129,7 @@ func replaceRelationshipsID(workbookMarshal string) string {
rids = strings.Replace(rids, `<tableParts count="0"></tableParts>`, ``, -1)
rids = strings.Replace(rids, `<picture></picture>`, ``, -1)
rids = strings.Replace(rids, `<legacyDrawing></legacyDrawing>`, ``, -1)
+ rids = strings.Replace(rids, `<tabColor></tabColor>`, ``, -1)
return strings.Replace(rids, `<drawing rid="`, `<drawing r:id="`, -1)
}
@@ -191,10 +193,12 @@ func workBookCompatibility(workbookMarshal string) string {
workbookMarshal = strings.Replace(workbookMarshal, `></workbookView>`, ` />`, -1)
workbookMarshal = strings.Replace(workbookMarshal, `></fileVersion>`, ` />`, -1)
workbookMarshal = strings.Replace(workbookMarshal, `></workbookPr>`, ` />`, -1)
- workbookMarshal = strings.Replace(workbookMarshal, `></definedNames>`, ` />`, -1)
workbookMarshal = strings.Replace(workbookMarshal, `></calcPr>`, ` />`, -1)
workbookMarshal = strings.Replace(workbookMarshal, `></workbookProtection>`, ` />`, -1)
workbookMarshal = strings.Replace(workbookMarshal, `></fileRecoveryPr>`, ` />`, -1)
workbookMarshal = strings.Replace(workbookMarshal, `></hyperlink>`, ` />`, -1)
+ workbookMarshal = strings.Replace(workbookMarshal, `></tabColor>`, ` />`, -1)
+ workbookMarshal = strings.Replace(workbookMarshal, `></pageSetUpPr>`, ` />`, -1)
+ workbookMarshal = strings.Replace(workbookMarshal, `></pane>`, ` />`, -1)
return workbookMarshal
}