浏览代码

Merge pull request #464 from mlh758/fix-462

Fixed #462 Handle multi row inline strings
xuri 6 年之前
父节点
当前提交
adc4aed472
共有 6 个文件被更改,包括 27 次插入21 次删除
  1. 1 1
      README.md
  2. 5 0
      excelize_test.go
  3. 2 9
      rows.go
  4. 二进制
      test/SharedStrings.xlsx
  5. 15 1
      xmlSharedStrings.go
  6. 4 10
      xmlWorksheet.go

+ 1 - 1
README.md

@@ -14,7 +14,7 @@
 ## Introduction
 
 Excelize is a library written in pure Go providing a set of functions that allow you to write to and read from XLSX files. Supports reading and writing XLSX file generated by Microsoft Excel™ 2007 and later.
-Supports saving a file without losing original charts of XLSX. This library needs Go version 1.8 or later. The full API docs can be seen using go's built-in documentation tool, or online at [godoc.org](https://godoc.org/github.com/360EntSecGroup-Skylar/excelize) and [docs reference](https://xuri.me/excelize/).
+Supports saving a file without losing original charts of XLSX. This library needs Go version 1.10 or later. The full API docs can be seen using go's built-in documentation tool, or online at [godoc.org](https://godoc.org/github.com/360EntSecGroup-Skylar/excelize) and [docs reference](https://xuri.me/excelize/).
 
 ## Basic Usage
 

+ 5 - 0
excelize_test.go

@@ -1008,6 +1008,11 @@ func TestSharedStrings(t *testing.T) {
 		t.FailNow()
 	}
 	assert.Equal(t, "A", rows[0][0])
+	rows, err = f.GetRows("Sheet2")
+	if !assert.NoError(t, err) {
+		t.FailNow()
+	}
+	assert.Equal(t, "Test Weight (Kgs)", rows[0][0])
 }
 
 func TestSetSheetRow(t *testing.T) {

+ 2 - 9
rows.go

@@ -206,18 +206,11 @@ func (xlsx *xlsxC) getValueFrom(f *File, d *xlsxSST) (string, error) {
 	case "s":
 		xlsxSI := 0
 		xlsxSI, _ = strconv.Atoi(xlsx.V)
-		if len(d.SI[xlsxSI].R) > 0 {
-			value := ""
-			for _, v := range d.SI[xlsxSI].R {
-				value += v.T
-			}
-			return value, nil
-		}
-		return f.formattedValue(xlsx.S, d.SI[xlsxSI].T), nil
+		return f.formattedValue(xlsx.S, d.SI[xlsxSI].String()), nil
 	case "str":
 		return f.formattedValue(xlsx.S, xlsx.V), nil
 	case "inlineStr":
-		return f.formattedValue(xlsx.S, xlsx.IS.T), nil
+		return f.formattedValue(xlsx.S, xlsx.IS.String()), nil
 	default:
 		return f.formattedValue(xlsx.S, xlsx.V), nil
 	}

二进制
test/SharedStrings.xlsx


+ 15 - 1
xmlSharedStrings.go

@@ -9,7 +9,10 @@
 
 package excelize
 
-import "encoding/xml"
+import (
+	"encoding/xml"
+	"strings"
+)
 
 // xlsxSST directly maps the sst element from the namespace
 // http://schemas.openxmlformats.org/spreadsheetml/2006/main. String values may
@@ -33,6 +36,17 @@ type xlsxSI struct {
 	R []xlsxR `xml:"r"`
 }
 
+func (x xlsxSI) String() string {
+	if len(x.R) > 0 {
+		var rows strings.Builder
+		for _, s := range x.R {
+			rows.WriteString(s.T)
+		}
+		return rows.String()
+	}
+	return x.T
+}
+
 // xlsxR directly maps the r element from the namespace
 // http://schemas.openxmlformats.org/spreadsheetml/2006/main - currently I have
 // not checked this for completeness - it does as much as I need.

+ 4 - 10
xmlWorksheet.go

@@ -9,7 +9,9 @@
 
 package excelize
 
-import "encoding/xml"
+import (
+	"encoding/xml"
+)
 
 // xlsxWorksheet directly maps the worksheet element in the namespace
 // http://schemas.openxmlformats.org/spreadsheetml/2006/main - currently I have
@@ -424,18 +426,10 @@ type xlsxC struct {
 	T        string   `xml:"t,attr,omitempty"` // Type.
 	F        *xlsxF   `xml:"f,omitempty"`      // Formula
 	V        string   `xml:"v,omitempty"`      // Value
-	IS       *xlsxIS  `xml:"is"`
+	IS       *xlsxSI  `xml:"is"`
 	XMLSpace xml.Attr `xml:"space,attr,omitempty"`
 }
 
-// xlsxIS directly maps the t element. Cell containing an (inline) rich
-// string, i.e., one not in the shared string table. If this cell type is
-// used, then the cell value is in the is element rather than the v element in
-// the cell (c element).
-type xlsxIS struct {
-	T string `xml:"t"`
-}
-
 // xlsxF directly maps the f element in the namespace
 // http://schemas.openxmlformats.org/spreadsheetml/2006/main - currently I have
 // not checked it for completeness - it does as much as I need.