Explorar el Código

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

Ri Xu hace 9 años
padre
commit
6e1475a242
Se han modificado 4 ficheros con 48 adiciones y 25 borrados
  1. 29 17
      excelize.go
  2. 2 5
      sheet.go
  3. BIN
      test/Workbook1.xlsx
  4. 17 3
      xmlWorksheet.go

+ 29 - 17
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
 }
 

+ 2 - 5
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
 }

BIN
test/Workbook1.xlsx


+ 17 - 3
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"`
+}