diff options
| author | Randy Morgan <[email protected]> | 2012-10-14 07:54:41 +0900 |
|---|---|---|
| committer | Randy Morgan <[email protected]> | 2012-10-14 07:54:41 +0900 |
| commit | 4612f09d5ab8233d4a3f6781976aa26c2d503d01 (patch) | |
| tree | fad6721ce938af2a9fc06ec544554a913dbf5d4b /examples | |
| parent | 88d8a35b400cd25b0e68edc13519dae5a9c4d4d6 (diff) | |
| download | caxlsx-4612f09d5ab8233d4a3f6781976aa26c2d503d01.tar.gz caxlsx-4612f09d5ab8233d4a3f6781976aa26c2d503d01.zip | |
Adding in a examples for conditionally formatted hyperlink color and a financial report
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/colored_links.rb | 59 | ||||
| -rw-r--r-- | examples/finance.rb | 82 |
2 files changed, 141 insertions, 0 deletions
diff --git a/examples/colored_links.rb b/examples/colored_links.rb new file mode 100644 index 00000000..2e6bb487 --- /dev/null +++ b/examples/colored_links.rb @@ -0,0 +1,59 @@ +require 'axlsx' + +############################### +# Function to output results data row to summary spreadsheet +def outputRow (sid, type) + + $sumSheet.add_row [ sid, type, "1", "2", "3", "4", "5" ], :style => $black_cell + + if sid.odd? + link = "A#{$curRow}" + puts "outputRow: sid: #{sid}, link: #{link}" + # Setting the style for the link will apply the xf that we created in the Main Program block + $sumSheet[link].style = $blue_link + $sumSheet.add_hyperlink :location => "'Log'!A#{$curRow}", :target => :sheet, :ref => link + end + $curRow += 1 +end + +############################## +# Main Program + +$package = Axlsx::Package.new +$workbook = $package.workbook +## We want to create our sytles outside of the outputRow method +# Each style only needs to be declared once in the workbook. +$workbook.styles do |s| + $black_cell = s.add_style :sz => 10, :alignment => { :horizontal=> :center } + $blue_link = s.add_style :fg_color => '0000FF' +end + + +# Create summary sheet +$sumSheet = $workbook.add_worksheet(:name => 'Summary') +$sumSheet.add_row ["Test Results"], :sz => 16 +$sumSheet.add_row +$sumSheet.add_row +$sumSheet.add_row ["Note: Blue cells below are links to the Log sheet"], :sz => 10 +$sumSheet.add_row +$workbook.styles do |s| + black_cell = s.add_style :sz => 14, :alignment => { :horizontal=> :center } + $sumSheet.add_row ["ID","Type","Match","Mismatch","Diffs","Errors","Result"], :style => black_cell +end +$sumSheet.column_widths 10, 10, 10, 11, 10, 10, 10 + +# Starting data row in summary spreadsheet (after header info) +$curRow = 7 + +# Create Log Sheet +$logSheet = $workbook.add_worksheet(:name => 'Log') +$logSheet.column_widths 10 +$logSheet.add_row ['Log Detail'], :sz => 16 +$logSheet.add_row + +# Add rows to summary sheet +for i in 1..10 do + outputRow(i, 'test') +end + +$package.serialize 'where_is_my_color.xlsx' diff --git a/examples/finance.rb b/examples/finance.rb new file mode 100644 index 00000000..623773fd --- /dev/null +++ b/examples/finance.rb @@ -0,0 +1,82 @@ +$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' |
