From 7345247a916f5186e64d11e42d7d4007f3db4cd9 Mon Sep 17 00:00:00 2001 From: Liu Ming Date: Wed, 25 Mar 2015 11:03:05 +0800 Subject: [PATCH] add file link support --- bof.go | 20 +++++++++++++++++++- cell_range.go | 28 ++++++++++++++-------------- worksheet.go | 32 ++++++++++++++++++-------------- 3 files changed, 51 insertions(+), 29 deletions(-) diff --git a/bof.go b/bof.go index d0483d7..f963a58 100644 --- a/bof.go +++ b/bof.go @@ -1,12 +1,30 @@ package xls -import () +import ( + "bytes" + "encoding/binary" + "io" + "unicode/utf16" +) type BOF struct { Id uint16 Size uint16 } +func (b *BOF) Reader(buf io.ReadSeeker) io.ReadSeeker { + rts := make([]byte, b.Size) + buf.Read(rts) + return bytes.NewReader(rts) +} + +func (b *BOF) Utf16String(buf io.ReadSeeker, count uint32) string { + var bts = make([]uint16, count) + binary.Read(buf, binary.LittleEndian, &bts) + runes := utf16.Decode(bts[:len(bts)-1]) + return string(runes) +} + type BIFFHeader struct { Ver uint16 Type uint16 diff --git a/cell_range.go b/cell_range.go index 1145dd8..3e7525b 100644 --- a/cell_range.go +++ b/cell_range.go @@ -34,26 +34,26 @@ func (c *CellRange) LastCol() uint16 { type HyperLink struct { CellRange - Description string - TextMark string - TargetFrame string - Url string + Description string + TextMark string + TargetFrame string + Url string + ShortedFilePath string + ExtendedFilePath string + IsUrl bool } func (h *HyperLink) String(wb *WorkBook) []string { res := make([]string, h.LastColB-h.FristColB+1) - str := fmt.Sprintf("%s(%s)", h.Description, h.Url) + var str string + if h.IsUrl { + str = fmt.Sprintf("%s(%s)", h.Description, h.Url) + } else { + str = h.ExtendedFilePath + } + for i := uint16(0); i < h.LastColB-h.FristColB+1; i++ { res[i] = str } return res } - -// func (h *HyperLinkCR) ParseFrom(buf io.ReadSeeker) { -// if h.Flags&0x1 != 0 { -// binary.Read(buf, binary.LittleEndian, &richtext_num) -// } -// if h.Flags&0x4 != 0 { -// binary.Read(buf, binary.LittleEndian, &phonetic_size) -// } -// } diff --git a/worksheet.go b/worksheet.go index 8c441e2..f140b5d 100644 --- a/worksheet.go +++ b/worksheet.go @@ -87,7 +87,6 @@ func (w *WorkSheet) parseBof(buf io.ReadSeeker, bof *BOF, pre *BOF) *BOF { case 0x1b8: //HYPERLINK var hy HyperLink binary.Read(buf, binary.LittleEndian, &hy.CellRange) - buf.Seek(20, 1) var flag uint32 binary.Read(buf, binary.LittleEndian, &flag) @@ -95,29 +94,34 @@ func (w *WorkSheet) parseBof(buf io.ReadSeeker, bof *BOF, pre *BOF) *BOF { if flag&0x14 != 0 { binary.Read(buf, binary.LittleEndian, &count) - var bts = make([]uint16, count) - binary.Read(buf, binary.LittleEndian, &bts) - runes := utf16.Decode(bts[:len(bts)-1]) - hy.Description = string(runes) + hy.Description = bof.Utf16String(buf, count) } if flag&0x80 != 0 { binary.Read(buf, binary.LittleEndian, &count) - var bts = make([]uint16, count) - binary.Read(buf, binary.LittleEndian, &bts) - runes := utf16.Decode(bts[:len(bts)-1]) - hy.TargetFrame = string(runes) + hy.TargetFrame = bof.Utf16String(buf, count) } if flag&0x1 != 0 { var guid [2]uint64 binary.Read(buf, binary.BigEndian, &guid) if guid[0] == 0xE0C9EA79F9BACE11 && guid[1] == 0x8C8200AA004BA90B { //URL + hy.IsUrl = true binary.Read(buf, binary.LittleEndian, &count) - var bts = make([]uint16, count/2) + hy.Url = bof.Utf16String(buf, count/2) + } else if guid[0] == 0x303000000000000 && guid[1] == 0xC000000000000046 { //URL{ + var upCount uint16 + binary.Read(buf, binary.LittleEndian, &upCount) + binary.Read(buf, binary.LittleEndian, &count) + bts := make([]byte, count) binary.Read(buf, binary.LittleEndian, &bts) - runes := utf16.Decode(bts[:len(bts)-1]) - hy.Url = string(runes) - } else { - log.Panicln("not support yet") + hy.ShortedFilePath = string(bts) + buf.Seek(24, 1) + binary.Read(buf, binary.LittleEndian, &count) + if count > 0 { + binary.Read(buf, binary.LittleEndian, &count) + buf.Seek(2, 1) + hy.ExtendedFilePath = bof.Utf16String(buf, count/2+1) + } + log.Println(hy) } } if flag&0x8 != 0 {