xmlSharedStrings.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // Copyright 2016 - 2020 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 Exce™ 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.10 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. }
  40. // String extracts characters from a string item.
  41. func (x xlsxSI) String() string {
  42. if len(x.R) > 0 {
  43. var rows strings.Builder
  44. for _, s := range x.R {
  45. if s.T != nil {
  46. rows.WriteString(s.T.Val)
  47. }
  48. }
  49. return rows.String()
  50. }
  51. if x.T != nil {
  52. return x.T.Val
  53. }
  54. return ""
  55. }
  56. // xlsxR represents a run of rich text. A rich text run is a region of text
  57. // that share a common set of properties, such as formatting properties. The
  58. // properties are defined in the rPr element, and the text displayed to the
  59. // user is defined in the Text (t) element.
  60. type xlsxR struct {
  61. RPr *xlsxRPr `xml:"rPr"`
  62. T *xlsxT `xml:"t"`
  63. }
  64. // xlsxT directly maps the t element in the run properties.
  65. type xlsxT struct {
  66. XMLName xml.Name `xml:"t"`
  67. Space xml.Attr `xml:"space,attr,omitempty"`
  68. Val string `xml:",chardata"`
  69. }
  70. // xlsxRPr (Run Properties) specifies a set of run properties which shall be
  71. // applied to the contents of the parent run after all style formatting has been
  72. // applied to the text. These properties are defined as direct formatting, since
  73. // they are directly applied to the run and supersede any formatting from
  74. // styles.
  75. type xlsxRPr struct {
  76. RFont *attrValString `xml:"rFont"`
  77. Charset *attrValInt `xml:"charset"`
  78. Family *attrValInt `xml:"family"`
  79. B string `xml:"b,omitempty"`
  80. I string `xml:"i,omitempty"`
  81. Strike string `xml:"strike,omitempty"`
  82. Outline string `xml:"outline,omitempty"`
  83. Shadow string `xml:"shadow,omitempty"`
  84. Condense string `xml:"condense,omitempty"`
  85. Extend string `xml:"extend,omitempty"`
  86. Color *xlsxColor `xml:"color"`
  87. Sz *attrValFloat `xml:"sz"`
  88. U *attrValString `xml:"u"`
  89. VertAlign *attrValString `xml:"vertAlign"`
  90. Scheme *attrValString `xml:"scheme"`
  91. }
  92. // RichTextRun directly maps the settings of the rich text run.
  93. type RichTextRun struct {
  94. Font *Font
  95. Text string
  96. }