summaryrefslogtreecommitdiffhomepage
path: root/examples
diff options
context:
space:
mode:
authorStephen Pike <[email protected]>2012-04-20 15:05:01 -0400
committerStephen Pike <[email protected]>2012-04-20 15:05:01 -0400
commitf96205df2b10c68d10a2a6882fe77928358ed362 (patch)
treeba6539559d00ae055ece38427d22ad8f64da5e07 /examples
parent6b0c1e3c59e581525161491d85a153a537711370 (diff)
downloadcaxlsx-f96205df2b10c68d10a2a6882fe77928358ed362.tar.gz
caxlsx-f96205df2b10c68d10a2a6882fe77928358ed362.zip
[#33] Add support for Dxf elements to enable conditional formatting
New Dxf class implements 18.8.14. Conditional formatting now "works". Add :type option to add_styles, defaults to :xf when add_styles is called with :dxf type, new styles are not added to the global @styles set. Dxf child elements only exist inside the `<dxf>` chunk. Added tests and an example.
Diffstat (limited to 'examples')
-rw-r--r--examples/example_conditional_formatting.rb31
1 files changed, 31 insertions, 0 deletions
diff --git a/examples/example_conditional_formatting.rb b/examples/example_conditional_formatting.rb
new file mode 100644
index 00000000..cfff3c86
--- /dev/null
+++ b/examples/example_conditional_formatting.rb
@@ -0,0 +1,31 @@
+#!/usr/bin/env ruby -w -s
+# -*- coding: utf-8 -*-
+$LOAD_PATH.unshift "#{File.dirname(__FILE__)}/../lib"
+require 'axlsx'
+
+p = Axlsx::Package.new
+book = p.workbook
+ws = book.add_worksheet
+
+# define your regular styles
+percent = book.styles.add_style(:format_code => "0.00%", :border => Axlsx::STYLE_THIN_BORDER)
+money = book.styles.add_style(:format_code => '0,000', :border => Axlsx::STYLE_THIN_BORDER)
+
+# define the style for conditional formatting
+profitable = book.styles.add_style( :fg_color=>"FF428751",
+ :type => :dxf)
+
+# Generate 20 rows of data
+ws.add_row ["Previous Year Quarterly Profits (JPY)"]
+ws.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]
+end
+
+# Apply conditional formatting to range B4:B100 in the worksheet
+ws.add_conditional_formatting("B4:B100", { :type => :cellIs, :operator => :greaterThan, :formula => "100000", :dxfId => profitable, :priority => 1 })
+
+f = File.open('example_differential_styling.xlsx', 'w')
+p.serialize(f)