This closes #1508, support SST index which contains blank characters

This commit is contained in:
xuri 2023-03-28 00:05:18 +08:00
parent 60b9d029a6
commit 9dbba9f34a
No known key found for this signature in database
GPG Key ID: BA5E5BB1C948EDF7
3 changed files with 23 additions and 14 deletions

View File

@ -564,7 +564,7 @@ func (c *xlsxC) getValueFrom(f *File, d *xlsxSST, raw bool) (string, error) {
case "s": case "s":
if c.V != "" { if c.V != "" {
xlsxSI := 0 xlsxSI := 0
xlsxSI, _ = strconv.Atoi(c.V) xlsxSI, _ = strconv.Atoi(strings.TrimSpace(c.V))
if _, ok := f.tempFiles.Load(defaultXMLPathSharedStrings); ok { if _, ok := f.tempFiles.Load(defaultXMLPathSharedStrings); ok {
return f.formattedValue(c.S, f.getFromStringItem(xlsxSI), raw) return f.formattedValue(c.S, f.getFromStringItem(xlsxSI), raw)
} }

View File

@ -432,6 +432,11 @@ func TestGetValueFrom(t *testing.T) {
value, err := c.getValueFrom(f, sst, false) value, err := c.getValueFrom(f, sst, false)
assert.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, "", value) assert.Equal(t, "", value)
c = xlsxC{T: "s", V: " 1 "}
value, err = c.getValueFrom(f, &xlsxSST{Count: 1, SI: []xlsxSI{{}, {T: &xlsxT{Val: "s"}}}}, false)
assert.NoError(t, err)
assert.Equal(t, "s", value)
} }
func TestGetCellFormula(t *testing.T) { func TestGetCellFormula(t *testing.T) {

View File

@ -47,23 +47,23 @@ type StreamWriter struct {
// 16MB. For example, set data for worksheet of size 102400 rows x 50 columns // 16MB. For example, set data for worksheet of size 102400 rows x 50 columns
// with numbers and style: // with numbers and style:
// //
// file := excelize.NewFile() // f := excelize.NewFile()
// defer func() { // defer func() {
// if err := file.Close(); err != nil { // if err := f.Close(); err != nil {
// fmt.Println(err) // fmt.Println(err)
// } // }
// }() // }()
// streamWriter, err := file.NewStreamWriter("Sheet1") // sw, err := f.NewStreamWriter("Sheet1")
// if err != nil { // if err != nil {
// fmt.Println(err) // fmt.Println(err)
// return // return
// } // }
// styleID, err := file.NewStyle(&excelize.Style{Font: &excelize.Font{Color: "777777"}}) // styleID, err := f.NewStyle(&excelize.Style{Font: &excelize.Font{Color: "777777"}})
// if err != nil { // if err != nil {
// fmt.Println(err) // fmt.Println(err)
// return // return
// } // }
// if err := streamWriter.SetRow("A1", // if err := sw.SetRow("A1",
// []interface{}{ // []interface{}{
// excelize.Cell{StyleID: styleID, Value: "Data"}, // excelize.Cell{StyleID: styleID, Value: "Data"},
// []excelize.RichTextRun{ // []excelize.RichTextRun{
@ -80,30 +80,34 @@ type StreamWriter struct {
// for colID := 0; colID < 50; colID++ { // for colID := 0; colID < 50; colID++ {
// row[colID] = rand.Intn(640000) // row[colID] = rand.Intn(640000)
// } // }
// cell, _ := excelize.CoordinatesToCellName(1, rowID) // cell, err := excelize.CoordinatesToCellName(1, rowID)
// if err := streamWriter.SetRow(cell, row); err != nil { // if err != nil {
// fmt.Println(err)
// break
// }
// if err := sw.SetRow(cell, row); err != nil {
// fmt.Println(err)
// break
// }
// }
// if err := sw.Flush(); err != nil {
// fmt.Println(err) // fmt.Println(err)
// return // return
// } // }
// } // if err := f.SaveAs("Book1.xlsx"); err != nil {
// if err := streamWriter.Flush(); err != nil {
// fmt.Println(err)
// return
// }
// if err := file.SaveAs("Book1.xlsx"); err != nil {
// fmt.Println(err) // fmt.Println(err)
// } // }
// //
// Set cell value and cell formula for a worksheet with stream writer: // Set cell value and cell formula for a worksheet with stream writer:
// //
// err := streamWriter.SetRow("A1", []interface{}{ // err := sw.SetRow("A1", []interface{}{
// excelize.Cell{Value: 1}, // excelize.Cell{Value: 1},
// excelize.Cell{Value: 2}, // excelize.Cell{Value: 2},
// excelize.Cell{Formula: "SUM(A1,B1)"}}); // excelize.Cell{Formula: "SUM(A1,B1)"}});
// //
// Set cell value and rows style for a worksheet with stream writer: // Set cell value and rows style for a worksheet with stream writer:
// //
// err := streamWriter.SetRow("A1", []interface{}{ // err := sw.SetRow("A1", []interface{}{
// excelize.Cell{Value: 1}}, // excelize.Cell{Value: 1}},
// excelize.RowOpts{StyleID: styleID, Height: 20, Hidden: false}); // excelize.RowOpts{StyleID: styleID, Height: 20, Hidden: false});
func (f *File) NewStreamWriter(sheet string) (*StreamWriter, error) { func (f *File) NewStreamWriter(sheet string) (*StreamWriter, error) {
@ -432,7 +436,7 @@ func (sw *StreamWriter) SetRow(cell string, values []interface{}, opts ...RowOpt
// the 'SetColWidth' function before the 'SetRow' function. For example set // the 'SetColWidth' function before the 'SetRow' function. For example set
// the width column B:C as 20: // the width column B:C as 20:
// //
// err := streamWriter.SetColWidth(2, 3, 20) // err := sw.SetColWidth(2, 3, 20)
func (sw *StreamWriter) SetColWidth(min, max int, width float64) error { func (sw *StreamWriter) SetColWidth(min, max int, width float64) error {
if sw.sheetWritten { if sw.sheetWritten {
return ErrStreamSetColWidth return ErrStreamSetColWidth