Class: XlsxWriter::RichString

Inherits:
Object
  • Object
show all
Defined in:
lib/xlsxwriter/rich_string.rb,
ext/xlsxwriter/rich_string.c

Overview

RichString is a multipart string consting of sub-strings with format

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(workbook, parts = []) ⇒ RichString

call-seq:

RichString.new(workbook, parts)
RichString.new(workbook)

Creates rich string that could be written to the workbook. parts is an array of pairs string + format. If no format is needed for the part single string can be specified instead. For example: [‘This is ’, [‘bold’, :bold], [‘ and this is’], [‘italic’, :italic]]

If parts is not specified it could be populated later with #add_part.

Please note that on writing additional validations are applied (e.g. string should not be empty). Please consult libxlsxwriter documentation for additional details.



23
24
25
26
27
28
# File 'lib/xlsxwriter/rich_string.rb', line 23

def initialize(workbook, parts = [])
  @workbook = workbook
  @parts = parts.map! do |(str, format)|
    make_part(str, format)
  end
end

Instance Attribute Details

#partsObject (readonly)

Returns the value of attribute parts.



7
8
9
# File 'lib/xlsxwriter/rich_string.rb', line 7

def parts
  @parts
end

#workbookObject (readonly)

Returns the value of attribute workbook.



7
8
9
# File 'lib/xlsxwriter/rich_string.rb', line 7

def workbook
  @workbook
end

Instance Method Details

#<<(part) ⇒ Object



46
47
48
# File 'lib/xlsxwriter/rich_string.rb', line 46

def <<(part)
  add_part(*Array(part))
end

#add_part(str, format = nil) ⇒ Object

call-seq:

rs.add_part(str)
rs.add_part(str, format)

Adds part (string str formatted with format) to the RichString rs.

Please note, that no parts can be added to a rich string that has already been written once.

Raises:



38
39
40
41
42
43
44
# File 'lib/xlsxwriter/rich_string.rb', line 38

def add_part(str, format = nil)
  raise Error, 'Cannot modify as the RichString has already been written to a workbook.' if cached?

  parts << make_part(str, format)

  self
end

#inspectObject



50
51
52
# File 'lib/xlsxwriter/rich_string.rb', line 50

def inspect
  "#<#{self.class}:#{to_s.inspect}, @cached=#{@cached}>"
end

#to_sObject



54
55
56
# File 'lib/xlsxwriter/rich_string.rb', line 54

def to_s
  parts.map(&:first)
end