Module: RubyXL::LegacyWorkbook
Constant Summary collapse
- SHEET_NAME_TEMPLATE =
'Sheet%d'- APPLICATION =
'Microsoft Macintosh Excel'- APPVERSION =
'12.0000'
Instance Attribute Summary collapse
-
#calculation_chain ⇒ Object
Returns the value of attribute calculation_chain.
-
#chart_rels ⇒ Object
Returns the value of attribute chart_rels.
-
#charts ⇒ Object
Returns the value of attribute charts.
-
#chartsheet_rels ⇒ Object
Returns the value of attribute chartsheet_rels.
-
#created_at ⇒ Object
Returns the value of attribute created_at.
-
#creator ⇒ Object
Returns the value of attribute creator.
-
#document_properties ⇒ Object
Returns the value of attribute document_properties.
-
#drawings ⇒ Object
Returns the value of attribute drawings.
-
#drawings_rels ⇒ Object
Returns the value of attribute drawings_rels.
-
#external_links ⇒ Object
Returns the value of attribute external_links.
-
#external_links_rels ⇒ Object
Returns the value of attribute external_links_rels.
-
#filepath ⇒ Object
Returns the value of attribute filepath.
-
#macros ⇒ Object
Returns the value of attribute macros.
-
#media ⇒ Object
Returns the value of attribute media.
-
#modified_at ⇒ Object
Returns the value of attribute modified_at.
-
#modifier ⇒ Object
Returns the value of attribute modifier.
-
#printer_settings ⇒ Object
Returns the value of attribute printer_settings.
-
#relationship_container ⇒ Object
Returns the value of attribute relationship_container.
-
#root_relationship_container ⇒ Object
Returns the value of attribute root_relationship_container.
-
#shared_strings_container ⇒ Object
Returns the value of attribute shared_strings_container.
-
#stylesheet ⇒ Object
Returns the value of attribute stylesheet.
-
#theme ⇒ Object
Returns the value of attribute theme.
-
#worksheet_rels ⇒ Object
Returns the value of attribute worksheet_rels.
-
#worksheets ⇒ Object
Returns the value of attribute worksheets.
Instance Method Summary collapse
-
#[](ind) ⇒ Object
Finds worksheet by its name or numerical index.
-
#add_worksheet(name = nil) ⇒ Object
Create new simple worksheet and add it to the workbook worksheets.
-
#borders ⇒ Object
Stylesheet should be pre-filled with defaults on initialize().
-
#cell_xfs ⇒ Object
Stylesheet should be pre-filled with defaults on initialize().
- #date_to_num(date) ⇒ Object
- #each ⇒ Object
-
#fills ⇒ Object
Stylesheet should be pre-filled with defaults on initialize().
-
#fonts ⇒ Object
Stylesheet should be pre-filled with defaults on initialize().
- #get_fill_color(xf) ⇒ Object
- #initialize(worksheets = [], filepath = nil, creator = nil, modifier = nil, created_at = nil, company = '', application = APPLICATION, appversion = APPVERSION, date1904 = 0) ⇒ Object
- #modify_alignment(style_index, is_horizontal, alignment) ⇒ Object
- #modify_border(style_index, direction, weight) ⇒ Object
- #modify_fill(style_index, rgb) ⇒ Object
- #modify_text_wrap(style_index, wrap = false) ⇒ Object
- #num_to_date(num) ⇒ Object
- #register_new_fill(new_fill, old_xf) ⇒ Object
- #register_new_font(new_font, old_xf) ⇒ Object
- #register_new_xf(new_xf, old_style_index) ⇒ Object
-
#write(filepath = @filepath) ⇒ Object
filepath of xlsx file (including file itself).
Instance Attribute Details
#calculation_chain ⇒ Object
Returns the value of attribute calculation_chain.
17 18 19 |
# File 'lib/rubyXL/workbook.rb', line 17 def calculation_chain @calculation_chain end |
#chart_rels ⇒ Object
Returns the value of attribute chart_rels.
13 14 15 |
# File 'lib/rubyXL/workbook.rb', line 13 def chart_rels @chart_rels end |
#charts ⇒ Object
Returns the value of attribute charts.
13 14 15 |
# File 'lib/rubyXL/workbook.rb', line 13 def charts @charts end |
#chartsheet_rels ⇒ Object
Returns the value of attribute chartsheet_rels.
13 14 15 |
# File 'lib/rubyXL/workbook.rb', line 13 def chartsheet_rels @chartsheet_rels end |
#created_at ⇒ Object
Returns the value of attribute created_at.
13 14 15 |
# File 'lib/rubyXL/workbook.rb', line 13 def created_at @created_at end |
#creator ⇒ Object
Returns the value of attribute creator.
13 14 15 |
# File 'lib/rubyXL/workbook.rb', line 13 def creator @creator end |
#document_properties ⇒ Object
Returns the value of attribute document_properties.
17 18 19 |
# File 'lib/rubyXL/workbook.rb', line 17 def document_properties @document_properties end |
#drawings ⇒ Object
Returns the value of attribute drawings.
13 14 15 |
# File 'lib/rubyXL/workbook.rb', line 13 def drawings @drawings end |
#drawings_rels ⇒ Object
Returns the value of attribute drawings_rels.
13 14 15 |
# File 'lib/rubyXL/workbook.rb', line 13 def drawings_rels @drawings_rels end |
#external_links ⇒ Object
Returns the value of attribute external_links.
13 14 15 |
# File 'lib/rubyXL/workbook.rb', line 13 def external_links @external_links end |
#external_links_rels ⇒ Object
Returns the value of attribute external_links_rels.
13 14 15 |
# File 'lib/rubyXL/workbook.rb', line 13 def external_links_rels @external_links_rels end |
#filepath ⇒ Object
Returns the value of attribute filepath.
13 14 15 |
# File 'lib/rubyXL/workbook.rb', line 13 def filepath @filepath end |
#macros ⇒ Object
Returns the value of attribute macros.
13 14 15 |
# File 'lib/rubyXL/workbook.rb', line 13 def macros @macros end |
#media ⇒ Object
Returns the value of attribute media.
13 14 15 |
# File 'lib/rubyXL/workbook.rb', line 13 def media @media end |
#modified_at ⇒ Object
Returns the value of attribute modified_at.
13 14 15 |
# File 'lib/rubyXL/workbook.rb', line 13 def modified_at @modified_at end |
#modifier ⇒ Object
Returns the value of attribute modifier.
13 14 15 |
# File 'lib/rubyXL/workbook.rb', line 13 def modifier @modifier end |
#printer_settings ⇒ Object
Returns the value of attribute printer_settings.
13 14 15 |
# File 'lib/rubyXL/workbook.rb', line 13 def printer_settings @printer_settings end |
#relationship_container ⇒ Object
Returns the value of attribute relationship_container.
17 18 19 |
# File 'lib/rubyXL/workbook.rb', line 17 def relationship_container @relationship_container end |
#root_relationship_container ⇒ Object
Returns the value of attribute root_relationship_container.
17 18 19 |
# File 'lib/rubyXL/workbook.rb', line 17 def root_relationship_container @root_relationship_container end |
#shared_strings_container ⇒ Object
Returns the value of attribute shared_strings_container.
17 18 19 |
# File 'lib/rubyXL/workbook.rb', line 17 def shared_strings_container @shared_strings_container end |
#stylesheet ⇒ Object
Returns the value of attribute stylesheet.
17 18 19 |
# File 'lib/rubyXL/workbook.rb', line 17 def stylesheet @stylesheet end |
#theme ⇒ Object
Returns the value of attribute theme.
13 14 15 |
# File 'lib/rubyXL/workbook.rb', line 13 def theme @theme end |
#worksheet_rels ⇒ Object
Returns the value of attribute worksheet_rels.
13 14 15 |
# File 'lib/rubyXL/workbook.rb', line 13 def worksheet_rels @worksheet_rels end |
#worksheets ⇒ Object
Returns the value of attribute worksheets.
13 14 15 |
# File 'lib/rubyXL/workbook.rb', line 13 def worksheets @worksheets end |
Instance Method Details
#[](ind) ⇒ Object
Finds worksheet by its name or numerical index
71 72 73 74 75 76 |
# File 'lib/rubyXL/workbook.rb', line 71 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
81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/rubyXL/workbook.rb', line 81 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 || get_default_name) worksheets << new_worksheet new_worksheet end |
#borders ⇒ Object
Stylesheet should be pre-filled with defaults on initialize()
260 261 262 |
# File 'lib/rubyXL/workbook.rb', line 260 def borders # Stylesheet should be pre-filled with defaults on initialize() stylesheet.borders end |
#cell_xfs ⇒ Object
Stylesheet should be pre-filled with defaults on initialize()
248 249 250 |
# File 'lib/rubyXL/workbook.rb', line 248 def cell_xfs # Stylesheet should be pre-filled with defaults on initialize() stylesheet.cell_xfs end |
#date_to_num(date) ⇒ Object
146 147 148 |
# File 'lib/rubyXL/workbook.rb', line 146 def date_to_num(date) date && (date.ajd - base_date().ajd).to_f end |
#each ⇒ Object
95 96 97 |
# File 'lib/rubyXL/workbook.rb', line 95 def each worksheets.each{|i| yield i} end |
#fills ⇒ Object
Stylesheet should be pre-filled with defaults on initialize()
256 257 258 |
# File 'lib/rubyXL/workbook.rb', line 256 def fills # Stylesheet should be pre-filled with defaults on initialize() stylesheet.fills end |
#fonts ⇒ Object
Stylesheet should be pre-filled with defaults on initialize()
252 253 254 |
# File 'lib/rubyXL/workbook.rb', line 252 def fonts # Stylesheet should be pre-filled with defaults on initialize() stylesheet.fonts end |
#get_fill_color(xf) ⇒ Object
154 155 156 157 158 159 |
# File 'lib/rubyXL/workbook.rb', line 154 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
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/rubyXL/workbook.rb', line 24 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? @filepath = filepath @creator = creator @modifier = modifier self.date1904 = date1904 > 0 @media = RubyXL::GenericStorage.new(File.join('xl', 'media')).binary @external_links = RubyXL::GenericStorage.new(File.join('xl', 'externalLinks')) @external_links_rels = RubyXL::GenericStorage.new(File.join('xl', 'externalLinks', '_rels')) @drawings = RubyXL::GenericStorage.new(File.join('xl', 'drawings')) @drawings_rels = RubyXL::GenericStorage.new(File.join('xl', 'drawings', '_rels')) @charts = RubyXL::GenericStorage.new(File.join('xl', 'charts')) @chart_rels = RubyXL::GenericStorage.new(File.join('xl', 'charts', '_rels')) @worksheet_rels = RubyXL::GenericStorage.new(File.join('xl', 'worksheets', '_rels')) @chartsheet_rels = RubyXL::GenericStorage.new(File.join('xl', 'chartsheets', '_rels')) @theme = RubyXL::GenericStorage.new(File.join('xl', 'theme')) @printer_settings = RubyXL::GenericStorage.new(File.join('xl', 'printerSettings')).binary @macros = RubyXL::GenericStorage.new('xl').binary @shared_strings_container = RubyXL::SharedStringsTable.new @stylesheet = RubyXL::Stylesheet.default @document_properties = RubyXL::DocumentProperties.new @relationship_container = RubyXL::WorkbookRelationships.new @root_relationship_container = RubyXL::RootRelationships.new @calculation_chain = nil 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
210 211 212 213 214 215 216 |
# File 'lib/rubyXL/workbook.rb', line 210 def modify_alignment(style_index, is_horizontal, alignment) xf = cell_xfs[style_index].dup xf.alignment = RubyXL::Alignment.new(:apply_alignment => true, :horizontal => is_horizontal ? alignment : nil, :vertical => is_horizontal ? nil : alignment) register_new_xf(xf, style_index) end |
#modify_border(style_index, direction, weight) ⇒ Object
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 |
# File 'lib/rubyXL/workbook.rb', line 227 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 unless borders[old_xf.border_id].count == 1 && old_xf.border_id > 0 # If the old border not used anymore, just replace it 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. end borders[old_xf.border_id].count -= 1 new_border.count += 1 borders[new_xf.border_id] = new_border new_xf.apply_border = true register_new_xf(new_xf, style_index) end |
#modify_fill(style_index, rgb) ⇒ Object
218 219 220 221 222 223 224 225 |
# File 'lib/rubyXL/workbook.rb', line 218 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
204 205 206 207 208 |
# File 'lib/rubyXL/workbook.rb', line 204 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
150 151 152 |
# File 'lib/rubyXL/workbook.rb', line 150 def num_to_date(num) num && (base_date + num) end |
#register_new_fill(new_fill, old_xf) ⇒ Object
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/rubyXL/workbook.rb', line 161 def register_new_fill(new_fill, old_xf) new_xf = old_xf.dup unless fills[old_xf.fill_id].count == 1 && old_xf.fill_id > 2 # If the old fill is not used anymore, just replace it 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. end fills[old_xf.fill_id].count -= 1 new_fill.count += 1 fills[new_xf.fill_id] = new_fill new_xf.apply_fill = true new_xf end |
#register_new_font(new_font, old_xf) ⇒ Object
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
# File 'lib/rubyXL/workbook.rb', line 177 def register_new_font(new_font, old_xf) new_xf = old_xf.dup unless fonts[old_xf.font_id].count == 1 && old_xf.font_id > 1 # If the old font is not used anymore, just replace it 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. end fonts[old_xf.font_id].count -= 1 new_font.count += 1 fonts[new_xf.font_id] = new_font new_xf.apply_font = true new_xf end |
#register_new_xf(new_xf, old_style_index) ⇒ Object
193 194 195 196 197 198 199 200 201 202 |
# File 'lib/rubyXL/workbook.rb', line 193 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[old_style_index].count -= 1 new_xf.count += 1 cell_xfs[new_xf_id] = new_xf new_xf_id end |
#write(filepath = @filepath) ⇒ Object
filepath of xlsx file (including file itself)
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/rubyXL/workbook.rb', line 100 def write(filepath = @filepath) extension = File.extname(filepath) unless %w{.xlsx .xlsm}.include?(extension) raise "Only xlsx and xlsm files are supported. Unsupported extension: #{extension}" end dirpath = File.dirname(filepath) temppath = File.join(dirpath, Dir::Tmpname.make_tmpname([ File.basename(filepath), '.tmp' ], nil)) FileUtils.mkdir_p(temppath) zippath = File.join(temppath, 'file.zip') ::Zip::File.open(zippath, ::Zip::File::CREATE) { |zipfile| [ Writer::ContentTypesWriter, Writer::CoreWriter, Writer::ThemeWriter, Writer::WorkbookWriter, Writer::StylesWriter ].each { |writer_class| writer_class.new(self).add_to_zip(zipfile) } calculation_chain && calculation_chain.add_to_zip(zipfile) shared_strings_container && shared_strings_container.add_to_zip(zipfile) document_properties.add_to_zip(zipfile) relationship_container.workbook = self relationship_container.add_to_zip(zipfile) root_relationship_container.add_to_zip(zipfile) [ @media, @external_links, @external_links_rels, @drawings, @drawings_rels, @charts, @chart_rels, @printer_settings, @worksheet_rels, @chartsheet_rels, @macros ].each { |s| s.add_to_zip(zipfile) } @worksheets.each { |sheet| sheet.add_to_zip(zipfile) } } FileUtils.mv(zippath, filepath) FileUtils.rm_rf(temppath) if File.exist?(filepath) return filepath end |