xmlSharedStrings.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 string `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. return x.T
  52. }
  53. // xlsxR represents a run of rich text. A rich text run is a region of text
  54. // that share a common set of properties, such as formatting properties. The
  55. // properties are defined in the rPr element, and the text displayed to the
  56. // user is defined in the Text (t) element.
  57. type xlsxR struct {
  58. RPr *xlsxRPr `xml:"rPr"`
  59. T *xlsxT `xml:"t"`
  60. }
  61. // xlsxT directly maps the t element in the run properties.
  62. type xlsxT struct {
  63. XMLName xml.Name `xml:"t"`
  64. Space xml.Attr `xml:"space,attr,omitempty"`
  65. Val string `xml:",innerxml"`
  66. }
  67. // xlsxRPr (Run Properties) specifies a set of run properties which shall be
  68. // applied to the contents of the parent run after all style formatting has been
  69. // applied to the text. These properties are defined as direct formatting, since
  70. // they are directly applied to the run and supersede any formatting from
  71. // styles.
  72. type xlsxRPr struct {
  73. RFont *attrValString `xml:"rFont"`
  74. Charset *attrValInt `xml:"charset"`
  75. Family *attrValInt `xml:"family"`
  76. B string `xml:"b,omitempty"`
  77. I string `xml:"i,omitempty"`
  78. Strike string `xml:"strike,omitempty"`
  79. Outline string `xml:"outline,omitempty"`
  80. Shadow string `xml:"shadow,omitempty"`
  81. Condense string `xml:"condense,omitempty"`
  82. Extend string `xml:"extend,omitempty"`
  83. Color *xlsxColor `xml:"color"`
  84. Sz *attrValFloat `xml:"sz"`
  85. U *attrValString `xml:"u"`
  86. VertAlign *attrValString `xml:"vertAlign"`
  87. Scheme *attrValString `xml:"scheme"`
  88. }
  89. // RichTextRun directly maps the settings of the rich text run.
  90. type RichTextRun struct {
  91. Font *Font
  92. Text string
  93. }