diff options
| author | Randy Morgan <[email protected]> | 2012-11-25 16:08:23 +0900 |
|---|---|---|
| committer | Randy Morgan <[email protected]> | 2012-11-25 16:08:23 +0900 |
| commit | 1eee1d3b3d032e349b907cd4b416114e25aecda8 (patch) | |
| tree | 16c8c2deed98049b3d1b0e7d574a526e00f52238 | |
| parent | be2c173291e33e84c7de088d73f2f676a879306d (diff) | |
| download | caxlsx-1eee1d3b3d032e349b907cd4b416114e25aecda8.tar.gz caxlsx-1eee1d3b3d032e349b907cd4b416114e25aecda8.zip | |
Cleaned up examples directory
| -rwxr-xr-x | examples/example.rb | 78 | ||||
| -rw-r--r-- | examples/finance.rb | 82 | ||||
| -rw-r--r-- | examples/hyperlinks.rb | 23 | ||||
| -rw-r--r-- | examples/scraping_html.rb | 91 | ||||
| -rw-r--r-- | examples/skydrive/axlsx.csv | 1 | ||||
| -rw-r--r-- | examples/sprk2012/basics.rb | 11 | ||||
| -rw-r--r-- | examples/sprk2012/images.rb | 9 | ||||
| -rw-r--r-- | examples/sprk2012/styles.rb | 20 | ||||
| -rw-r--r-- | examples/two_cell_anchor_image.rb | 11 |
9 files changed, 53 insertions, 273 deletions
diff --git a/examples/example.rb b/examples/example.rb index a675cfa6..a7eace2a 100755 --- a/examples/example.rb +++ b/examples/example.rb @@ -38,6 +38,7 @@ examples << :printing examples << :header_footer examples << :comments examples << :panes +examples << :sheet_view examples << :conditional_formatting examples << :streaming examples << :shared_strings @@ -582,6 +583,33 @@ if examples.include? :panes end end +if examples.include? :sheet_view + ws = wb.add_worksheet(:name => 'SheetView - Split') + ws.sheet_view do |vs| + vs.pane do |pane| + pane.active_pane = :top_right + pane.state = :split + pane.x_split = 11080 + pane.y_split = 5000 + pane.top_left_cell = 'C44' + end + + vs.add_selection(:top_left, { :active_cell => 'A2', :sqref => 'A2' }) + vs.add_selection(:top_right, { :active_cell => 'I10', :sqref => 'I10' }) + vs.add_selection(:bottom_left, { :active_cell => 'E55', :sqref => 'E55' }) + vs.add_selection(:bottom_right, { :active_cell => 'I57', :sqref => 'I57' }) + end + + ws = wb.add_worksheet :name => "Sheetview - Frozen" + ws.sheet_view do |vs| + vs.pane do |pane| + pane.state = :frozen + pane.x_split = 3 + pane.y_split = 4 + end + end +end + # conditional formatting # if examples.include? :conditional_formatting @@ -592,61 +620,61 @@ if examples.include? :conditional_formatting profitable = wb.styles.add_style( :fg_color=>"FF428751", :type => :dxf) - wb.add_worksheet(:name => "Conditional Cell Is") do |ws| + wb.add_worksheet(:name => "Conditional Cell Is") do |sheet| - # Generate 20 rows of data - ws.add_row ["Previous Year Quarterly Profits (JPY)"] - ws.add_row ["Quarter", "Profit", "% of Total"] + # Generate 20 rosheet of data + sheet.add_row ["Previous Year Quarterly Profits (JPY)"] + sheet.add_row ["Quarter", "Profit", "% of Total"] offset = 3 - rows = 20 - offset.upto(rows + offset) do |i| - ws.add_row ["Q#{i}", 10000*((rows/2-i) * (rows/2-i)), "=100*B#{i}/SUM(B3:B#{rows+offset})"], :style=>[nil, money, percent] + rosheet = 20 + offset.upto(rosheet + offset) do |i| + sheet.add_row ["Q#{i}", 10000*((rosheet/2-i) * (rosheet/2-i)), "=100*B#{i}/SUM(B3:B#{rosheet+offset})"], :style=>[nil, money, percent] end # Apply conditional formatting to range B3:B100 in the worksheet - ws.add_conditional_formatting("B3:B100", { :type => :cellIs, :operator => :greaterThan, :formula => "100000", :dxfId => profitable, :priority => 1 }) + sheet.add_conditional_formatting("B3:B100", { :type => :cellIs, :operator => :greaterThan, :formula => "100000", :dxfId => profitable, :priority => 1 }) end - wb.add_worksheet(:name => "Conditional Color Scale") do |ws| - ws.add_row ["Previous Year Quarterly Profits (JPY)"] - ws.add_row ["Quarter", "Profit", "% of Total"] + wb.add_worksheet(:name => "Conditional Color Scale") do |sheet| + sheet.add_row ["Previous Year Quarterly Profits (JPY)"] + sheet.add_row ["Quarter", "Profit", "% of Total"] offset = 3 - rows = 20 - offset.upto(rows + offset) do |i| - ws.add_row ["Q#{i}", 10000*((rows/2-i) * (rows/2-i)), "=100*B#{i}/SUM(B3:B#{rows+offset})"], :style=>[nil, money, percent] + rosheet = 20 + offset.upto(rosheet + offset) do |i| + sheet.add_row ["Q#{i}", 10000*((rosheet/2-i) * (rosheet/2-i)), "=100*B#{i}/SUM(B3:B#{rosheet+offset})"], :style=>[nil, money, percent] end # color scale has two_tone and three_tone class methods to setup the excel defaults (2011) # alternatively, you can pass in {:type => [:min, :max, :percent], :val => [whatever], :color =>[Some RGB String] to create a customized color scale object color_scale = Axlsx::ColorScale.three_tone - ws.add_conditional_formatting("B3:B100", { :type => :colorScale, :operator => :greaterThan, :formula => "100000", :dxfId => profitable, :priority => 1, :color_scale => color_scale }) + sheet.add_conditional_formatting("B3:B100", { :type => :colorScale, :operator => :greaterThan, :formula => "100000", :dxfId => profitable, :priority => 1, :color_scale => color_scale }) end - wb.add_worksheet(:name => "Conditional Data Bar") do |ws| - ws.add_row ["Previous Year Quarterly Profits (JPY)"] - ws.add_row ["Quarter", "Profit", "% of Total"] + wb.add_worksheet(:name => "Conditional Data Bar") do |sheet| + sheet.add_row ["Previous Year Quarterly Profits (JPY)"] + sheet.add_row ["Quarter", "Profit", "% of Total"] offset = 3 rows = 20 offset.upto(rows + offset) do |i| - ws.add_row ["Q#{i}", 10000*((rows/2-i) * (rows/2-i)), "=100*B#{i}/SUM(B3:B#{rows+offset})"], :style=>[nil, money, percent] + sheet.add_row ["Q#{i}", 10000*((rows/2-i) * (rows/2-i)), "=100*B#{i}/SUM(B3:B#{rows+offset})"], :style=>[nil, money, percent] end # Apply conditional formatting to range B3:B100 in the worksheet data_bar = Axlsx::DataBar.new - ws.add_conditional_formatting("B3:B100", { :type => :dataBar, :dxfId => profitable, :priority => 1, :data_bar => data_bar }) + sheet.add_conditional_formatting("B3:B100", { :type => :dataBar, :dxfId => profitable, :priority => 1, :data_bar => data_bar }) end - wb.add_worksheet(:name => "Conditional Format Icon Set") do |ws| - ws.add_row ["Previous Year Quarterly Profits (JPY)"] - ws.add_row ["Quarter", "Profit", "% of Total"] + wb.add_worksheet(:name => "Conditional Format Icon Set") do |sheet| + sheet.add_row ["Previous Year Quarterly Profits (JPY)"] + sheet.add_row ["Quarter", "Profit", "% of Total"] offset = 3 rows = 20 offset.upto(rows + offset) do |i| - ws.add_row ["Q#{i}", 10000*((rows/2-i) * (rows/2-i)), "=100*B#{i}/SUM(B3:B#{rows+offset})"], :style=>[nil, money, percent] + sheet.add_row ["Q#{i}", 10000*((rows/2-i) * (rows/2-i)), "=100*B#{i}/SUM(B3:B#{rows+offset})"], :style=>[nil, money, percent] end # Apply conditional formatting to range B3:B100 in the worksheet icon_set = Axlsx::IconSet.new - ws.add_conditional_formatting("B3:B100", { :type => :iconSet, :dxfId => profitable, :priority => 1, :icon_set => icon_set }) + sheet.add_conditional_formatting("B3:B100", { :type => :iconSet, :dxfId => profitable, :priority => 1, :icon_set => icon_set }) end end diff --git a/examples/finance.rb b/examples/finance.rb deleted file mode 100644 index 623773fd..00000000 --- a/examples/finance.rb +++ /dev/null @@ -1,82 +0,0 @@ -$LOAD_PATH.unshift "#{File.dirname(__FILE__)}/../lib" - - -require 'axlsx' - -# First thing to do is setup our styles. OOXML Style management is unfortunately very different from CSS so we want to use the -# add_style helper method on the workbook styles object so we dont go insane. - -# I find it easier to declare a hash and then feed that in later - -class FinancialReport - - def initialize(data) - create_styles - prepare - insert_data data - finalize - package - end - - def style_hash - - sienna = 'A0522D' - { - :search_results => { :sz => 10, :b => true }, - :bold_header => { :b => true }, - :grey_bg => { :bg_color => "DEDEDE" }, - :transaction__header => { :fg_color => sienna, :b => true }, - :transaction_currency => { :fb_color => sienna, :num_fmt => 5 }, - :transactin_date => { :fg_color => sienna, :format_code => 'yyyy-mm-dd' } - } - end - - def styles - @styles ||= {} - end - - def package - @package ||= Axlsx::Package.new - end - - # Just a place to put some defualt data for the exercise - def self.data - @data = ['baked', - "American Medical Systems Holdings, Inc.", - "Endo Pharmaceuticals Holdings, Inc.", - "4371", - Date.new, - 2757001160, - 2519495160, - 116, - 3842, - "Medical devices for urology disorders"] - end - - # Populates an array of ['style_name'] = style_index - def create_styles - package.workbook.styles do |style| - style_hash.each do |key, value| - styles[key] = style.add_style(value) - end - end - end - def prepare - package.workbook.add_worksheet(:name => 'All Information') do |sheet| - sheet.add_row [nil, 'Search Results'], :style => [nil, styles['search_results']] - end - end - - - def insert_data(data) - package.workbook.worksheets.first do |sheet| - sheet.add_row data, style=> [styles[: - end - end - - def finalize - # package.serialize 'financial.xlsx' - end -end -f = FinancialReport.new(FinancialReport.data) -f.package.serialize 'finance.xlsx' diff --git a/examples/hyperlinks.rb b/examples/hyperlinks.rb deleted file mode 100644 index 9519f154..00000000 --- a/examples/hyperlinks.rb +++ /dev/null @@ -1,23 +0,0 @@ -#!/usr/bin/env ruby -w -s -# -*- coding: utf-8 -*- -$LOAD_PATH.unshift "#{File.dirname(__FILE__)}/../lib" - -#```ruby -require 'axlsx' - -p = Axlsx::Package.new -wb = p.workbook -wb.add_worksheet(:name => 'hyperlinks') do |sheet| - # external references - sheet.add_row ['axlsx'] - sheet.add_hyperlink :location => 'https://github.com/randym/axlsx', :ref => sheet.rows.first.cells.first - # internal references - sheet.add_row ['next sheet'] - sheet.add_hyperlink :location => "'Next Sheet'!A1", :target => :sheet, :ref => 'A2' -end - -wb.add_worksheet(:name => 'Next Sheet') do |sheet| - sheet.add_row ['hello!'] -end - -p.serialize 'hyperlinks.xlsx' diff --git a/examples/scraping_html.rb b/examples/scraping_html.rb deleted file mode 100644 index 7df27c25..00000000 --- a/examples/scraping_html.rb +++ /dev/null @@ -1,91 +0,0 @@ - require 'rubygems' - require 'nokogiri' - require 'open-uri' - require 'axlsx' - - class Scraper - - def initialize(url, selector) - @url = url - @selector = selector - end - - def hooks - @hooks ||= {} - end - - def add_hook(clue, p_roc) - hooks[clue] = p_roc - end - - def export(file_name) - Scraper.clues.each do |clue| - if detail = parse_clue(clue) - output << [clue, detail.pop] - detail.each { |datum| output << ['', datum] } - end - end - serialize(file_name) - end - - private - - def self.clues - @clues ||= ['Operating system', 'Processors', 'Chipset', 'Memory type', 'Hard drive', 'Graphics', - 'Ports', 'Webcam', 'Pointing device', 'Keyboard', 'Network interface', 'Chipset', 'Wireless', - 'Power supply type', 'Energy efficiency', 'Weight', 'Minimum dimensions (W x D x H)', - 'Warranty', 'Software included', 'Product color'] - end - - def doc - @doc ||= begin - Nokogiri::HTML(open(@url)) - rescue - raise ArgumentError, 'Invalid URL - Nothing to parse' - end - end - - def output - @output ||= [] - end - - def selector_for_clue(clue) - @selector % clue - end - - def parse_clue(clue) - if element = doc.at(selector_for_clue(clue)) - call_hook(clue, element) || element.inner_html.split('<br>').each(&:strip) - end - end - - def call_hook(clue, element) - if hooks[clue].is_a? Proc - value = hooks[clue].call(element) - value.is_a?(Array) ? value : [value] - end - end - - def package - @package ||= Axlsx::Package.new - end - - def serialize(file_name) - package.workbook.add_worksheet do |sheet| - output.each { |datum| sheet.add_row datum } - end - package.serialize(file_name) - end - end - - scraper = Scraper.new("http://h10010.www1.hp.com/wwpc/ie/en/ho/WF06b/321957-321957-3329742-89318-89318-5186820-5231694.html?dnr=1", "//td[text()='%s']/following-sibling::td") - - # define a custom action to take against any elements found. - os_parse = Proc.new do |element| - element.inner_html.split('<br>').each(&:strip!).each(&:upcase!) - end - - scraper.add_hook('Operating system', os_parse) - - scraper.export('foo.xlsx') - diff --git a/examples/skydrive/axlsx.csv b/examples/skydrive/axlsx.csv deleted file mode 100644 index b3cea7c1..00000000 --- a/examples/skydrive/axlsx.csv +++ /dev/null @@ -1 +0,0 @@ -,,,,,
,College Budget,,,,
,,,,,
,What's coming in this month.,,,How am I doing,
,Item,Amount,,Item,Amount
,Estimated monthly net income,\500,,Monthly income,"\20,600"
,Financial aid,\100,,Monthly expenses,"\2,255"
,Allowance from mom & dad,"\20,000",,Semester expenses,\440
,Total,"\20,600",,Difference,"\17,905"
,,,,,
,What's going out this month.,,,Semester Costs,
,Item,Amount,,Item,Amount
,Rent,\650,,Tuition,\200
,Utilities,\120,,Lab fees,\50
,Cell phone,\100,,Other fees,\10
,Groceries,\75,,Books,\150
,Auto expenses,\0,,Deposits,\0
,Student loans,\0,,Transportation,\30
,Other loans,\350,,Total,\440
,Credit cards,\450,,,
,Insurance,\0,,,
,Laundry,\10,,,
,Haircuts,\0,,,
,Medical expenses,\0,,,
,Entertainment,\500,,,
,Miscellaneous,\0,,,
,Total,"\2,255",,,
\ No newline at end of file diff --git a/examples/sprk2012/basics.rb b/examples/sprk2012/basics.rb deleted file mode 100644 index 82c56a4e..00000000 --- a/examples/sprk2012/basics.rb +++ /dev/null @@ -1,11 +0,0 @@ -$LOAD_PATH.unshift "#{File.dirname(__FILE__)}/../../lib" -require 'axlsx' - -package = Axlsx::Package.new -package.workbook.add_worksheet(:name => "Basic Worksheet") do |sheet| - sheet.add_row ["First Column", "Second", "Third"] - sheet.add_row [1, 2, 3] -end -package.serialize 'basics.xlsx' - - diff --git a/examples/sprk2012/images.rb b/examples/sprk2012/images.rb deleted file mode 100644 index 6e0b306d..00000000 --- a/examples/sprk2012/images.rb +++ /dev/null @@ -1,9 +0,0 @@ -$LOAD_PATH.unshift "#{File.dirname(__FILE__)}/../../lib" - -require 'axlsx' -package = Axlsx::Package.new do |package| - package.workbook.add_worksheet(:name => "imagex") do |sheet| - img_path = File.expand_path('../../image1.jpeg', __FILE__) - sheet.add_image(:image_src => img_path, :width => 720, :height => 666, :start_at => [2,2]) - end -end.serialize 'images.xlsx' diff --git a/examples/sprk2012/styles.rb b/examples/sprk2012/styles.rb deleted file mode 100644 index ce1bf2a0..00000000 --- a/examples/sprk2012/styles.rb +++ /dev/null @@ -1,20 +0,0 @@ -$LOAD_PATH.unshift "#{File.dirname(__FILE__)}/../../lib" - -require 'axlsx' -package = Axlsx::Package.new -package.workbook do |workbook| - workbook.styles do |s| - black_cell = s.add_style :bg_color => "00", :fg_color => "FF", :sz => 14, :alignment => { :horizontal=> :center } - blue_cell = s.add_style :bg_color => "0000FF", :fg_color => "FF", :sz => 20, :alignment => { :horizontal=> :center } - - workbook.add_worksheet(:name => "Styles") do |sheet| - # Applies the black_cell style to the first and third cell, and the blue_cell style to the second. - sheet.add_row ["Text Autowidth", "Second", "Third"], :style => [black_cell, blue_cell, black_cell] - - # Applies the thin border to all three cells - sheet.add_row [1, 2, 3], :style => Axlsx::STYLE_THIN_BORDER - end - end -end -package.serialize 'styles.xlsx' - diff --git a/examples/two_cell_anchor_image.rb b/examples/two_cell_anchor_image.rb deleted file mode 100644 index 4fe4b566..00000000 --- a/examples/two_cell_anchor_image.rb +++ /dev/null @@ -1,11 +0,0 @@ -#!/usr/bin/env ruby -w -s -# -*- coding: utf-8 -*- -$LOAD_PATH.unshift "#{File.dirname(__FILE__)}/../lib" -require 'axlsx' - -p = Axlsx::Package.new -src = "#{File.dirname(__FILE__)}/image1.png" -p.workbook.add_worksheet(:name => 'double_anchor') do |ws| - ws.add_image(:image_src => src, :start_at => [0,0], :end_at => [2,4]) -end -p.serialize('two_cell_anchor_image.xlsx') |
