Module: RubyXL::WorkbookConvenienceMethods

Defined in:
lib/rubyXL/convenience_methods/workbook.rb

Instance Method Summary collapse

Instance Method Details

#bordersObject

Stylesheet should be pre-filled with defaults on initialize()



19
20
21
# File 'lib/rubyXL/convenience_methods/workbook.rb', line 19

def borders # Stylesheet should be pre-filled with defaults on initialize()
  stylesheet.borders
end

#cell_xfsObject

Stylesheet should be pre-filled with defaults on initialize()



7
8
9
# File 'lib/rubyXL/convenience_methods/workbook.rb', line 7

def cell_xfs # Stylesheet should be pre-filled with defaults on initialize()
  stylesheet.cell_xfs
end

#define_new_name(name, reference) ⇒ Object



118
119
120
121
# File 'lib/rubyXL/convenience_methods/workbook.rb', line 118

def define_new_name(name, reference)
  self.defined_names ||= RubyXL::DefinedNames.new
  self.defined_names << RubyXL::DefinedName.new({:name => name, :reference => reference})
end

#eachObject



3
4
5
# File 'lib/rubyXL/convenience_methods/workbook.rb', line 3

def each
  worksheets.each{ |i| yield i }
end

#fillsObject

Stylesheet should be pre-filled with defaults on initialize()



15
16
17
# File 'lib/rubyXL/convenience_methods/workbook.rb', line 15

def fills # Stylesheet should be pre-filled with defaults on initialize()
  stylesheet.fills
end

#fontsObject

Stylesheet should be pre-filled with defaults on initialize()



11
12
13
# File 'lib/rubyXL/convenience_methods/workbook.rb', line 11

def fonts # Stylesheet should be pre-filled with defaults on initialize()
  stylesheet.fonts
end

#get_defined_name(name) ⇒ Object



123
124
125
# File 'lib/rubyXL/convenience_methods/workbook.rb', line 123

def get_defined_name(name)
  self.defined_names && self.defined_names.find { |n| n.name == name }
end

#get_fill_color(xf) ⇒ Object



23
24
25
# File 'lib/rubyXL/convenience_methods/workbook.rb', line 23

def get_fill_color(xf)
  fills[xf.fill_id]&.pattern_fill&.fg_color&.get_rgb(self)&.to_s || 'ffffff'
end

#modify_alignment(style_index) {|new_xf.alignment| ... } ⇒ Object

Yields:

  • (new_xf.alignment)


52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/rubyXL/convenience_methods/workbook.rb', line 52

def modify_alignment(style_index, &block)
  old_xf = cell_xfs[style_index || 0]
  new_xf = old_xf.dup
  if old_xf.alignment then
    new_xf.alignment = old_xf.alignment.dup
  else
    new_xf.alignment = RubyXL::Alignment.new
  end

  yield(new_xf.alignment)
  new_xf.apply_alignment = true

  register_new_xf(new_xf)
end

#modify_border(style_index, direction, weight) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/rubyXL/convenience_methods/workbook.rb', line 75

def modify_border(style_index, direction, weight)
  xf = cell_xfs[style_index || 0].dup
  new_border = borders[xf.border_id || 0].dup

  edge = new_border.send(direction)
  new_border.send("#{direction}=", edge.dup) if edge

  new_border.set_edge_style(direction, weight)

  xf.border_id = borders.find_index { |x| x == new_border } # Reuse existing border, if it exists
  xf.border_id ||= borders.size # If this border has never existed before, add it to collection.
  borders[xf.border_id] = new_border
  xf.apply_border = true

  register_new_xf(xf)
end

#modify_border_color(style_index, direction, color) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/rubyXL/convenience_methods/workbook.rb', line 92

def modify_border_color(style_index, direction, color)
  xf = cell_xfs[style_index || 0].dup
  new_border = borders[xf.border_id || 0].dup
  new_border.set_edge_color(direction, color)

  xf.border_id = borders.find_index { |x| x == new_border } # Reuse existing border, if it exists
  xf.border_id ||= borders.size # If this border has never existed before, add it to collection.
  borders[xf.border_id] = new_border
  xf.apply_border = true

  register_new_xf(xf)
end

#modify_fill(style_index, rgb) ⇒ Object



67
68
69
70
71
72
73
# File 'lib/rubyXL/convenience_methods/workbook.rb', line 67

def modify_fill(style_index, rgb)
  xf = cell_xfs[style_index || 0].dup
  new_fill = RubyXL::Fill.new(:pattern_fill =>
                                               RubyXL::PatternFill.new(:pattern_type => 'solid',
                                                                       :fg_color     => RubyXL::Color.new(:rgb => rgb)))
  register_new_xf(register_new_fill(new_fill, xf))
end

#password_hash(pwd) ⇒ Object

Calculate password hash from string for use in ‘password’ fields. www.openoffice.org/sc/excelfileformat.pdf



107
108
109
110
111
112
113
114
115
116
# File 'lib/rubyXL/convenience_methods/workbook.rb', line 107

def password_hash(pwd)
  hsh = 0
  pwd.reverse.each_char { |c|
    hsh = hsh ^ c.ord
    hsh = hsh << 1
    hsh -= 0x7fff if hsh > 0x7fff
  }

  (hsh ^ pwd.length ^ 0xCE4B).to_s(16)
end

#register_new_fill(new_fill, old_xf) ⇒ Object



27
28
29
30
31
32
33
34
# File 'lib/rubyXL/convenience_methods/workbook.rb', line 27

def register_new_fill(new_fill, old_xf)
  new_xf = old_xf.dup
  new_xf.apply_fill = true
  new_xf.fill_id = fills.find_index { |x| x == new_fill } # Reuse existing fill, if it exists
  new_xf.fill_id ||= fills.size # If this fill has never existed before, add it to collection.
  fills[new_xf.fill_id] = new_fill
  new_xf
end

#register_new_font(new_font, old_xf) ⇒ Object



36
37
38
39
40
41
42
43
# File 'lib/rubyXL/convenience_methods/workbook.rb', line 36

def register_new_font(new_font, old_xf)
  new_xf = old_xf.dup
  new_xf.font_id = fonts.find_index { |x| x == new_font } # Reuse existing font, if it exists
  new_xf.font_id ||= fonts.size # If this font has never existed before, add it to collection.
  fonts[new_xf.font_id] = new_font
  new_xf.apply_font = true
  new_xf
end

#register_new_xf(new_xf) ⇒ Object



45
46
47
48
49
50
# File 'lib/rubyXL/convenience_methods/workbook.rb', line 45

def register_new_xf(new_xf)
  new_xf_id = cell_xfs.find_index { |xf| xf == new_xf } # Reuse existing XF, if it exists
  new_xf_id ||= cell_xfs.size # If this XF has never existed before, add it to collection.
  cell_xfs[new_xf_id] = new_xf
  new_xf_id
end

#titleObject



127
128
129
# File 'lib/rubyXL/convenience_methods/workbook.rb', line 127

def title
  self.root.core_properties.dc_title && self.root.core_properties.dc_title.value
end

#title=(v) ⇒ Object



131
132
133
# File 'lib/rubyXL/convenience_methods/workbook.rb', line 131

def title=(v)
  self.root.core_properties.dc_title = v && RubyXL::StringNode.new(:value => v)
end