xmlSharedStrings.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // Copyright 2016 - 2021 The excelize Authors. All rights reserved. Use of
  2. // this source code is governed by a BSD-style license that can be found in
  3. // the LICENSE file.
  4. //
  5. // Package excelize providing a set of functions that allow you to write to
  6. // and read from XLSX / XLSM / XLTM files. Supports reading and writing
  7. // spreadsheet documents generated by Microsoft Excel™ 2007 and later. Supports
  8. // complex components by high compatibility, and provided streaming API for
  9. // generating or reading data from a worksheet with huge amounts of data. This
  10. // library needs Go version 1.15 or later.
  11. package excelize
  12. import (
  13. "encoding/xml"
  14. "strings"
  15. )
  16. // xlsxSST directly maps the sst element from the namespace
  17. // http://schemas.openxmlformats.org/spreadsheetml/2006/main. String values may
  18. // be stored directly inside spreadsheet cell elements; however, storing the
  19. // same value inside multiple cell elements can result in very large worksheet
  20. // Parts, possibly resulting in performance degradation. The Shared String Table
  21. // is an indexed list of string values, shared across the workbook, which allows
  22. // implementations to store values only once.
  23. type xlsxSST struct {
  24. XMLName xml.Name `xml:"http://schemas.openxmlformats.org/spreadsheetml/2006/main sst"`
  25. Count int `xml:"count,attr"`
  26. UniqueCount int `xml:"uniqueCount,attr"`
  27. SI []xlsxSI `xml:"si"`
  28. }
  29. // xlsxSI (String Item) is the representation of an individual string in the
  30. // Shared String table. If the string is just a simple string with formatting
  31. // applied at the cell level, then the String Item (si) should contain a
  32. // single text element used to express the string. However, if the string in
  33. // the cell is more complex - i.e., has formatting applied at the character
  34. // level - then the string item shall consist of multiple rich text runs which
  35. // collectively are used to express the string.
  36. type xlsxSI struct {
  37. T *xlsxT `xml:"t,omitempty"`
  38. R []xlsxR `xml:"r"`
  39. RPh []*xlsxPhoneticRun `xml:"rPh"`
  40. PhoneticPr *xlsxPhoneticPr `xml:"phoneticPr"`
  41. }
  42. // String extracts characters from a string item.
  43. func (x xlsxSI) String() string {
  44. if len(x.R) > 0 {
  45. var rows strings.Builder
  46. for _, s := range x.R {
  47. if s.T != nil {
  48. rows.WriteString(s.T.Val)
  49. }
  50. }
  51. return bstrUnmarshal(rows.String())
  52. }
  53. if x.T != nil {
  54. return bstrUnmarshal(x.T.Val)
  55. }
  56. return ""
  57. }
  58. // xlsxR represents a run of rich text. A rich text run is a region of text
  59. // that share a common set of properties, such as formatting properties. The
  60. // properties are defined in the rPr element, and the text displayed to the
  61. // user is defined in the Text (t) element.
  62. type xlsxR struct {
  63. RPr *xlsxRPr `xml:"rPr"`
  64. T *xlsxT `xml:"t"`
  65. }
  66. // xlsxT directly maps the t element in the run properties.
  67. type xlsxT struct {
  68. XMLName xml.Name `xml:"t"`
  69. Space xml.Attr `xml:"space,attr,omitempty"`
  70. Val string `xml:",chardata"`
  71. }
  72. // xlsxRPr (Run Properties) specifies a set of run properties which shall be
  73. // applied to the contents of the parent run after all style formatting has been
  74. // applied to the text. These properties are defined as direct formatting, since
  75. // they are directly applied to the run and supersede any formatting from
  76. // styles.
  77. type xlsxRPr struct {
  78. RFont *attrValString `xml:"rFont"`
  79. Charset *attrValInt `xml:"charset"`
  80. Family *attrValInt `xml:"family"`
  81. B *string `xml:"b"`
  82. I *string `xml:"i"`
  83. Strike *string `xml:"strike"`
  84. Outline *string `xml:"outline"`
  85. Shadow *string `xml:"shadow"`
  86. Condense *string `xml:"condense"`
  87. Extend *string `xml:"extend"`
  88. Color *xlsxColor `xml:"color"`
  89. Sz *attrValFloat `xml:"sz"`
  90. U *attrValString `xml:"u"`
  91. VertAlign *attrValString `xml:"vertAlign"`
  92. Scheme *attrValString `xml:"scheme"`
  93. }
  94. // RichTextRun directly maps the settings of the rich text run.
  95. type RichTextRun struct {
  96. Font *Font
  97. Text string
  98. }