summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRi Xu <xuri.me@gmail.com>2016-12-20 14:40:36 +0800
committerRi Xu <xuri.me@gmail.com>2016-12-20 14:40:36 +0800
commit6e1475a2429b2088a4c0b322962fc584370653d9 (patch)
tree1c7de032a3d3d6ea671ee66aab185140388fb94c
parent2a3620e750df7dc8c24791d51ca8e06c0f68799c (diff)
Fix hyperlink missing after save issue and update completion row element logic to enhance compatibility.
-rw-r--r--excelize.go46
-rw-r--r--sheet.go7
-rw-r--r--test/Workbook1.xlsxbin18311 -> 18395 bytes
-rw-r--r--xmlWorksheet.go20
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 := `<workbook xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">`
newXmlns := `<workbook xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships">`
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, `<drawing rid="" />`, ``, -1)
+ rids = strings.Replace(rids, `<hyperlinks></hyperlinks>`, ``, -1)
return strings.Replace(rids, `<drawing rid="`, `<drawing r:id="`, -1)
}
@@ -196,5 +192,6 @@ func workBookCompatibility(workbookMarshal string) string {
workbookMarshal = strings.Replace(workbookMarshal, `></calcPr>`, ` />`, -1)
workbookMarshal = strings.Replace(workbookMarshal, `></workbookProtection>`, ` />`, -1)
workbookMarshal = strings.Replace(workbookMarshal, `></fileRecoveryPr>`, ` />`, -1)
+ workbookMarshal = strings.Replace(workbookMarshal, `></hyperlink>`, ` />`, -1)
return workbookMarshal
}
diff --git a/test/Workbook1.xlsx b/test/Workbook1.xlsx
index d2a9a2e..93b90f2 100644
--- a/test/Workbook1.xlsx
+++ b/test/Workbook1.xlsx
Binary files 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"`
+}