Class: Xmlss::Workbook

Inherits:
Object
  • Object
show all
Defined in:
lib/xmlss/workbook.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(writer, data = {}, &build) ⇒ Workbook

Returns a new instance of Workbook.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/xmlss/workbook.rb', line 21

def initialize(writer, data={}, &build)
  # (don't pollute workbook scope that the build may run in)

  # apply :data options to workbook scope
  if (data.keys.map(&:to_s) & self.public_methods.map(&:to_s)).size > 0
    raise ArgumentError, "data conflicts with workbook public methods."
  end
  metaclass = class << self; self; end
  data.each {|key, value| metaclass.class_eval { define_method(key){value} }}

  # setup the Undies xml writer with any :output options
  @__xmlss_writer           = writer
  @__xmlss_styles_stack     = ElementStack.new(writer, :styles)
  @__xmlss_worksheets_stack = ElementStack.new(writer, :worksheets)

  # run any instance workbook build given
  instance_eval(&build) if build
end

Class Method Details

.styles_stack(workbook) ⇒ Object



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

def self.styles_stack(workbook)
  workbook.instance_variable_get("@__xmlss_styles_stack")
end

.worksheets_stack(workbook) ⇒ Object



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

def self.worksheets_stack(workbook)
  workbook.instance_variable_get("@__xmlss_worksheets_stack")
end

.writer(workbook) ⇒ Object



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

def self.writer(workbook)
  workbook.instance_variable_get("@__xmlss_writer")
end

Instance Method Details

#alignment(*args, &block) ⇒ Object



56
57
58
# File 'lib/xmlss/workbook.rb', line 56

def alignment(*args, &block)
  self.class.styles_stack(self).using(Style::Alignment.new(*args), &block)
end

#autofit(value) ⇒ Object Also known as: auto_fit_width, auto_fit_height

column, row



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

def autofit(value)  # column, row
  self.class.worksheets_stack(self).current.autofit = value
end

#autofit?Boolean

Returns:

  • (Boolean)


137
138
139
# File 'lib/xmlss/workbook.rb', line 137

def autofit?
  self.class.worksheets_stack(self).current.autofit?
end

#border(*args, &block) ⇒ Object



64
65
66
# File 'lib/xmlss/workbook.rb', line 64

def border(*args, &block)
  self.class.styles_stack(self).using(Style::Border.new(*args), &block)
end

#borders(*args, &block) ⇒ Object



60
61
62
# File 'lib/xmlss/workbook.rb', line 60

def borders(*args, &block)
  self.class.styles_stack(self).using(Style::Borders.new(*args), &block)
end

#cell(*args, &block) ⇒ Object



98
99
100
# File 'lib/xmlss/workbook.rb', line 98

def cell(*args, &block)
  self.class.worksheets_stack(self).using(Element::Cell.new(*args), &block)
end

#column(*args, &block) ⇒ Object



90
91
92
# File 'lib/xmlss/workbook.rb', line 90

def column(*args, &block)
  self.class.worksheets_stack(self).using(Element::Column.new(*args), &block)
end

#data(value) ⇒ Object

cell



149
150
151
# File 'lib/xmlss/workbook.rb', line 149

def data(value)  # cell
  self.class.worksheets_stack(self).current.data = value
end

#font(*args, &block) ⇒ Object



68
69
70
# File 'lib/xmlss/workbook.rb', line 68

def font(*args, &block)
  self.class.styles_stack(self).using(Style::Font.new(*args), &block)
end

#formula(value) ⇒ Object

cell



157
158
159
# File 'lib/xmlss/workbook.rb', line 157

def formula(value)  # cell
  self.class.worksheets_stack(self).current.formula = value
end

#height(value) ⇒ Object

row



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

def height(value)  # row
  self.class.worksheets_stack(self).current.height = value
end

#hidden(value) ⇒ Object

row, column



141
142
143
# File 'lib/xmlss/workbook.rb', line 141

def hidden(value)  # row, column
  self.class.worksheets_stack(self).current.hidden = value
end

#hidden?Boolean

Returns:

  • (Boolean)


145
146
147
# File 'lib/xmlss/workbook.rb', line 145

def hidden?
  self.class.worksheets_stack(self).current.hidden?
end

#href(value) ⇒ Object

cell



153
154
155
# File 'lib/xmlss/workbook.rb', line 153

def href(value)  # cell
  self.class.worksheets_stack(self).current.href = value
end

#index(value) ⇒ Object

cell



161
162
163
# File 'lib/xmlss/workbook.rb', line 161

def index(value)  # cell
  self.class.worksheets_stack(self).current.index = value
end

#interior(*args, &block) ⇒ Object



72
73
74
# File 'lib/xmlss/workbook.rb', line 72

def interior(*args, &block)
  self.class.styles_stack(self).using(Style::Interior.new(*args), &block)
end

#merge_across(value) ⇒ Object

Workbook element attributes API



104
105
106
# File 'lib/xmlss/workbook.rb', line 104

def merge_across(value)  # cell
  self.class.worksheets_stack(self).current.merge_across = value
end

#merge_down(value) ⇒ Object

cell



108
109
110
# File 'lib/xmlss/workbook.rb', line 108

def merge_down(value)  # cell
  self.class.worksheets_stack(self).current.merge_down = value
end

#name(value) ⇒ Object

worksheet



112
113
114
# File 'lib/xmlss/workbook.rb', line 112

def name(value)  # worksheet
  self.class.worksheets_stack(self).current.name = value
end

#number_format(*args, &block) ⇒ Object



76
77
78
# File 'lib/xmlss/workbook.rb', line 76

def number_format(*args, &block)
  self.class.styles_stack(self).using(Style::NumberFormat.new(*args), &block)
end

#protection(*args, &block) ⇒ Object



80
81
82
# File 'lib/xmlss/workbook.rb', line 80

def protection(*args, &block)
  self.class.styles_stack(self).using(Style::Protection.new(*args), &block)
end

#row(*args, &block) ⇒ Object



94
95
96
# File 'lib/xmlss/workbook.rb', line 94

def row(*args, &block)
  self.class.worksheets_stack(self).using(Element::Row.new(*args), &block)
end

#style(*args, &block) ⇒ Object

Workbook styles API



52
53
54
# File 'lib/xmlss/workbook.rb', line 52

def style(*args, &block)
  self.class.styles_stack(self).using(Style::Base.new(*args), &block)
end

#style_id(value) ⇒ Object

cell, row, column



116
117
118
119
120
121
# File 'lib/xmlss/workbook.rb', line 116

def style_id(value)  # cell, row, column
  # check to make sure there is a current on this one
  if (c = self.class.worksheets_stack(self).current)
    c.style_id = value
  end
end

#to_file(path) ⇒ Object



44
45
46
47
48
# File 'lib/xmlss/workbook.rb', line 44

def to_file(path)
  FileUtils.mkdir_p(File.dirname(path))
  File.open(path, 'w') { |f| f.write self.to_s }
  File.exists?(path) ? path : false
end

#to_sObject



40
41
42
# File 'lib/xmlss/workbook.rb', line 40

def to_s
  self.class.writer(self).workbook
end

#to_str(*args) ⇒ Object Also known as: inspect

overriding to make less noisy



166
167
168
169
170
# File 'lib/xmlss/workbook.rb', line 166

def to_str(*args)
  "#<Xmlss::Workbook:#{self.object_id} " +
  "current_element=#{self.class.worksheets_stack(self).current.inspect}, " +
  "current_style=#{self.class.styles_stack(self).current.inspect}>"
end

#width(value) ⇒ Object

column



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

def width(value)  # column
  self.class.worksheets_stack(self).current.width = value
end

#worksheet(*args, &block) ⇒ Object

Workbook elements API



86
87
88
# File 'lib/xmlss/workbook.rb', line 86

def worksheet(*args, &block)
  self.class.worksheets_stack(self).using(Element::Worksheet.new(*args), &block)
end