class RubyXL::Workbook
def base_date
def base_date (workbook_properties && workbook_properties.date1904) ? DATE1904 : DATE1899 end
def before_write_xml
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
def content_type
def content_type if macros then CONTENT_TYPE_MACRO else CONTENT_TYPE end end
def date_to_num(date)
def date_to_num(date) date && (date.ajd - base_date().ajd).to_f end
def initialize(worksheets = [], filepath = nil, creator = nil, modifier = nil, created_at = nil,
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
def num_to_date(num)
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
def related_objects
def related_objects [ calculation_chain, stylesheet, theme, shared_strings_container, macros ] + @worksheets end
def save(filepath = nil)
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
def stream
def stream root.stream end
def xlsx_path
def xlsx_path ROOT.join('xl', 'workbook.xml') end