From f958f05a3bf6f9fdc7f76539d9a3b24f49cc2694 Mon Sep 17 00:00:00 2001 From: Ri Xu Date: Mon, 26 Dec 2016 23:55:59 +0800 Subject: [PATCH] - Fix issue: sheet protection and conditional formatting proprietary missing after save; - Update workbook and sheet relationships and self-close tag replacement hack functions --- excelize.go | 9 ----- lib.go | 7 ++-- sheet.go | 96 ++++++++++++++++++++++++++++++++----------------- xmlWorksheet.go | 73 +++++++++++++++++++++++++++---------- 4 files changed, 121 insertions(+), 64 deletions(-) diff --git a/excelize.go b/excelize.go index 86e2d99..af076fb 100644 --- a/excelize.go +++ b/excelize.go @@ -183,15 +183,6 @@ func replaceWorkSheetsRelationshipsNameSpace(workbookMarshal string) string { oldXmlns := `` newXmlns := `` workbookMarshal = strings.Replace(workbookMarshal, oldXmlns, newXmlns, -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 } diff --git a/lib.go b/lib.go index 03fc8f2..6ad3175 100644 --- a/lib.go +++ b/lib.go @@ -7,7 +7,6 @@ import ( "io" "log" "math" - "strings" ) // ReadZip takes a pointer to a zip.ReadCloser and returns a @@ -28,7 +27,7 @@ func ReadZipReader(r *zip.Reader) (map[string]string, int, error) { if len(v.Name) > 18 { if v.Name[0:19] == `xl/worksheets/sheet` { var xlsx xlsxWorksheet - xml.Unmarshal([]byte(strings.Replace(fileList[v.Name], ``: ` />`}, + {`>`: ` />`}, + {`>`: ` />`}, + {`>`: ` />`}, + {`>`: ` />`}, + {`>`: ` />`}, + {`>`: ` />`}, + {`>`: ` />`}, + {`>`: ` />`}, + {`>`: ` />`}, + {`>`: ` />`}, + {``: ``}, + {``: ``}, + {``: ``}, + {``: ``}, + {``: ``}, + {``: ``}, + {``: ``}, + {``: ``}, + {``: ``}, + } + 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 } diff --git a/xmlWorksheet.go b/xmlWorksheet.go index 4752a15..0c5494d 100644 --- a/xmlWorksheet.go +++ b/xmlWorksheet.go @@ -9,29 +9,31 @@ import "encoding/xml" // currently I have not checked it for completeness - it does as much // as I need. type xlsxWorksheet struct { - XMLName xml.Name `xml:"http://schemas.openxmlformats.org/spreadsheetml/2006/main worksheet"` - SheetPr xlsxSheetPr `xml:"sheetPr"` - Dimension xlsxDimension `xml:"dimension"` - SheetViews xlsxSheetViews `xml:"sheetViews"` - SheetFormatPr xlsxSheetFormatPr `xml:"sheetFormatPr"` - Cols *xlsxCols `xml:"cols,omitempty"` - SheetData xlsxSheetData `xml:"sheetData"` - Hyperlinks xlsxHyperlinks `xml:"hyperlinks"` - MergeCells *xlsxMergeCells `xml:"mergeCells,omitempty"` - PrintOptions xlsxPrintOptions `xml:"printOptions"` - PageMargins xlsxPageMargins `xml:"pageMargins"` - PageSetUp xlsxPageSetUp `xml:"pageSetup"` - HeaderFooter xlsxHeaderFooter `xml:"headerFooter"` - Drawing xlsxDrawing `xml:"drawing"` - LegacyDrawing xlsxLegacyDrawing `xml:"legacyDrawing"` - Picture xlsxPicture `xml:"picture"` - TableParts xlsxTableParts `xml:"tableParts"` - ExtLst xlsxExtLst `xml:"extLst"` + XMLName xml.Name `xml:"http://schemas.openxmlformats.org/spreadsheetml/2006/main worksheet"` + SheetPr xlsxSheetPr `xml:"sheetPr"` + Dimension xlsxDimension `xml:"dimension"` + SheetViews xlsxSheetViews `xml:"sheetViews"` + SheetFormatPr xlsxSheetFormatPr `xml:"sheetFormatPr"` + Cols *xlsxCols `xml:"cols,omitempty"` + SheetData xlsxSheetData `xml:"sheetData"` + SheetProtection xlsxSheetProtection `xml:"sheetProtection"` + MergeCells *xlsxMergeCells `xml:"mergeCells,omitempty"` + ConditionalFormatting xlsxConditionalFormatting `xml:"conditionalFormatting"` + Hyperlinks xlsxHyperlinks `xml:"hyperlinks"` + PrintOptions xlsxPrintOptions `xml:"printOptions"` + PageMargins xlsxPageMargins `xml:"pageMargins"` + PageSetUp xlsxPageSetUp `xml:"pageSetup"` + HeaderFooter xlsxHeaderFooter `xml:"headerFooter"` + Drawing xlsxDrawing `xml:"drawing"` + LegacyDrawing xlsxLegacyDrawing `xml:"legacyDrawing"` + Picture xlsxPicture `xml:"picture"` + TableParts xlsxTableParts `xml:"tableParts"` + ExtLst xlsxExtLst `xml:"extLst"` } // xlsxDrawing change r:id to rid in the namespace. type xlsxDrawing struct { - RID string `xml:"rid,attr"` + RID string `xml:"http://schemas.openxmlformats.org/officeDocument/2006/relationships id,attr,omitempty"` } // xlsxHeaderFooter directly maps the headerFooter element in the namespace @@ -303,6 +305,39 @@ type xlsxF struct { Si string `xml:"si,attr,omitempty"` // Shared formula index } +// xlsxSheetProtection collection expresses the sheet protection options +// to enforce when the sheet is protected. +type xlsxSheetProtection struct { + AlgorithmName string `xml:"algorithmName,attr,omitempty"` + AutoFilter int `xml:"autoFilter,attr,omitempty"` + DeleteColumns int `xml:"deleteColumns,attr,omitempty"` + DeleteRows int `xml:"deleteRows,attr,omitempty"` + FormatCells int `xml:"formatCells,attr,omitempty"` + FormatColumns int `xml:"formatColumns,attr,omitempty"` + FormatRows int `xml:"formatRows,attr,omitempty"` + HashValue string `xml:"hashValue,attr,omitempty"` + InsertColumns int `xml:"insertColumns,attr,omitempty"` + InsertHyperlinks int `xml:"insertHyperlinks,attr,omitempty"` + InsertRows int `xml:"insertRows,attr,omitempty"` + Objects int `xml:"objects,attr,omitempty"` + PivotTables int `xml:"pivotTables,attr,omitempty"` + SaltValue string `xml:"saltValue,attr,omitempty"` + Scenarios int `xml:"scenarios,attr,omitempty"` + SelectLockedCells int `xml:"selectLockedCells,attr,omitempty"` + SelectUnlockedCell int `xml:"selectUnlockedCell,attr,omitempty"` + Sheet int `xml:"sheet,attr,omitempty"` + Sort int `xml:"sort,attr,omitempty"` + SpinCount int `xml:"spinCount,attr,omitempty"` +} + +// A Conditional Format is a format, such as cell shading or font color, +// that a spreadsheet application can automatically apply to cells if a +// specified condition is true. This collection expresses conditional +// formatting rules applied to a particular cell or range. +type xlsxConditionalFormatting struct { + CfRule string `xml:",innerxml"` +} + // xlsxHyperlinks directly maps the hyperlinks element in the namespace // http://schemas.openxmlformats.org/spreadsheetml/2006/main type xlsxHyperlinks struct {