Handle multi row inline strings

The inline string struct is actually the same
as the shared strings struct, reuse it.

Note that Go version 1.10 is required.

Fixes #462
This commit is contained in:
Harris 2019-08-07 16:26:13 -05:00
parent e07581e980
commit acd76425c2
No known key found for this signature in database
GPG Key ID: 0139BFA5D1B8F737
6 changed files with 27 additions and 21 deletions

View File

@ -14,7 +14,7 @@
## Introduction ## 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. 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 ## Basic Usage

View File

@ -1008,6 +1008,11 @@ func TestSharedStrings(t *testing.T) {
t.FailNow() t.FailNow()
} }
assert.Equal(t, "A", rows[0][0]) 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) { func TestSetSheetRow(t *testing.T) {

11
rows.go
View File

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

Binary file not shown.

View File

@ -9,7 +9,10 @@
package excelize package excelize
import "encoding/xml" import (
"encoding/xml"
"strings"
)
// xlsxSST directly maps the sst element from the namespace // xlsxSST directly maps the sst element from the namespace
// http://schemas.openxmlformats.org/spreadsheetml/2006/main. String values may // http://schemas.openxmlformats.org/spreadsheetml/2006/main. String values may
@ -33,6 +36,17 @@ type xlsxSI struct {
R []xlsxR `xml:"r"` 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 // xlsxR directly maps the r element from the namespace
// http://schemas.openxmlformats.org/spreadsheetml/2006/main - currently I have // http://schemas.openxmlformats.org/spreadsheetml/2006/main - currently I have
// not checked this for completeness - it does as much as I need. // not checked this for completeness - it does as much as I need.

View File

@ -9,7 +9,9 @@
package excelize package excelize
import "encoding/xml" import (
"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 - currently I have // http://schemas.openxmlformats.org/spreadsheetml/2006/main - currently I have
@ -424,18 +426,10 @@ type xlsxC struct {
T string `xml:"t,attr,omitempty"` // Type. T string `xml:"t,attr,omitempty"` // Type.
F *xlsxF `xml:"f,omitempty"` // Formula F *xlsxF `xml:"f,omitempty"` // Formula
V string `xml:"v,omitempty"` // Value V string `xml:"v,omitempty"` // Value
IS *xlsxIS `xml:"is"` IS *xlsxSI `xml:"is"`
XMLSpace xml.Attr `xml:"space,attr,omitempty"` 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 // xlsxF directly maps the f element in the namespace
// http://schemas.openxmlformats.org/spreadsheetml/2006/main - currently I have // http://schemas.openxmlformats.org/spreadsheetml/2006/main - currently I have
// not checked it for completeness - it does as much as I need. // not checked it for completeness - it does as much as I need.