Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle multi row inline strings #464

Merged
merged 1 commit into from
Aug 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
5 changes: 5 additions & 0 deletions excelize_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
11 changes: 2 additions & 9 deletions rows.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
Binary file modified test/SharedStrings.xlsx
Binary file not shown.
16 changes: 15 additions & 1 deletion xmlSharedStrings.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand Down
14 changes: 4 additions & 10 deletions xmlWorksheet.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down