Class: RubyXL::Workbook

Inherits:
OOXMLTopLevelObject show all
Includes:
Enumerable, RelationshipSupport, WorkbookConvenienceMethods
Defined in:
lib/rubyXL/objects/workbook.rb

Overview

Constant Summary collapse

CONTENT_TYPE =
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml'
REL_TYPE =
'http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument'
DATE1904 =
DateTime.new(1904, 1, 1)
DATE1899 =

Subtracting one day to accomodate for erroneous 1900 leap year compatibility only for 1900 based dates

DateTime.new(1899, 12, 31) - 1
APPLICATION =
'Microsoft Macintosh Excel'
APPVERSION =
'12.0000'
@@debug =
nil

Constants included from WorkbookConvenienceMethods

RubyXL::WorkbookConvenienceMethods::SHEET_NAME_TEMPLATE

Constants inherited from OOXMLTopLevelObject

OOXMLTopLevelObject::ROOT, OOXMLTopLevelObject::SAVE_ORDER

Instance Attribute Summary collapse

Attributes included from RelationshipSupport

#generic_storage, #relationship_container

Attributes inherited from OOXMLTopLevelObject

#root

Instance Method Summary collapse

Methods included from WorkbookConvenienceMethods

#[], #add_worksheet, #application, #application=, #appversion, #appversion=, #borders, #cell_xfs, #company, #company=, #created_at, #created_at=, #creator, #creator=, #date1904, #date1904=, #each, #fills, #fonts, #get_fill_color, #modified_at, #modified_at=, #modifier, #modifier=, #modify_alignment, #modify_border, #modify_fill, #modify_text_wrap, #register_new_fill, #register_new_font, #register_new_xf

Methods included from RelationshipSupport

#attach_relationship, #collect_related_objects, included, #load_relationships, #store_relationship

Methods inherited from OOXMLTopLevelObject

#add_to_zip, #file_index, parse_file, set_namespaces

Methods included from OOXMLObjectInstanceMethods

#==, included, #index_in_collection, #write_xml

Constructor Details

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

Returns a new instance of Workbook.



410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
# File 'lib/rubyXL/objects/workbook.rb', line 410

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?

  @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

  creation_time = DateTime.parse(created_at) rescue DateTime.now
  self.created_at  = creation_time
  self.modified_at = creation_time
  self.company     = company
  self.application = application
  self.appversion  = appversion
  self.creator     = creator
  self.modifier    = modifier
  self.date1904    = date1904 > 0
end

Instance Attribute Details

#worksheetsObject

Returns the value of attribute worksheets.



348
349
350
# File 'lib/rubyXL/objects/workbook.rb', line 348

def worksheets
  @worksheets
end

Instance Method Details

#before_write_xmlObject



350
351
352
353
354
355
356
357
358
359
360
# File 'lib/rubyXL/objects/workbook.rb', line 350

def before_write_xml
  self.sheets = RubyXL::Sheets.new

  worksheets.each_with_index { |sheet, i|
    rel = relationship_container.find_by_target(sheet.xlsx_path)
    sheets << RubyXL::Sheet.new(:name => sheet.sheet_name[0..30], # Max sheet name length is 31 char
                                :sheet_id => sheet.sheet_id || (i + 1),
                                :state => sheet.state, :r_id => rel.id)
  }
  true
end

#date_to_num(date) ⇒ Object



395
396
397
# File 'lib/rubyXL/objects/workbook.rb', line 395

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

#num_to_date(num) ⇒ Object



399
400
401
# File 'lib/rubyXL/objects/workbook.rb', line 399

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


305
306
307
# File 'lib/rubyXL/objects/workbook.rb', line 305

def related_objects
  [ calculation_chain, stylesheet, theme, shared_strings_container ] + @worksheets
end

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

Save the resulting XLSX file to the specified location



372
373
374
375
376
377
378
379
380
381
382
383
# File 'lib/rubyXL/objects/workbook.rb', line 372

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)



367
368
369
# File 'lib/rubyXL/objects/workbook.rb', line 367

def stream
  root.stream
end

#xlsx_pathObject



362
363
364
# File 'lib/rubyXL/objects/workbook.rb', line 362

def xlsx_path
  ROOT.join('xl', 'workbook.xml')
end