diff options
| author | Randy Morgan <[email protected]> | 2011-12-05 14:19:12 +0900 |
|---|---|---|
| committer | Randy Morgan <[email protected]> | 2011-12-05 14:19:12 +0900 |
| commit | 78de4578651503bf9fa8cc8028ac0192eb8b448d (patch) | |
| tree | eade9db785cccc8d3966387d152f3231ff456139 /examples/example.rb | |
| parent | 34b08ce7a0e781ababdf5fb581965201ba578d38 (diff) | |
| download | caxlsx-78de4578651503bf9fa8cc8028ac0192eb8b448d.tar.gz caxlsx-78de4578651503bf9fa8cc8028ac0192eb8b448d.zip | |
updating docs and examples
Diffstat (limited to 'examples/example.rb')
| -rw-r--r-- | examples/example.rb | 72 |
1 files changed, 49 insertions, 23 deletions
diff --git a/examples/example.rb b/examples/example.rb index e8b00155..8900ea09 100644 --- a/examples/example.rb +++ b/examples/example.rb @@ -18,26 +18,29 @@ if ARGV.size==0 || ARGV.include?("2") p = Axlsx::Package.new p.workbook.add_worksheet do |sheet| + sheet.add_row ["A Simple Bar Chart"] sheet.add_row ["First", "Second", "Third"] sheet.add_row [1, 2, 3] - sheet.add_chart(Axlsx::Bar3DChart, :start_at => [0,2], :end_at => [5, 15], :title=>"example 2: Chart") do |chart| - chart.add_series :data=>sheet.rows.last.cells, :labels=> sheet.rows.first.cells + sheet.add_chart(Axlsx::Bar3DChart, :start_at => "A4", :end_at => "F17", :title=>sheet["A1"]) do |chart| + chart.add_series :data => sheet["A3:C3"], :labels => sheet["A2:C2"] end end p.serialize("example2.xlsx") + end #Generating A Pie Chart if ARGV.size==0 || ARGV.include?("3") p = Axlsx::Package.new p.workbook.add_worksheet do |sheet| - sheet.add_row ["First", "Second", "Third"] - sheet.add_row [1, 2, 3] + sheet.add_row ["First", "Second", "Third", "Fourth"] + sheet.add_row [1, 2, 3, "=PRODUCT(A2:C2)"] sheet.add_chart(Axlsx::Pie3DChart, :start_at => [0,2], :end_at => [5, 15], :title=>"example 3: Pie Chart") do |chart| - chart.add_series :data=>sheet.rows.last.cells, :labels=> sheet.rows.first.cells + chart.add_series :data => sheet["A2:D2"], :labels => sheet["A1:D1"] end end p.serialize("example3.xlsx") + end #Using Custom Styles @@ -45,13 +48,14 @@ if ARGV.size==0 || ARGV.include?("4") p = Axlsx::Package.new wb = p.workbook - black_cell = wb.styles.add_style :bg_color => "FF000000", :fg_color => "FFFFFFFF", :sz=>14, :alignment => { :horizontal=> :center } - blue_cell = wb.styles.add_style :bg_color => "FF0000FF", :fg_color => "FFFFFFFF", :sz=>14, :alignment => { :horizontal=> :center } + black_cell = wb.styles.add_style :bg_color => "00", :fg_color => "FF", :sz=>14, :alignment => { :horizontal=> :center } + blue_cell = wb.styles.add_style :bg_color => "0000FF", :fg_color => "FF", :sz=>14, :alignment => { :horizontal=> :center } wb.add_worksheet do |sheet| sheet.add_row ["Text Autowidth", "Second", "Third"], :style => [black_cell, blue_cell, black_cell] sheet.add_row [1, 2, 3], :style => Axlsx::STYLE_THIN_BORDER end p.serialize("example4.xlsx") + end #Using Custom Formatting and date1904 if ARGV.size==0 || ARGV.include?("5") @@ -80,6 +84,7 @@ if ARGV.size==0 || ARGV.include?("6") p.validate.each do |error| puts error.inspect end + end #Generating A Line Chart if ARGV.size==0 || ARGV.include?("7") @@ -91,12 +96,13 @@ if ARGV.size==0 || ARGV.include?("7") sheet.add_chart(Axlsx::Line3DChart, :title=>"example 6: Line Chart") do |chart| chart.start_at 0, 2 chart.end_at 10, 15 - chart.add_series :data=>sheet.rows.first.cells[(1..-1)], :title=> sheet.rows.first.cells.first + chart.add_series :data=>["B1:E1"], :title=> sheet["A1"] chart.add_series :data=>sheet.rows.last.cells[(1..-1)], :title=> sheet.rows.last.cells.first end end p.serialize("example7.xlsx") + end #Add an Image @@ -112,6 +118,7 @@ if ARGV.size==0 || ARGV.include?("8") end end p.serialize("example8.xlsx") + end #Asian Language Support @@ -124,11 +131,13 @@ if ARGV.size==0 || ARGV.include?("9") sheet.add_row ["한국어/조선말"] end p.serialize("example9.xlsx") + end #Styling Columns if ARGV.size==0 || ARGV.include?("10") + p = Axlsx::Package.new percent = p.workbook.styles.add_style :num_fmt => 9 p.workbook.add_worksheet do |sheet| @@ -139,10 +148,12 @@ if ARGV.size==0 || ARGV.include?("10") end p.workbook.worksheets.first.col_style 2, percent, :row_offset=>1 p.serialize("example10.xlsx") + end #Styling Rows if ARGV.size==0 || ARGV.include?("11") + p = Axlsx::Package.new p.workbook.add_worksheet do |sheet| sheet.add_row ['col 1', 'col 2', 'col 3', 'col 4'] @@ -150,27 +161,42 @@ if ARGV.size==0 || ARGV.include?("11") sheet.add_row [1, 2, 0.2, 4] sheet.add_row [1, 2, 0.1, 4] end - head = p.workbook.styles.add_style :bg_color => "FF000000", :fg_color=>"FFFFFFFF" + head = p.workbook.styles.add_style :bg_color => "00", :fg_color=>"FF" percent = p.workbook.styles.add_style :num_fmt => 9 p.workbook.worksheets.first.col_style 2, percent, :row_offset=>1 p.workbook.worksheets.first.row_style 0, head p.serialize("example11.xlsx") + end -#Rails 3 - - # class MyModel < ActiveRecord::Base - # acts_as_axlsx - # end - # - # class MyModelController < ApplicationController - # - # GET /posts/xlsx - # def xlsx - # p = Post.to_xlsx - # p.serialize('posts.xlsx') - # send_file 'posts.xlsx', :type=>"application/xlsx", :x_sendfile=>true - # end +#Using formula +if ARGV.size==0 || ARGV.include?("12") + + p = Axlsx::Package.new + p.workbook.add_worksheet do |sheet| + sheet.add_row ['col 1', 'col 2', 'col 3', 'col 4'] + sheet.add_row [1, 2, 3, "=SUM(A2:C2)"] + end + p.serialize("example12.xlsx") + +end + + +#Using cell specific styling and range / name based access +if ARGV.size==0 || ARGV.include?("13") + + p = Axlsx::Package.new + p.workbook.add_worksheet(:name=>'My Worksheet') do |sheet| + # cell level style overides when adding cells + sheet.add_row ['col 1', 'col 2', 'col 3', 'col 4'], :sz => 16 + sheet.add_row [1, 2, 3, "=SUM(A2:C2)"] + # cell level style overrides via sheet range + sheet["A1:D1"].each { |c| c.color = "FF0000"} + end + p.workbook['My Worksheet!A1:D2'].each { |c| c.style = Axlsx::STYLE_THIN_BORDER } + p.serialize("example13.xlsx") + +end |
