From 03cbf97453b14197a742eb161a9ac9cc924389c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=B4=E4=BF=8A=E6=9D=B0?= Date: Fri, 11 Sep 2020 14:25:20 +0800 Subject: [PATCH] fix:add parse merge cell --- .gitignore | 2 ++ col.go | 22 +++++++++++++++++----- worksheet.go | 17 +++++++++++++++-- 3 files changed, 34 insertions(+), 7 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..40f1746 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.idea/ +go.sum \ No newline at end of file diff --git a/col.go b/col.go index 8636566..6f4005f 100644 --- a/col.go +++ b/col.go @@ -11,7 +11,7 @@ import ( yymmdd "github.com/extrame/goyymmdd" ) -//content type +// content type type contentHandler interface { String(*WorkBook) []string FirstCol() uint16 @@ -64,7 +64,7 @@ func (xf *XfRk) String(wb *WorkBook) string { strings.Contains(formatterLower, "d.y") || strings.Contains(formatterLower, "h:") || strings.Contains(formatterLower, "д.г") { - //If format contains # or .00 then this is a number + // If format contains # or .00 then this is a number return xf.Rk.String() } else { i, f, isFloat := xf.Rk.number() @@ -82,7 +82,7 @@ func (xf *XfRk) String(wb *WorkBook) string { f = float64(i) } t := timeFromExcelTime(f, wb.dateMode == 1) - return t.Format(time.RFC3339) //TODO it should be international + return t.Format(time.RFC3339) // TODO it should be international } } return xf.Rk.String() @@ -184,8 +184,8 @@ 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 +// str, err = wb.get_string(buf_item, size) +// wb.sst[offset_pre] = wb.sst[offset_pre] + str type FormulaCol struct { Header struct { @@ -238,3 +238,15 @@ type BlankCol struct { func (c *BlankCol) String(wb *WorkBook) []string { return []string{""} } + +type MergeCells struct { + Count uint16 + Refs []Ref8 +} + +type Ref8 struct { + RwFirst uint16 + RwLast uint16 + ColFirst uint16 + ColLast uint16 +} diff --git a/worksheet.go b/worksheet.go index 0f4ec8b..35eda7b 100644 --- a/worksheet.go +++ b/worksheet.go @@ -5,6 +5,7 @@ import ( "encoding/binary" "fmt" "io" + "log" "unicode/utf16" ) @@ -35,6 +36,8 @@ type WorkSheet struct { MaxRow uint16 parsed bool rightToLeft bool + // NOTICE: get all merge cell + mergeCells *MergeCells } func (w *WorkSheet) Row(i int) *Row { @@ -70,8 +73,18 @@ func (w *WorkSheet) parseBof(buf io.ReadSeeker, b *bof, pre *bof, col_pre interf binary.Read(buf, binary.LittleEndian, bts) buf = bytes.NewReader(bts) switch b.Id { - // case 0x0E5: //MERGEDCELLS - // ws.mergedCells(buf) + case 0x0E5: // MERGEDCELLS + mergeCells := new(MergeCells) + err := binary.Read(buf, binary.LittleEndian, &mergeCells.Count) + size := (b.Size - 2) / 8 + mergeCells.Refs = make([]Ref8, size) + for i := uint16(0); i < size; i++ { + binary.Read(buf, binary.LittleEndian, &mergeCells.Refs[i]) + } + if err != nil { + log.Fatal(err) + } + w.mergeCells = mergeCells case 0x23E: // WINDOW2 var sheetOptions, firstVisibleRow, firstVisibleColumn uint16 binary.Read(buf, binary.LittleEndian, &sheetOptions)