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



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

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



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

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
26
27
28
29
# File 'lib/rubyXL/convenience_methods/workbook.rb', line 23

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

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

Yields:

  • (new_xf.alignment)


56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/rubyXL/convenience_methods/workbook.rb', line 56

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



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/rubyXL/convenience_methods/workbook.rb', line 79

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



96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/rubyXL/convenience_methods/workbook.rb', line 96

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



71
72
73
74
75
76
77
# File 'lib/rubyXL/convenience_methods/workbook.rb', line 71

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



111
112
113
114
115
116
117
118
119
120
# File 'lib/rubyXL/convenience_methods/workbook.rb', line 111

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



31
32
33
34
35
36
37
38
# File 'lib/rubyXL/convenience_methods/workbook.rb', line 31

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



40
41
42
43
44
45
46
47
# File 'lib/rubyXL/convenience_methods/workbook.rb', line 40

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



49
50
51
52
53
54
# File 'lib/rubyXL/convenience_methods/workbook.rb', line 49

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



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

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

#title=(v) ⇒ Object



135
136
137
# File 'lib/rubyXL/convenience_methods/workbook.rb', line 135

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