From b84bfa7eab84a8e065bd5acedeae3d0ba8dc5f8b Mon Sep 17 00:00:00 2001 From: Ri Xu Date: Fri, 23 Dec 2016 17:47:25 +0800 Subject: - 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 --- sheet.go | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) (limited to 'sheet.go') 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, ``, ``, -1) rids = strings.Replace(rids, ``, ``, -1) rids = strings.Replace(rids, ``, ``, -1) + rids = strings.Replace(rids, ``, ``, -1) return strings.Replace(rids, ``, ` />`, -1) workbookMarshal = strings.Replace(workbookMarshal, `>`, ` />`, -1) workbookMarshal = strings.Replace(workbookMarshal, `>`, ` />`, -1) - workbookMarshal = strings.Replace(workbookMarshal, `>`, ` />`, -1) workbookMarshal = strings.Replace(workbookMarshal, `>`, ` />`, -1) workbookMarshal = strings.Replace(workbookMarshal, `>`, ` />`, -1) workbookMarshal = strings.Replace(workbookMarshal, `>`, ` />`, -1) workbookMarshal = strings.Replace(workbookMarshal, `>`, ` />`, -1) + workbookMarshal = strings.Replace(workbookMarshal, `>`, ` />`, -1) + workbookMarshal = strings.Replace(workbookMarshal, `>`, ` />`, -1) + workbookMarshal = strings.Replace(workbookMarshal, `>`, ` />`, -1) return workbookMarshal } -- cgit v1.2.1