From 6e1475a2429b2088a4c0b322962fc584370653d9 Mon Sep 17 00:00:00 2001 From: Ri Xu Date: Tue, 20 Dec 2016 14:40:36 +0800 Subject: Fix hyperlink missing after save issue and update completion row element logic to enhance compatibility. --- excelize.go | 46 +++++++++++++++++++++++++++++----------------- sheet.go | 7 ++----- test/Workbook1.xlsx | Bin 18311 -> 18395 bytes xmlWorksheet.go | 20 +++++++++++++++++--- 4 files changed, 48 insertions(+), 25 deletions(-) diff --git a/excelize.go b/excelize.go index bb0659a..162cb2c 100644 --- a/excelize.go +++ b/excelize.go @@ -138,27 +138,39 @@ func completeCol(xlsx xlsxWorksheet, row int, cell int) xlsxWorksheet { // Completion row element tags of XML in a sheet. func completeRow(xlsx xlsxWorksheet, row int, cell int) xlsxWorksheet { - if len(xlsx.SheetData.Row) < row { - for i := len(xlsx.SheetData.Row); i < row; i++ { - xlsx.SheetData.Row = append(xlsx.SheetData.Row, xlsxRow{ - R: i + 1, - }) + if len(xlsx.SheetData.Row) >= row { + row = len(xlsx.SheetData.Row) + } + sheetData := xlsxSheetData{} + existsRows := map[int]int{} + for k, v := range xlsx.SheetData.Row { + existsRows[v.R] = k + } + for i := 0; i < row; i++ { + _, ok := existsRows[i+1] + if ok { + sheetData.Row = append(sheetData.Row, xlsx.SheetData.Row[existsRows[i+1]]) + continue } - buffer := bytes.Buffer{} - for ii := 0; ii < row; ii++ { - start := len(xlsx.SheetData.Row[ii].C) - if start == 0 { - for iii := start; iii < cell; iii++ { - buffer.WriteString(toAlphaString(iii + 1)) - buffer.WriteString(strconv.Itoa(ii + 1)) - xlsx.SheetData.Row[ii].C = append(xlsx.SheetData.Row[ii].C, xlsxC{ - R: buffer.String(), - }) - buffer.Reset() - } + sheetData.Row = append(sheetData.Row, xlsxRow{ + R: i + 1, + }) + } + buffer := bytes.Buffer{} + for ii := 0; ii < row; ii++ { + start := len(sheetData.Row[ii].C) + if start == 0 { + for iii := start; iii < cell; iii++ { + buffer.WriteString(toAlphaString(iii + 1)) + buffer.WriteString(strconv.Itoa(ii + 1)) + sheetData.Row[ii].C = append(sheetData.Row[ii].C, xlsxC{ + R: buffer.String(), + }) + buffer.Reset() } } } + xlsx.SheetData = sheetData return xlsx } diff --git a/sheet.go b/sheet.go index c0e9287..36488d5 100644 --- a/sheet.go +++ b/sheet.go @@ -116,11 +116,6 @@ func (f *File) setAppXML() { // declarations in a single element of a document. This function is a // horrible hack to fix that after the XML marshalling is completed. func replaceRelationshipsNameSpace(workbookMarshal string) string { - // newWorkbook := strings.Replace(workbookMarshal, `xmlns:relationships="http://schemas.openxmlformats.org/officeDocument/2006/relationships" relationships:id`, `r:id`, -1) - // Dirty hack to fix issues #63 and #91; encoding/xml currently - // "doesn't allow for additional namespaces to be defined in the - // root element of the document," as described by @tealeg in the - // comments for #63. oldXmlns := `` newXmlns := `` return strings.Replace(workbookMarshal, oldXmlns, newXmlns, -1) @@ -129,6 +124,7 @@ func replaceRelationshipsNameSpace(workbookMarshal string) string { // replace relationships ID in worksheets/sheet%d.xml func replaceRelationshipsID(workbookMarshal string) string { rids := strings.Replace(workbookMarshal, ``, ``, -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) return workbookMarshal } diff --git a/test/Workbook1.xlsx b/test/Workbook1.xlsx index d2a9a2e..93b90f2 100644 Binary files a/test/Workbook1.xlsx and b/test/Workbook1.xlsx differ diff --git a/xmlWorksheet.go b/xmlWorksheet.go index b83eaa9..349350e 100644 --- a/xmlWorksheet.go +++ b/xmlWorksheet.go @@ -2,9 +2,7 @@ package excelize -import ( - "encoding/xml" -) +import "encoding/xml" // xlsxWorksheet directly maps the worksheet element in the namespace // http://schemas.openxmlformats.org/spreadsheetml/2006/main - @@ -18,6 +16,7 @@ type xlsxWorksheet struct { 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"` @@ -278,3 +277,18 @@ type xlsxF struct { Ref string `xml:"ref,attr,omitempty"` // Shared formula ref Si int `xml:"si,attr,omitempty"` // Shared formula index } + +// xlsxHyperlinks directly maps the hyperlinks element in the namespace +// http://schemas.openxmlformats.org/spreadsheetml/2006/main +type xlsxHyperlinks struct { + Hyperlink []xlsxHyperlink `xml:"hyperlink"` +} + +// xlsxHyperlink directly maps the hyperlink element in the namespace +// http://schemas.openxmlformats.org/spreadsheetml/2006/main +type xlsxHyperlink struct { + Ref string `xml:"ref,attr"` + Location string `xml:"location,attr,omitempty"` + Display string `xml:"display,attr,omitempty"` + RID string `xml:"http://schemas.openxmlformats.org/officeDocument/2006/relationships id,attr,omitempty"` +} -- cgit v1.2.1