class Axlsx::RichTextRun

Constants

INLINE_STYLES

Attributes

b[R]

The inline bold property for the cell @return [Boolean]

cell[RW]
charset[R]

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 @return [String]

color[R]

The inline color property for the cell @return [Color]

condense[R]

The inline condense property for the cell @return [Boolean]

extend[R]

The inline extend property for the cell @return [Boolean]

family[R]

The inline family property for the cell @return [Integer] 1 Roman 2 Swiss 3 Modern 4 Script 5 Decorative

font_name[R]

The inline font_name property for the cell @return [String]

i[R]

The inline italic property for the cell @return [Boolean]

outline[R]

The inline outline property for the cell @return [Boolean]

scheme[R]

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

shadow[R]

The inline shadow property for the cell @return [Boolean]

ssti[R]

The Shared Strings Table index for this cell @return [Integer]

strike[R]

The inline strike property for the cell @return [Boolean]

sz[R]

The inline sz property for the cell @return [Inteter]

u[R]

The inline underline property for the cell. It must be one of :none, :single, :double, :singleAccounting, :doubleAccounting, true @return [Boolean] @return [String] @note true is for backwards compatability and is reassigned to :single

value[R]
vertAlign[R]

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

Public Class Methods

new(value, options={}) click to toggle source
# File lib/axlsx/workbook/worksheet/rich_text_run.rb, line 13
def initialize(value, options={})
  self.value = value
  parse_options(options) 
end

Public Instance Methods

autowidth(widtharray) click to toggle source
# File lib/axlsx/workbook/worksheet/rich_text_run.rb, line 170
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
b=(v) click to toggle source

@see b

# File lib/axlsx/workbook/worksheet/rich_text_run.rb, line 73
def b=(v) set_run_style :validate_boolean, :b, v; end
charset=(v) click to toggle source

@see charset

# File lib/axlsx/workbook/worksheet/rich_text_run.rb, line 54
def charset=(v) set_run_style :validate_unsigned_int, :charset, v; end
color=(v) click to toggle source

@param [String] v The 8 character representation for an rgb color FFFFFFFF“

# File lib/axlsx/workbook/worksheet/rich_text_run.rb, line 127
def color=(v)
  @color = v.is_a?(Color) ? v : Color.new(:rgb=>v)
end
condense=(v) click to toggle source

@see condense

# File lib/axlsx/workbook/worksheet/rich_text_run.rb, line 103
def condense=(v) set_run_style :validate_boolean, :condense, v; end
extend=(v) click to toggle source

@see extend

# File lib/axlsx/workbook/worksheet/rich_text_run.rb, line 109
def extend=(v) set_run_style :validate_boolean, :extend, v; end
family=(v) click to toggle source

@see family

# File lib/axlsx/workbook/worksheet/rich_text_run.rb, line 65
def family=(v)
  set_run_style :validate_family, :family, v.to_i
end
font_name=(v) click to toggle source

@see font_name

# File lib/axlsx/workbook/worksheet/rich_text_run.rb, line 28
def font_name=(v) set_run_style :validate_string, :font_name, v; end
i=(v) click to toggle source

@see i

# File lib/axlsx/workbook/worksheet/rich_text_run.rb, line 79
def i=(v) set_run_style :validate_boolean, :i, v; end
outline=(v) click to toggle source

@see outline

# File lib/axlsx/workbook/worksheet/rich_text_run.rb, line 91
def outline=(v) set_run_style :validate_boolean, :outline, v; end
scheme=(v) click to toggle source

@see scheme

# File lib/axlsx/workbook/worksheet/rich_text_run.rb, line 152
def scheme=(v)
  RestrictionValidator.validate :cell_scheme, [:none, :major, :minor], v
  set_run_style nil, :scheme, v
end
set_run_style(validator, attr, value) click to toggle source

Utility method for setting inline style attributes

# File lib/axlsx/workbook/worksheet/rich_text_run.rb, line 189
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
shadow=(v) click to toggle source

@see shadow

# File lib/axlsx/workbook/worksheet/rich_text_run.rb, line 97
def shadow=(v) set_run_style :validate_boolean, :shadow, v; end
strike=(v) click to toggle source

@see strike

# File lib/axlsx/workbook/worksheet/rich_text_run.rb, line 85
def strike=(v) set_run_style :validate_boolean, :strike, v; end
style=(v) click to toggle source

@return [Integer] The cellXfs item index applied to this cell. @raise [ArgumentError] Invalid cellXfs id if the value provided is not within cellXfs items range.

# File lib/axlsx/workbook/worksheet/rich_text_run.rb, line 163
def style=(v)
  Axlsx::validate_unsigned_int(v)
  count = styles.cellXfs.size
  raise ArgumentError, "Invalid cellXfs id" unless v < count
  @style = v
end
sz=(v) click to toggle source

@see sz

# File lib/axlsx/workbook/worksheet/rich_text_run.rb, line 135
def sz=(v) set_run_style :validate_unsigned_int, :sz, v; end
to_xml_string(str = '') click to toggle source
# File lib/axlsx/workbook/worksheet/rich_text_run.rb, line 195
def to_xml_string(str = '')
  valid = RichTextRun::INLINE_STYLES
  data = Hash[self.instance_values.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
u=(v) click to toggle source

@see u

# File lib/axlsx/workbook/worksheet/rich_text_run.rb, line 118
def u=(v)
  v = :single if (v == true || v == 1 || v == :true || v == 'true')
  set_run_style :validate_cell_u, :u, v
end
value=(value) click to toggle source
# File lib/axlsx/workbook/worksheet/rich_text_run.rb, line 18
def value=(value)
  @value = value
end
vertAlign=(v) click to toggle source

@see vertAlign

# File lib/axlsx/workbook/worksheet/rich_text_run.rb, line 142
def vertAlign=(v)
  RestrictionValidator.validate :cell_vertAlign, [:baseline, :subscript, :superscript], v
  set_run_style nil, :vertAlign, v
end

Private Instance Methods

font_size() click to toggle source

we scale the font size if bold style is applied to either the style font or the cell itself. Yes, it is a bit of a hack, but it is much better than using imagemagick and loading metrics for every character.

# File lib/axlsx/workbook/worksheet/rich_text_run.rb, line 228
def font_size
  return sz if sz
  font = styles.fonts[styles.cellXfs[style].fontId] || styles.fonts[0]
  (font.b || (defined?(@b) && @b)) ? (font.sz * 1.5) : font.sz
end
string_width(string, font_size) click to toggle source

Returns the width of a string according to the current style This is still not perfect…

- scaling is not linear as font sizes increase
# File lib/axlsx/workbook/worksheet/rich_text_run.rb, line 220
def string_width(string, font_size)
  font_scale = font_size / 10.0
  string.count(Worksheet::THIN_CHARS) * font_scale
end
style() click to toggle source
# File lib/axlsx/workbook/worksheet/rich_text_run.rb, line 234
def style
  cell.style
end
styles() click to toggle source
# File lib/axlsx/workbook/worksheet/rich_text_run.rb, line 238
def styles
  cell.row.worksheet.styles 
end
xml_value(value) click to toggle source

Converts the value to the correct XML representation (fixes issues with Numbers)

# File lib/axlsx/workbook/worksheet/rich_text_run.rb, line 244
def xml_value value
  if value == true
    1
  elsif value == false
    0
  else
    value
  end.to_s
end