From f958f05a3bf6f9fdc7f76539d9a3b24f49cc2694 Mon Sep 17 00:00:00 2001 From: Ri Xu Date: Mon, 26 Dec 2016 23:55:59 +0800 Subject: - Fix issue: sheet protection and conditional formatting proprietary missing after save; - Update workbook and sheet relationships and self-close tag replacement hack functions --- sheet.go | 96 ++++++++++++++++++++++++++++++++++++++++++---------------------- 1 file changed, 64 insertions(+), 32 deletions(-) (limited to 'sheet.go') diff --git a/sheet.go b/sheet.go index 16b9a60..aa6844d 100644 --- a/sheet.go +++ b/sheet.go @@ -8,6 +8,55 @@ import ( "strings" ) +// Define the empty element and self-close XML tags hack rules for +// xl/workbook.xml and xl/worksheets/sheet%d.xml. +var ( + WorkbookRules = []map[string]string{ + {`xmlns:relationships="http://schemas.openxmlformats.org/officeDocument/2006/relationships" relationships:id="`: `r:id="`}, + {`>`: ` />`}, + {`>`: ` />`}, + {`>`: ` />`}, + {`>`: ` />`}, + {`>`: ` />`}, + {`>`: ` />`}, + {`>`: ` />`}, + {`>`: ` />`}, + {`>`: ` />`}, + {`>`: ` />`}, + {`>`: ` />`}, + {``: ``}, + {``: ``}, + {``: ``}, + {``: ``}, + {``: ``}, + {``: ``}, + {``: ``}, + {``: ``}, + {``: ``}, + } + SheetRules = []map[string]string{ + {`xmlns:relationships="http://schemas.openxmlformats.org/officeDocument/2006/relationships" relationships:id="`: `r:id="`}, + {``: ``}, + {``: ``}, + {``: ``}, + {``: ``}, + {``: ``}, + {``: ``}, + {``: ``}, + {``: ``}, + {``: ``}, + {``: ``}, + {`>`: ` />`}, + {`>`: ` />`}, + {`>`: ` />`}, + {`>`: ` />`}, + {`>`: ` />`}, + {`>`: ` />`}, + {`>`: ` />`}, + {`>`: ` />`}, + } +) + // NewSheet provice function to greate a new sheet by given index, when // creating a new XLSX file, the default sheet will be create, when you // create a new file, you need to ensure that the index is continuous. @@ -122,17 +171,6 @@ func replaceRelationshipsNameSpace(workbookMarshal string) string { return strings.Replace(workbookMarshal, oldXmlns, newXmlns, -1) } -// replace relationships ID in worksheets/sheet%d.xml -func replaceRelationshipsID(workbookMarshal string) string { - rids := strings.Replace(workbookMarshal, ``, ``, -1) - rids = strings.Replace(rids, ``, ``, -1) - 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) - 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) + for _, rules := range WorkbookRules { + for k, v := range rules { + workbookMarshal = strings.Replace(workbookMarshal, k, v, -1) + } + } + return workbookMarshal +} + +// replace relationships ID in worksheets/sheet%d.xml +func replaceRelationshipsID(workbookMarshal string) string { + for _, rules := range SheetRules { + for k, v := range rules { + workbookMarshal = strings.Replace(workbookMarshal, k, v, -1) + } + } return workbookMarshal } -- cgit v1.2.1