github.com/shakinm/xlsReader@v0.9.12/xls/record/label.go (about)

     1  package record
     2  
     3  import (
     4  	"github.com/shakinm/xlsReader/helpers"
     5  	"golang.org/x/text/encoding/charmap"
     6  	"reflect"
     7  	"strings"
     8  	"unicode/utf16"
     9  )
    10  
    11  //LABEL: Cell Value, String Constant (204h)
    12  
    13  var LabelRecord = []byte{0x04, 0x02} //(204h)
    14  
    15  /*
    16  
    17  A LABEL record describes a cell that contains a pre-BIFF8 string constant. Note:
    18  this was replaced in BIFF8 by LABELSST .
    19  
    20  Record Data 8
    21  Offset		Field Name		Size		Contents
    22  ------------------------------------------------
    23  4			rw				2			Row (0-based)
    24  6			col				2			Column (0-based)
    25  8			ixfe			2			Index to the XF record
    26  10			cch				2			Length of the string (must be <= 255)
    27  12			grbit			1			Option flags
    28  13			rgb				var			Array of string characters
    29  */
    30  
    31  type LabelBIFF8 struct {
    32  	rw    [2]byte
    33  	col   [2]byte
    34  	ixfe  [2]byte
    35  	cch   [2]byte
    36  	grbit [1]byte
    37  	rgb   []byte
    38  }
    39  
    40  type LabelBIFF5 struct {
    41  	rw   [2]byte
    42  	col  [2]byte
    43  	ixfe [2]byte
    44  	cch  [2]byte
    45  	grbit [1]byte
    46  	rgb  []byte
    47  }
    48  
    49  func (r *LabelBIFF8) GetRow() [2]byte {
    50  	return r.rw
    51  }
    52  
    53  func (r *LabelBIFF8) GetCol() [2]byte {
    54  	return r.col
    55  }
    56  
    57  func (r *LabelBIFF8) GetString() string {
    58  	if int(r.grbit[0]) == 1 {
    59  		name := helpers.BytesToUints16(r.rgb[:])
    60  		runes := utf16.Decode(name)
    61  		return string(runes)
    62  	} else {
    63  		return string(decodeWindows1251(r.rgb[:]))
    64  	}
    65  }
    66  
    67  func (r *LabelBIFF8) GetFloat64() (fl float64) {
    68  	return fl
    69  }
    70  func (r *LabelBIFF8) GetInt64() (in int64) {
    71  	return in
    72  }
    73  
    74  func (r *LabelBIFF8) GetType() string {
    75  	return reflect.TypeOf(r).String()
    76  }
    77  
    78  func (r *LabelBIFF8) GetXFIndex() int {
    79  	return int(helpers.BytesToUint16(r.ixfe[:]))
    80  }
    81  
    82  func (r *LabelBIFF8) Read(stream []byte) {
    83  
    84  	copy(r.rw[:], stream[:2])
    85  	copy(r.col[:], stream[2:4])
    86  	copy(r.ixfe[:], stream[4:6])
    87  	copy(r.cch[:], stream[6:8])
    88  	copy(r.grbit[:], stream[8:9])
    89  	if int(r.grbit[0]) == 1 {
    90  		r.rgb = make([]byte, helpers.BytesToUint16(r.cch[:])*2)
    91  	} else {
    92  		r.rgb = make([]byte, helpers.BytesToUint16(r.cch[:]))
    93  	}
    94  
    95  	copy(r.rgb[:], stream[9:])
    96  }
    97  
    98  func (r *LabelBIFF5) GetRow() [2]byte {
    99  	return r.rw
   100  }
   101  
   102  func (r *LabelBIFF5) GetCol() [2]byte {
   103  	return r.col
   104  }
   105  
   106  func (r *LabelBIFF5) GetString() string {
   107  	strLen := helpers.BytesToUint16(r.cch[:])
   108  	return strings.TrimSpace(string(decodeWindows1251(r.rgb[:int(strLen)])))
   109  }
   110  
   111  func (r *LabelBIFF5) GetFloat64() (fl float64) {
   112  	return fl
   113  }
   114  func (r *LabelBIFF5) GetInt64() (in int64) {
   115  	return in
   116  }
   117  
   118  func (r *LabelBIFF5) GetType() string {
   119  	return reflect.TypeOf(r).String()
   120  }
   121  
   122  func (r *LabelBIFF5) GetXFIndex() int {
   123  	return int(helpers.BytesToUint16(r.ixfe[:]))
   124  }
   125  
   126  func (r *LabelBIFF5) Read(stream []byte) {
   127  
   128  	copy(r.rw[:], stream[:2])
   129  	copy(r.col[:], stream[2:4])
   130  	copy(r.ixfe[:], stream[4:6])
   131  	copy(r.cch[:], stream[6:8])
   132  	//copy(r.grbit[:], stream[8:9])
   133  	r.rgb = make([]byte, helpers.BytesToUint16(r.cch[:]))
   134  	copy(r.rgb[:], stream[8:])
   135  }
   136  
   137  func decodeWindows1251(ba []uint8) []uint8 {
   138  	dec := charmap.Windows1251.NewDecoder()
   139  	out, _ := dec.Bytes(ba)
   140  	return out
   141  }