20
0

[fixed] empty value is^W was returned if cell is string formula

This commit is contained in:
Ivan 2019-04-02 23:38:51 +03:00
parent 5480ee5058
commit 0bc58acfcb
3 changed files with 40 additions and 11 deletions

18
col.go
View File

@ -54,10 +54,10 @@ func (xf *XfRk) String(wb *WorkBook) string {
fNo := wb.Xfs[idx].formatNo() fNo := wb.Xfs[idx].formatNo()
if fNo >= 164 { // user defined format if fNo >= 164 { // user defined format
if formatter := wb.Formats[fNo]; formatter != nil { if formatter := wb.Formats[fNo]; formatter != nil {
if (strings.Contains(formatter.str, "#") || strings.Contains(formatter.str, ".00")){ if strings.Contains(formatter.str, "#") || strings.Contains(formatter.str, ".00") {
//If format contains # or .00 then this is a number //If format contains # or .00 then this is a number
return xf.Rk.String() return xf.Rk.String()
}else{ } else {
i, f, isFloat := xf.Rk.number() i, f, isFloat := xf.Rk.number()
if !isFloat { if !isFloat {
f = float64(i) f = float64(i)
@ -165,6 +165,18 @@ func (c *NumberCol) String(wb *WorkBook) []string {
return []string{strconv.FormatFloat(c.Float, 'f', -1, 64)} return []string{strconv.FormatFloat(c.Float, 'f', -1, 64)}
} }
type FormulaStringCol struct {
Col
RenderedValue string
}
func (c *FormulaStringCol) String(wb *WorkBook) []string {
return []string{c.RenderedValue}
}
//str, err = wb.get_string(buf_item, size)
//wb.sst[offset_pre] = wb.sst[offset_pre] + str
type FormulaCol struct { type FormulaCol struct {
Header struct { Header struct {
Col Col

View File

@ -3,10 +3,10 @@ package xls
import ( import (
"bytes" "bytes"
"encoding/binary" "encoding/binary"
"golang.org/x/text/encoding/charmap"
"io" "io"
"os" "os"
"unicode/utf16" "unicode/utf16"
"golang.org/x/text/encoding/charmap"
) )
//xls workbook type //xls workbook type
@ -162,9 +162,9 @@ func (wb *WorkBook) parseBof(buf io.ReadSeeker, b *bof, pre *bof, offset_pre int
return return
} }
func decodeWindows1251(enc []byte) string { func decodeWindows1251(enc []byte) string {
dec := charmap.Windows1251.NewDecoder() dec := charmap.Windows1251.NewDecoder()
out, _ := dec.Bytes(enc) out, _ := dec.Bytes(enc)
return string(out) return string(out)
} }
func (w *WorkBook) get_string(buf io.ReadSeeker, size uint16) (res string, err error) { func (w *WorkBook) get_string(buf io.ReadSeeker, size uint16) (res string, err error) {
if w.Is5ver { if w.Is5ver {

View File

@ -1,6 +1,7 @@
package xls package xls
import ( import (
"bytes"
"encoding/binary" "encoding/binary"
"fmt" "fmt"
"io" "io"
@ -48,9 +49,10 @@ func (w *WorkSheet) parse(buf io.ReadSeeker) {
w.rows = make(map[uint16]*Row) w.rows = make(map[uint16]*Row)
b := new(bof) b := new(bof)
var bof_pre *bof var bof_pre *bof
var col_pre interface{}
for { for {
if err := binary.Read(buf, binary.LittleEndian, b); err == nil { if err := binary.Read(buf, binary.LittleEndian, b); err == nil {
bof_pre = w.parseBof(buf, b, bof_pre) bof_pre, col_pre = w.parseBof(buf, b, bof_pre, col_pre)
if b.Id == 0xa { if b.Id == 0xa {
break break
} }
@ -62,8 +64,11 @@ func (w *WorkSheet) parse(buf io.ReadSeeker) {
w.parsed = true w.parsed = true
} }
func (w *WorkSheet) parseBof(buf io.ReadSeeker, b *bof, pre *bof) *bof { func (w *WorkSheet) parseBof(buf io.ReadSeeker, b *bof, pre *bof, col_pre interface{}) (*bof, interface{}) {
var col interface{} var col interface{}
var bts = make([]byte, b.Size)
binary.Read(buf, binary.LittleEndian, bts)
buf = bytes.NewReader(bts)
switch b.Id { switch b.Id {
// case 0x0E5: //MERGEDCELLS // case 0x0E5: //MERGEDCELLS
// ws.mergedCells(buf) // ws.mergedCells(buf)
@ -72,7 +77,7 @@ func (w *WorkSheet) parseBof(buf io.ReadSeeker, b *bof, pre *bof) *bof {
binary.Read(buf, binary.LittleEndian, &sheetOptions) binary.Read(buf, binary.LittleEndian, &sheetOptions)
binary.Read(buf, binary.LittleEndian, &firstVisibleRow) // not valuable binary.Read(buf, binary.LittleEndian, &firstVisibleRow) // not valuable
binary.Read(buf, binary.LittleEndian, &firstVisibleColumn) // not valuable binary.Read(buf, binary.LittleEndian, &firstVisibleColumn) // not valuable
buf.Seek(int64(b.Size)-2*3, 1) //buf.Seek(int64(b.Size)-2*3, 1)
w.rightToLeft = (sheetOptions & 0x40) != 0 w.rightToLeft = (sheetOptions & 0x40) != 0
w.Selected = (sheetOptions & 0x400) != 0 w.Selected = (sheetOptions & 0x400) != 0
case 0x208: //ROW case 0x208: //ROW
@ -108,6 +113,18 @@ func (w *WorkSheet) parseBof(buf io.ReadSeeker, b *bof, pre *bof) *bof {
c.Bts = make([]byte, b.Size-20) c.Bts = make([]byte, b.Size-20)
binary.Read(buf, binary.LittleEndian, &c.Bts) binary.Read(buf, binary.LittleEndian, &c.Bts)
col = c col = c
case 0x207: //STRING = FORMULA-VALUE is expected right after FORMULA
if ch, ok := col_pre.(*FormulaCol); ok {
c := new(FormulaStringCol)
c.Col = ch.Header.Col
var cStringLen uint16
binary.Read(buf, binary.LittleEndian, &cStringLen)
str, err := w.wb.get_string(buf, cStringLen)
if nil == err {
c.RenderedValue = str
}
col = c
}
case 0x27e: //RK case 0x27e: //RK
col = new(RkCol) col = new(RkCol)
binary.Read(buf, binary.LittleEndian, col) binary.Read(buf, binary.LittleEndian, col)
@ -182,7 +199,7 @@ func (w *WorkSheet) parseBof(buf io.ReadSeeker, b *bof, pre *bof) *bof {
if col != nil { if col != nil {
w.add(col) w.add(col)
} }
return b return b, col
} }
func (w *WorkSheet) add(content interface{}) { func (w *WorkSheet) add(content interface{}) {