Module: RubyXL::WorkbookConvenienceMethods

Included in:
Workbook
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()



21
22
23
# File 'lib/rubyXL/convenience_methods/workbook.rb', line 21

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()



9
10
11
# File 'lib/rubyXL/convenience_methods/workbook.rb', line 9

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

#define_new_name(name, reference) ⇒ Object



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

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

#eachObject



5
6
7
# File 'lib/rubyXL/convenience_methods/workbook.rb', line 5

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

#fillsObject

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



17
18
19
# File 'lib/rubyXL/convenience_methods/workbook.rb', line 17

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

#fontsObject

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



13
14
15
# File 'lib/rubyXL/convenience_methods/workbook.rb', line 13

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

#get_defined_name(name) ⇒ Object



129
130
131
# File 'lib/rubyXL/convenience_methods/workbook.rb', line 129

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

#get_fill_color(xf) ⇒ Object



25
26
27
28
29
30
31
# File 'lib/rubyXL/convenience_methods/workbook.rb', line 25

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)


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

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



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

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



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

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



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

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



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

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



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

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



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

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



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

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