Module: RubyXL::LegacyWorkbook

Includes:
Enumerable
Included in:
Workbook
Defined in:
lib/rubyXL/workbook.rb

Constant Summary collapse

SHEET_NAME_TEMPLATE =
'Sheet%d'
APPLICATION =
'Microsoft Macintosh Excel'
APPVERSION =
'12.0000'
@@debug =
nil

Instance Method Summary collapse

Instance Method Details

#[](ind) ⇒ Object

Finds worksheet by its name or numerical index



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

def [](ind)
  case ind
  when Integer then worksheets[ind]
  when String  then worksheets.find { |ws| ws.sheet_name == ind }
  end
end

#add_worksheet(name = nil) ⇒ Object

Create new simple worksheet and add it to the workbook worksheets

Parameters:

  • The (String)

    name for the new worksheet



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

def add_worksheet(name = nil)
  if name.nil? then
    n = 0

    begin
      name = SHEET_NAME_TEMPLATE % (n += 1)
    end until self[name].nil?
  end

  new_worksheet = Worksheet.new(:workbook => self, :sheet_name => name)
  worksheets << new_worksheet
  new_worksheet
end

#bordersObject

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



198
199
200
# File 'lib/rubyXL/workbook.rb', line 198

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



186
187
188
# File 'lib/rubyXL/workbook.rb', line 186

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

#date_to_num(date) ⇒ Object



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

def date_to_num(date)
  date && (date.ajd - base_date().ajd).to_f
end

#eachObject



74
75
76
# File 'lib/rubyXL/workbook.rb', line 74

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

#fillsObject

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



194
195
196
# File 'lib/rubyXL/workbook.rb', line 194

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

#fontsObject

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



190
191
192
# File 'lib/rubyXL/workbook.rb', line 190

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

#get_fill_color(xf) ⇒ Object



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

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

#initialize(worksheets = [], filepath = nil, creator = nil, modifier = nil, created_at = nil, company = '', application = APPLICATION, appversion = APPVERSION, date1904 = 0) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/rubyXL/workbook.rb', line 14

def initialize(worksheets=[], filepath=nil, creator=nil, modifier=nil, created_at=nil,
               company='', application=APPLICATION,
               appversion=APPVERSION, date1904=0)
  super()

  # Order of sheets in the +worksheets+ array corresponds to the order of pages in Excel UI.
  # SheetId's, rId's, etc. are completely unrelated to ordering.
  @worksheets = worksheets
  add_worksheet if @worksheets.empty?

  @creator             = creator
  @modifier            = modifier
  self.date1904        = date1904 > 0

  @theme                    = RubyXL::Theme.defaults
  @shared_strings_container = RubyXL::SharedStringsTable.new
  @stylesheet               = RubyXL::Stylesheet.default
  @relationship_container   = RubyXL::OOXMLRelationshipsFile.new
  @root                     = RubyXL::WorkbookRoot.default
  @root.workbook            = self
  @root.filepath            = filepath
  @comments                 = []

  self.company         = company
  self.application     = application
  self.appversion      = appversion

  begin
    @created_at       = DateTime.parse(created_at).strftime('%Y-%m-%dT%TZ')
  rescue
    @created_at       = Time.now.strftime('%Y-%m-%dT%TZ')
  end
  @modified_at        = @created_at
end

#modify_alignment(style_index, is_horizontal, alignment) ⇒ Object



154
155
156
157
158
159
160
# File 'lib/rubyXL/workbook.rb', line 154

def modify_alignment(style_index, is_horizontal, alignment)
  xf = cell_xfs[style_index].dup
  xf.apply_alignment = true
  xf.alignment = RubyXL::Alignment.new(:horizontal => is_horizontal ? alignment : nil,
                                       :vertical   => is_horizontal ? nil : alignment)
  register_new_xf(xf, style_index)
end

#modify_border(style_index, direction, weight) ⇒ Object



171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/rubyXL/workbook.rb', line 171

def modify_border(style_index, direction, weight)
  old_xf = cell_xfs[style_index].dup
  new_border = borders[old_xf.border_id].dup
  new_border.set_edge_style(direction, weight)

  new_xf = old_xf.dup
  new_xf.apply_border = true

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

  register_new_xf(new_xf, style_index)
end

#modify_fill(style_index, rgb) ⇒ Object



162
163
164
165
166
167
168
169
# File 'lib/rubyXL/workbook.rb', line 162

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

#modify_text_wrap(style_index, wrap = false) ⇒ Object



148
149
150
151
152
# File 'lib/rubyXL/workbook.rb', line 148

def modify_text_wrap(style_index, wrap = false)
  xf = cell_xfs[style_index].dup
  xf.alignment = RubyXL::Alignment.new(:wrap_text => wrap, :apply_alignment => true)
  register_new_xf(xf, style_index)
end

#num_to_date(num) ⇒ Object



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

def num_to_date(num)
  num && (base_date + num)
end

#register_new_fill(new_fill, old_xf) ⇒ Object



123
124
125
126
127
128
129
130
# File 'lib/rubyXL/workbook.rb', line 123

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 } # Use 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



132
133
134
135
136
137
138
139
# File 'lib/rubyXL/workbook.rb', line 132

def register_new_font(new_font, old_xf)
  new_xf = old_xf.dup
  new_xf.apply_font = true
  new_xf.font_id = fonts.find_index { |x| x == new_font } # Use 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
end

#register_new_xf(new_xf, old_style_index) ⇒ Object



141
142
143
144
145
146
# File 'lib/rubyXL/workbook.rb', line 141

def register_new_xf(new_xf, old_style_index)
  new_xf_id = cell_xfs.find_index { |xf| xf == new_xf } # Use 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

#save(filepath = nil) ⇒ Object Also known as: write

Save the resulting XLSX file to the specified location



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

def save(filepath = nil)
  filepath ||= root.filepath

  extension = File.extname(filepath)
  unless %w{.xlsx .xlsm}.include?(extension.downcase)
    raise "Unsupported extension: #{extension} (only .xlsx and .xlsm files are supported)."
  end

  File.open(filepath, "wb") { |output_file| FileUtils.copy_stream(root.stream, output_file) }

  return filepath
end

#streamObject

Return the resulting XLSX file in a stream (useful for sending over HTTP)



79
80
81
# File 'lib/rubyXL/workbook.rb', line 79

def stream
  root.stream
end