Module: RubyXL::LegacyWorkbook
Constant Summary collapse
- SHEET_NAME_TEMPLATE =
'Sheet%d'
- APPLICATION =
'Microsoft Macintosh Excel'
- APPVERSION =
'12.0000'
- @@debug =
nil
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
-
#save(filepath = nil) ⇒ Object
(also: #write)
Save the resulting XLSX file to the specified location.
-
#stream ⇒ Object
Return the resulting XLSX file in a stream (useful for sending over HTTP).
Instance Method Details
#[](ind) ⇒ Object
Finds worksheet by its name or numerical index
51 52 53 54 55 56 |
# File 'lib/rubyXL/workbook.rb', line 51 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
61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/rubyXL/workbook.rb', line 61 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 |
#borders ⇒ Object
Stylesheet should be pre-filled with defaults on initialize()
199 200 201 |
# File 'lib/rubyXL/workbook.rb', line 199 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()
187 188 189 |
# File 'lib/rubyXL/workbook.rb', line 187 def cell_xfs # Stylesheet should be pre-filled with defaults on initialize() stylesheet.cell_xfs end |
#date_to_num(date) ⇒ Object
109 110 111 |
# File 'lib/rubyXL/workbook.rb', line 109 def date_to_num(date) date && (date.ajd - base_date().ajd).to_f end |
#each ⇒ Object
75 76 77 |
# File 'lib/rubyXL/workbook.rb', line 75 def each worksheets.each{ |i| yield i } end |
#fills ⇒ Object
Stylesheet should be pre-filled with defaults on initialize()
195 196 197 |
# File 'lib/rubyXL/workbook.rb', line 195 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()
191 192 193 |
# File 'lib/rubyXL/workbook.rb', line 191 def fonts # Stylesheet should be pre-filled with defaults on initialize() stylesheet.fonts end |
#get_fill_color(xf) ⇒ Object
117 118 119 120 121 122 |
# File 'lib/rubyXL/workbook.rb', line 117 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
15 16 17 18 19 20 21 22 23 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 |
# File 'lib/rubyXL/workbook.rb', line 15 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? @creator = creator @modifier = modifier self.date1904 = date1904 > 0 @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 @comments = [] 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
155 156 157 158 159 160 161 |
# File 'lib/rubyXL/workbook.rb', line 155 def modify_alignment(style_index, is_horizontal, alignment) xf = cell_xfs[style_index].dup xf.apply_alignment = true xf.alignment = RubyXL::Alignment.new(:horizontal => is_horizontal ? alignment : nil, :vertical => is_horizontal ? nil : alignment) register_new_xf(xf, style_index) end |
#modify_border(style_index, direction, weight) ⇒ Object
172 173 174 175 176 177 178 179 180 181 182 183 184 185 |
# File 'lib/rubyXL/workbook.rb', line 172 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 new_xf.apply_border = true 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. borders[new_xf.border_id] = new_border register_new_xf(new_xf, style_index) end |
#modify_fill(style_index, rgb) ⇒ Object
163 164 165 166 167 168 169 170 |
# File 'lib/rubyXL/workbook.rb', line 163 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
149 150 151 152 153 |
# File 'lib/rubyXL/workbook.rb', line 149 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
113 114 115 |
# File 'lib/rubyXL/workbook.rb', line 113 def num_to_date(num) num && (base_date + num) end |
#register_new_fill(new_fill, old_xf) ⇒ Object
124 125 126 127 128 129 130 131 |
# File 'lib/rubyXL/workbook.rb', line 124 def register_new_fill(new_fill, old_xf) new_xf = old_xf.dup new_xf.apply_fill = true 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. fills[new_xf.fill_id] = new_fill new_xf end |
#register_new_font(new_font, old_xf) ⇒ Object
133 134 135 136 137 138 139 140 |
# File 'lib/rubyXL/workbook.rb', line 133 def register_new_font(new_font, old_xf) new_xf = old_xf.dup new_xf.apply_font = true 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. fonts[new_xf.font_id] = new_font new_xf end |
#register_new_xf(new_xf, old_style_index) ⇒ Object
142 143 144 145 146 147 |
# File 'lib/rubyXL/workbook.rb', line 142 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[new_xf_id] = new_xf new_xf_id end |
#save(filepath = nil) ⇒ Object Also known as: write
Save the resulting XLSX file to the specified location
85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/rubyXL/workbook.rb', line 85 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, "w") { |output_file| FileUtils.copy_stream(root.stream, output_file) } return filepath end |
#stream ⇒ Object
Return the resulting XLSX file in a stream (useful for sending over HTTP)
80 81 82 |
# File 'lib/rubyXL/workbook.rb', line 80 def stream root.stream end |