xmlSharedStrings.go 3.7 KB

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