Fix hyperlink missing after save issue and update completion row element logic to enhance compatibility.

This commit is contained in:
Ri Xu 2016-12-20 14:40:36 +08:00
parent 2a3620e750
commit 6e1475a242
4 changed files with 48 additions and 25 deletions

View File

@ -138,27 +138,39 @@ func completeCol(xlsx xlsxWorksheet, row int, cell int) xlsxWorksheet {
// Completion row element tags of XML in a sheet. // Completion row element tags of XML in a sheet.
func completeRow(xlsx xlsxWorksheet, row int, cell int) xlsxWorksheet { func completeRow(xlsx xlsxWorksheet, row int, cell int) xlsxWorksheet {
if len(xlsx.SheetData.Row) < row { if len(xlsx.SheetData.Row) >= row {
for i := len(xlsx.SheetData.Row); i < row; i++ { row = len(xlsx.SheetData.Row)
xlsx.SheetData.Row = append(xlsx.SheetData.Row, xlsxRow{ }
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
}
sheetData.Row = append(sheetData.Row, xlsxRow{
R: i + 1, R: i + 1,
}) })
} }
buffer := bytes.Buffer{} buffer := bytes.Buffer{}
for ii := 0; ii < row; ii++ { for ii := 0; ii < row; ii++ {
start := len(xlsx.SheetData.Row[ii].C) start := len(sheetData.Row[ii].C)
if start == 0 { if start == 0 {
for iii := start; iii < cell; iii++ { for iii := start; iii < cell; iii++ {
buffer.WriteString(toAlphaString(iii + 1)) buffer.WriteString(toAlphaString(iii + 1))
buffer.WriteString(strconv.Itoa(ii + 1)) buffer.WriteString(strconv.Itoa(ii + 1))
xlsx.SheetData.Row[ii].C = append(xlsx.SheetData.Row[ii].C, xlsxC{ sheetData.Row[ii].C = append(sheetData.Row[ii].C, xlsxC{
R: buffer.String(), R: buffer.String(),
}) })
buffer.Reset() buffer.Reset()
} }
} }
} }
} xlsx.SheetData = sheetData
return xlsx return xlsx
} }

View File

@ -116,11 +116,6 @@ func (f *File) setAppXML() {
// declarations in a single element of a document. This function is a // declarations in a single element of a document. This function is a
// horrible hack to fix that after the XML marshalling is completed. // horrible hack to fix that after the XML marshalling is completed.
func replaceRelationshipsNameSpace(workbookMarshal string) string { 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">` 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">` 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) return strings.Replace(workbookMarshal, oldXmlns, newXmlns, -1)
@ -129,6 +124,7 @@ func replaceRelationshipsNameSpace(workbookMarshal string) string {
// replace relationships ID in worksheets/sheet%d.xml // replace relationships ID in worksheets/sheet%d.xml
func replaceRelationshipsID(workbookMarshal string) string { func replaceRelationshipsID(workbookMarshal string) string {
rids := strings.Replace(workbookMarshal, `<drawing rid="" />`, ``, -1) rids := strings.Replace(workbookMarshal, `<drawing rid="" />`, ``, -1)
rids = strings.Replace(rids, `<hyperlinks></hyperlinks>`, ``, -1)
return strings.Replace(rids, `<drawing rid="`, `<drawing r:id="`, -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, `></calcPr>`, ` />`, -1)
workbookMarshal = strings.Replace(workbookMarshal, `></workbookProtection>`, ` />`, -1) workbookMarshal = strings.Replace(workbookMarshal, `></workbookProtection>`, ` />`, -1)
workbookMarshal = strings.Replace(workbookMarshal, `></fileRecoveryPr>`, ` />`, -1) workbookMarshal = strings.Replace(workbookMarshal, `></fileRecoveryPr>`, ` />`, -1)
workbookMarshal = strings.Replace(workbookMarshal, `></hyperlink>`, ` />`, -1)
return workbookMarshal return workbookMarshal
} }

Binary file not shown.

View File

@ -2,9 +2,7 @@
package excelize package excelize
import ( import "encoding/xml"
"encoding/xml"
)
// xlsxWorksheet directly maps the worksheet element in the namespace // xlsxWorksheet directly maps the worksheet element in the namespace
// http://schemas.openxmlformats.org/spreadsheetml/2006/main - // http://schemas.openxmlformats.org/spreadsheetml/2006/main -
@ -18,6 +16,7 @@ type xlsxWorksheet struct {
SheetFormatPr xlsxSheetFormatPr `xml:"sheetFormatPr"` SheetFormatPr xlsxSheetFormatPr `xml:"sheetFormatPr"`
Cols *xlsxCols `xml:"cols,omitempty"` Cols *xlsxCols `xml:"cols,omitempty"`
SheetData xlsxSheetData `xml:"sheetData"` SheetData xlsxSheetData `xml:"sheetData"`
Hyperlinks xlsxHyperlinks `xml:"hyperlinks"`
MergeCells *xlsxMergeCells `xml:"mergeCells,omitempty"` MergeCells *xlsxMergeCells `xml:"mergeCells,omitempty"`
PrintOptions xlsxPrintOptions `xml:"printOptions"` PrintOptions xlsxPrintOptions `xml:"printOptions"`
PageMargins xlsxPageMargins `xml:"pageMargins"` PageMargins xlsxPageMargins `xml:"pageMargins"`
@ -278,3 +277,18 @@ type xlsxF struct {
Ref string `xml:"ref,attr,omitempty"` // Shared formula ref Ref string `xml:"ref,attr,omitempty"` // Shared formula ref
Si int `xml:"si,attr,omitempty"` // Shared formula index 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"`
}