class RubyXL::Workbook

www.schemacentral.com/sc/ooxml/e-ssml_workbook.html

Constants

APPLICATION
APPVERSION
CONTENT_TYPE
CONTENT_TYPE_MACRO
DATE1899

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

DATE1904
MARCH_1_1900
REL_TYPE

Attributes

worksheets[RW]

Public Class Methods

new(worksheets = [], filepath = nil, creator = nil, modifier = nil, created_at = nil, company = '', application = APPLICATION, appversion = APPVERSION, date1904 = 0) click to toggle source
Calls superclass method RubyXL::OOXMLObjectInstanceMethods.new
# File lib/rubyXL/objects/workbook.rb, line 423
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.default
  @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

Public Instance Methods

before_write_xml() click to toggle source
# File lib/rubyXL/objects/workbook.rb, line 357
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
content_type() click to toggle source
# File lib/rubyXL/objects/workbook.rb, line 306
def content_type
  if macros then CONTENT_TYPE_MACRO else CONTENT_TYPE end
end
date_to_num(date) click to toggle source
# File lib/rubyXL/objects/workbook.rb, line 403
def date_to_num(date)
  date && (date.ajd - base_date().ajd).to_f
end
num_to_date(num) click to toggle source
# File lib/rubyXL/objects/workbook.rb, line 407
def num_to_date(num)
  # Bug-for-bug Excel compatibility (https://support.microsoft.com/kb/214058/)
  if num && num < MARCH_1_1900 then
    num += 1 unless workbook_properties && workbook_properties.date1904
  end

  num && (base_date + num)
end
save(filepath = nil) click to toggle source

Save the resulting XLSX file to the specified location

# File lib/rubyXL/objects/workbook.rb, line 379
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
Also aliased as: write
stream() click to toggle source

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

# File lib/rubyXL/objects/workbook.rb, line 374
def stream
  root.stream
end
write(filepath = nil)
Alias for: save
xlsx_path() click to toggle source
# File lib/rubyXL/objects/workbook.rb, line 369
def xlsx_path
  ROOT.join('xl', 'workbook.xml')
end