class RubyXL::Workbook
def [](ind)
def [](ind) case ind when Integer then worksheets[ind] when String then worksheets.find { |ws| ws.sheet_name == ind } end end
def add_worksheet(name = nil)
-
The
(String
) -- name for the new worksheet
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
def application
def application root.document_properties.application && root.document_properties.application.value end
def application=(v)
def application=(v) root.document_properties.application ||= StringNode.new root.document_properties.application.value = v end
def appversion
def appversion root.document_properties.app_version && root.document_properties.app_version.value end
def appversion=(v)
def appversion=(v) root.document_properties.app_version ||= StringNode.new root.document_properties.app_version.value = v end
def base_date
def base_date (workbook_properties && workbook_properties.date1904) ? DATE1904 : DATE1899 end
def before_write_xml
def before_write_xml max_sheet_id = worksheets.collect(&:sheet_id).compact.max || 0 self.sheets = RubyXL::Sheets.new worksheets.each { |sheet, i| rel = relationship_container.find_by_target(sheet.xlsx_path) raise "Worksheet name '#{sheet.sheet_name}' contains forbidden characters" if sheet.sheet_name =~ SHEET_NAME_FORBIDDEN_CHARS raise "Worksheet name '#{sheet.sheet_name}' is forbidden" if SHEET_NAME_FORBIDDEN_NAMES.include?(sheet.sheet_name) sheets << RubyXL::Sheet.new(:name => sheet.sheet_name[0..30], # Max sheet name length is 31 char :sheet_id => sheet.sheet_id || (max_sheet_id += 1), :state => sheet.state, :r_id => rel.id) } true end
def company
def company root.document_properties.company && root.document_properties.company.value end
def company=(v)
def company=(v) root.document_properties.company ||= StringNode.new root.document_properties.company.value = v end
def content_type
def content_type content_type_name = 'CONTENT_TYPE' content_type_name << '_TEMPLATE' if is_template content_type_name << '_WITH_MACROS' if macros self.class.const_get(content_type_name) end
def created_at
def created_at root.core_properties.created_at end
def created_at=(v)
def created_at=(v) root.core_properties.created_at = v end
def creator
def creator root.core_properties.creator end
def creator=(v)
def creator=(v) root.core_properties.creator = v end
def date1904
def date1904 workbook_properties && workbook_properties.date1904 end
def date1904=(v)
def date1904=(v) self.workbook_properties ||= RubyXL::WorkbookProperties.new workbook_properties.date1904 = v end
def date_to_num(date)
def date_to_num(date) case date when Date, DateTime then (date.ajd - base_date.ajd).to_f when Time then ((date.to_r - base_date.to_time.to_r) / 86400).to_f end end
def initialize(worksheets = [], src_file_path = nil, creator = nil, modifier = nil, created_at = nil,
def initialize(worksheets = [], src_file_path = nil, creator = nil, modifier = nil, created_at = nil, company = '', application = APPLICATION, appversion = APPVERSION, date1904 = 0, is_template = false) 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.source_file_path = src_file_path 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 self.is_template = is_template end
def modified_at
def modified_at root.core_properties.modified_at end
def modified_at=(v)
def modified_at=(v) root.core_properties.modified_at = v end
def modifier
def modifier root.core_properties.modifier end
def modifier=(v)
def modifier=(v) root.core_properties.modifier = v end
def num_to_date(num)
def num_to_date(num) return nil if num.nil? # Bug-for-bug Excel compatibility (https://support.microsoft.com/kb/214058/) if num < MARCH_1_1900 then num += 1 unless workbook_properties && workbook_properties.date1904 end dateparts = num.divmod(1) base_date + (dateparts[0] + (dateparts[1] * 86400).round(6) / 86400) end
def related_objects
def related_objects [ calculation_chain, stylesheet, theme, shared_strings_container, macros ] + @worksheets end
def save(dst_file_path = nil)
def save(dst_file_path = nil) dst_file_path ||= root.source_file_path extension = File.extname(dst_file_path) unless %w{.xlsx .xlsm .xltx .xltm}.include?(extension.downcase) raise "Unsupported extension: #{extension} (only .xlsx, .xlsm, .xltx and .xltm files are supported)." end File.open(dst_file_path, 'wb') { |output_file| FileUtils.copy_stream(root.stream, output_file) } return dst_file_path end
def stream
def stream root.stream end
def xlsx_path
def xlsx_path ROOT.join('xl', 'workbook.xml') end