Class: Axlsx::RichTextRun

Inherits:
Object
  • Object
show all
Includes:
OptionsParser
Defined in:
lib/axlsx/workbook/worksheet/rich_text_run.rb

Overview

The RichTextRun class creates and self serializing text run.

Constant Summary collapse

INLINE_STYLES =

A list of allowed inline style attributes used for validation

[:font_name, :charset,
:family, :b, :i, :strike, :outline,
:shadow, :condense, :extend, :u,
:vertAlign, :sz, :color, :scheme].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from OptionsParser

#parse_options

Constructor Details

#initialize(value, options = {}) ⇒ RichTextRun

Returns a new instance of RichTextRun.



14
15
16
17
# File 'lib/axlsx/workbook/worksheet/rich_text_run.rb', line 14

def initialize(value, options = {})
  self.value = value
  parse_options(options)
end

Instance Attribute Details

#bBoolean

The inline bold property for the cell

Returns:

  • (Boolean)


75
76
77
# File 'lib/axlsx/workbook/worksheet/rich_text_run.rb', line 75

def b
  @b
end

#cellObject

Returns the value of attribute cell.



23
24
25
# File 'lib/axlsx/workbook/worksheet/rich_text_run.rb', line 23

def cell
  @cell
end

#charsetString

The inline charset property for the cell As far as I can tell, this is pretty much ignored. However, based on the spec it should be one of the following: 0  ANSI_CHARSET 1 DEFAULT_CHARSET 2 SYMBOL_CHARSET 77 MAC_CHARSET 128 SHIFTJIS_CHARSET 129  HANGUL_CHARSET 130  JOHAB_CHARSET 134  GB2312_CHARSET 136  CHINESEBIG5_CHARSET 161  GREEK_CHARSET 162  TURKISH_CHARSET 163  VIETNAMESE_CHARSET 177  HEBREW_CHARSET 178  ARABIC_CHARSET 186  BALTIC_CHARSET 204  RUSSIAN_CHARSET 222  THAI_CHARSET 238  EASTEUROPE_CHARSET 255  OEM_CHARSET

Returns:

  • (String)


54
55
56
# File 'lib/axlsx/workbook/worksheet/rich_text_run.rb', line 54

def charset
  @charset
end

#colorColor

The inline color property for the cell

Returns:



137
138
139
# File 'lib/axlsx/workbook/worksheet/rich_text_run.rb', line 137

def color
  @color
end

#condenseBoolean

The inline condense property for the cell

Returns:

  • (Boolean)


110
111
112
# File 'lib/axlsx/workbook/worksheet/rich_text_run.rb', line 110

def condense
  @condense
end

#extendBoolean

The inline extend property for the cell

Returns:

  • (Boolean)


117
118
119
# File 'lib/axlsx/workbook/worksheet/rich_text_run.rb', line 117

def extend
  @extend
end

#familyInteger

The inline family property for the cell 1 Roman 2 Swiss 3 Modern 4 Script 5 Decorative

Returns:

  • (Integer)


66
67
68
# File 'lib/axlsx/workbook/worksheet/rich_text_run.rb', line 66

def family
  @family
end

#font_nameString

The inline font_name property for the cell

Returns:

  • (String)


27
28
29
# File 'lib/axlsx/workbook/worksheet/rich_text_run.rb', line 27

def font_name
  @font_name
end

#iBoolean

The inline italic property for the cell

Returns:

  • (Boolean)


82
83
84
# File 'lib/axlsx/workbook/worksheet/rich_text_run.rb', line 82

def i
  @i
end

#outlineBoolean

The inline outline property for the cell

Returns:

  • (Boolean)


96
97
98
# File 'lib/axlsx/workbook/worksheet/rich_text_run.rb', line 96

def outline
  @outline
end

#schemeSymbol

The inline scheme property for the cell this must be one of [:none, major, minor]

Returns:

  • (Symbol)


165
166
167
# File 'lib/axlsx/workbook/worksheet/rich_text_run.rb', line 165

def scheme
  @scheme
end

#shadowBoolean

The inline shadow property for the cell

Returns:

  • (Boolean)


103
104
105
# File 'lib/axlsx/workbook/worksheet/rich_text_run.rb', line 103

def shadow
  @shadow
end

#strikeBoolean

The inline strike property for the cell

Returns:

  • (Boolean)


89
90
91
# File 'lib/axlsx/workbook/worksheet/rich_text_run.rb', line 89

def strike
  @strike
end

#szInteter

The inline sz property for the cell

Returns:

  • (Inteter)


146
147
148
# File 'lib/axlsx/workbook/worksheet/rich_text_run.rb', line 146

def sz
  @sz
end

#uBoolean, String

Note:

true is for backwards compatability and is reassigned to :single

The inline underline property for the cell. It must be one of :none, :single, :double, :singleAccounting, :doubleAccounting, true

Returns:

  • (Boolean)
  • (String)


127
128
129
# File 'lib/axlsx/workbook/worksheet/rich_text_run.rb', line 127

def u
  @u
end

#valueObject

Returns the value of attribute value.



6
7
8
# File 'lib/axlsx/workbook/worksheet/rich_text_run.rb', line 6

def value
  @value
end

#vertAlignSymbol

The inline vertical alignment property for the cell this must be one of [:baseline, :subscript, :superscript]

Returns:

  • (Symbol)


154
155
156
# File 'lib/axlsx/workbook/worksheet/rich_text_run.rb', line 154

def vertAlign
  @vertAlign
end

Instance Method Details

#autowidth(widtharray) ⇒ Array

Tries to work out the width of the longest line in the run

Parameters:

  • widtharray (Array)

    this array is populated with the widths of each line in the run.

Returns:

  • (Array)


176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/axlsx/workbook/worksheet/rich_text_run.rb', line 176

def autowidth(widtharray)
  return if value.nil?

  if styles.cellXfs[style].alignment && styles.cellXfs[style].alignment.wrap_text
    first = true
    value.to_s.split(/\r?\n/, -1).each do |line|
      if first
        first = false
      else
        widtharray << 0
      end
      widtharray[-1] += string_width(line, font_size)
    end
  else
    widtharray[-1] += string_width(value.to_s, font_size)
  end
  widtharray
end

#set_run_style(validator, attr, value) ⇒ Object

Utility method for setting inline style attributes



196
197
198
199
200
201
# File 'lib/axlsx/workbook/worksheet/rich_text_run.rb', line 196

def set_run_style(validator, attr, value)
  return unless INLINE_STYLES.include?(attr.to_sym)

  Axlsx.send(validator, value) unless validator.nil?
  self.instance_variable_set :"@#{attr.to_s}", value
end

#to_xml_string(str = '') ⇒ String

Serializes the RichTextRun

Parameters:

  • str (String) (defaults to: '')

Returns:

  • (String)


206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/axlsx/workbook/worksheet/rich_text_run.rb', line 206

def to_xml_string(str = '')
  valid = RichTextRun::INLINE_STYLES
  data = Hash[Axlsx.instance_values_for(self).map { |k, v| [k.to_sym, v] }]
  data = data.select { |key, value| valid.include?(key) && !value.nil? }

  str << '<r><rPr>'
  data.keys.each do |key|
    case key
    when :font_name
      str << ('<rFont val="' << font_name << '"/>')
    when :color
      str << data[key].to_xml_string
    else
      str << ('<' << key.to_s << ' val="' << xml_value(data[key]) << '"/>')
    end
  end
  clean_value = Axlsx::trust_input ? @value.to_s : ::CGI.escapeHTML(Axlsx::sanitize(@value.to_s))
  str << ('</rPr><t>' << clean_value << '</t></r>')
end