summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorRandy Morgan <[email protected]>2012-11-15 02:05:21 +0900
committerRandy Morgan <[email protected]>2012-11-15 02:05:27 +0900
commit1e32d9b8b12b22a273a26f0aaff9f157fbedd966 (patch)
treeb6d4aa48f4f0ba4a4cd34374603889787f069742
parent4e6ff0e91a7666b78150f120dd7fd62a8196aa13 (diff)
downloadcaxlsx-1e32d9b8b12b22a273a26f0aaff9f157fbedd966.tar.gz
caxlsx-1e32d9b8b12b22a273a26f0aaff9f157fbedd966.zip
Added color scale initialization
This lets us pass hashes defiing cfvo and colors for color scale conditional formatting with out the bs of hacking up the existing defaults.
-rwxr-xr-xexamples/example.rb100
-rw-r--r--examples/wrap_text.rb21
-rw-r--r--lib/axlsx/workbook/worksheet/cfvos.rb3
-rw-r--r--lib/axlsx/workbook/worksheet/color_scale.rb49
-rw-r--r--lib/axlsx/workbook/worksheet/data_bar.rb4
-rw-r--r--test/workbook/worksheet/tc_color_scale.rb23
6 files changed, 144 insertions, 56 deletions
diff --git a/examples/example.rb b/examples/example.rb
index 54d467f3..dc56b882 100755
--- a/examples/example.rb
+++ b/examples/example.rb
@@ -5,40 +5,42 @@ $LOAD_PATH.unshift "#{File.dirname(__FILE__)}/../lib"
#```ruby
require 'axlsx'
examples = []
-examples << :basic
-examples << :custom_styles
-examples << :cell_style_override
-examples << :custom_borders
-examples << :surrounding_border
-examples << :deep_custom_borders
-examples << :row_column_style
-examples << :fixed_column_width
-examples << :merge_cells
-examples << :images
-examples << :format_dates
-examples << :mbcs
-examples << :formula
-examples << :auto_filter
-examples << :data_types
-examples << :hyperlinks
-examples << :number_currency_format
-examples << :bar_chart
-examples << :chart_gridlines
-examples << :pie_chart
-examples << :line_chart
-examples << :scatter_chart
-examples << :tables
-examples << :fit_to_page
-examples << :hide_gridlines
-examples << :repeated_header
-examples << :defined_name
-examples << :printing
-examples << :comments
-examples << :panes
-examples << :conditional_formatting
-examples << :streaming
-examples << :shared_strings
-examples << :no_autowidth
+#examples << :basic
+#examples << :custom_styles
+#examples << :wrap_text
+#examples << :cell_style_override
+#examples << :custom_borders
+#examples << :surrounding_border
+#examples << :deep_custom_borders
+#examples << :row_column_style
+#examples << :fixed_column_width
+#examples << :merge_cells
+#examples << :images
+#examples << :format_dates
+#examples << :mbcs
+#examples << :formula
+#examples << :auto_filter
+#examples << :data_types
+#examples << :hyperlinks
+#examples << :number_currency_format
+examples << :venezuela_currency
+#examples << :bar_chart
+#examples << :chart_gridlines
+#examples << :pie_chart
+#examples << :line_chart
+#examples << :scatter_chart
+#examples << :tables
+#examples << :fit_to_page
+#examples << :hide_gridlines
+#examples << :repeated_header
+#examples << :defined_name
+#examples << :printing
+#examples << :comments
+#examples << :panes
+#examples << :conditional_formatting
+#examples << :streaming
+#examples << :shared_strings
+#examples << :no_autowidth
p = Axlsx::Package.new
wb = p.workbook
@@ -75,7 +77,26 @@ if examples.include? :custom_styles
end
end
end
-#```
+
+
+#```ruby
+# A simple example of wrapping text. Seems this may not be working in Libre Office so here is an example for me to play with.
+if examples.include? :wrap_text
+ wb.styles do |s|
+ wrap_text = s.add_style :fg_color=> "FFFFFF",
+ :b => true,
+ :bg_color => "004586",
+ :sz => 12,
+ :border => { :style => :thin, :color => "00" },
+ :alignment => { :horizontal => :center,
+ :vertical => :center ,
+ :wrap_text => true}
+ wb.add_worksheet(:name => 'wrap text') do |sheet|
+ sheet.add_row ['Torp, White and Cronin'], :style=>wrap_text
+ sheet.column_info.first.width = 5
+ end
+ end
+end
##Styling Cell Overrides
@@ -349,6 +370,15 @@ if examples.include? :number_currency_format
sheet.add_row [1500, -122.34, 123456789, 594829], :style=> [currency, red_negative, comma, super_funk]
end
end
+
+## Venezuala currency
+if examples.include? :venezuela_currency
+ wb.add_worksheet(:name => 'Venezuala_currency') do |sheet|
+ number = wb.styles.add_style :format_code => '#.##0\,00'
+ sheet.add_row [2.5] , :style => [number]
+ end
+end
+
##Generating A Bar Chart
#```ruby
diff --git a/examples/wrap_text.rb b/examples/wrap_text.rb
new file mode 100644
index 00000000..c2e1aeb5
--- /dev/null
+++ b/examples/wrap_text.rb
@@ -0,0 +1,21 @@
+$LOAD_PATH.unshift "#{File.dirname(__FILE__)}/../lib"
+require 'axlsx'
+p = Axlsx::Package.new
+p.workbook do |wb|
+ wb.styles do |s|
+ wrap_text = s.add_style :fg_color=> "FFFFFF",
+ :b => true,
+ :bg_color => "004586",
+ :sz => 12,
+ :border => { :style => :thin, :color => "00" },
+ :alignment => { :horizontal => :center,
+ :vertical => :center ,
+ :wrap_text => true}
+ wb.add_worksheet(:name => 'wrap text') do |sheet|
+ sheet.add_row ['Torp, White and Cronin'], :style => wrap_text
+ # Forcing the column to be a bit narrow so we can see if the text wrap.
+ sheet.column_info.first.width = 5
+ end
+ end
+end
+p.serialize 'wrap_text.xlsx'
diff --git a/lib/axlsx/workbook/worksheet/cfvos.rb b/lib/axlsx/workbook/worksheet/cfvos.rb
index 06d9bdd4..d642bfc6 100644
--- a/lib/axlsx/workbook/worksheet/cfvos.rb
+++ b/lib/axlsx/workbook/worksheet/cfvos.rb
@@ -6,9 +6,6 @@ module Axlsx
def initialize
super(Cfvo)
- @list << Cfvo.new(:type => :min, :val => 0)
- @list << Cfvo.new(:type => :max, :val => 0)
- lock
end
def to_xml_string(str='')
diff --git a/lib/axlsx/workbook/worksheet/color_scale.rb b/lib/axlsx/workbook/worksheet/color_scale.rb
index 60a2efa3..cdeac099 100644
--- a/lib/axlsx/workbook/worksheet/color_scale.rb
+++ b/lib/axlsx/workbook/worksheet/color_scale.rb
@@ -7,6 +7,12 @@ module Axlsx
# @see ConditionalFormattingRule#initialize
class ColorScale
+ class << self
+ def default_cfvos
+ [{:type => :min, :val => 0, :color => 'FFFF0000'},
+ {:type => :max, :val => 0, :color => 'FF0000FF'}]
+ end
+ end
# A simple typed list of cfvos
# @return [SimpleTypedList]
# @see Cfvo
@@ -17,25 +23,31 @@ module Axlsx
# A simple types list of colors
# @return [SimpleTypedList]
# @see Color
- attr_reader :colors
+ def colors
+ @colors ||= SimpleTypedList.new Color
+ end
# creates a new ColorScale object.
- # This method will yield it self so you can alter the properites of the defauls conditional formating value object (cfvo and colors
- # Two value objects and two colors are created on initialization and cannot be deleted.
# @see Cfvo
# @see Color
- def initialize
- initialize_colors
+ # @example
+ # color_scale = Axlsx::ColorScale.new({:type => :num, :val => 0.55, :color => 'fff7696c'})
+ def initialize(*cfvos)
+ initialize_default_cfvos(cfvos)
yield self if block_given?
end
# adds a new cfvo / color pair to the color scale and returns a hash containing
# a reference to the newly created cfvo and color objects so you can alter the default properties.
# @return [Hash] a hash with :cfvo and :color keys referencing the newly added objects.
+ # @param [Hash] options options for the new cfvo and color objects
+ # @option [Symbol] type The type of cfvo you to add
+ # @option [Any] val The value of the cfvo to add
+ # @option [String] The rgb color for the cfvo
def add(options={})
value_objects << Cfvo.new(:type => options[:type] || :min, :val => options[:val] || 0)
- @colors << Color.new(:rgb => options[:color] || "FF000000")
- {:cfvo => value_objects.last, :color => @colors.last}
+ colors << Color.new(:rgb => options[:color] || "FF000000")
+ {:cfvo => value_objects.last, :color => colors.last}
end
@@ -44,7 +56,7 @@ module Axlsx
# @note you cannot remove the first two cfvo and color pairs
def delete_at(index=2)
value_objects.delete_at index
- @colors.delete_at index
+ colors.delete_at index
end
# Serialize this color_scale object data to an xml string
@@ -53,18 +65,23 @@ module Axlsx
def to_xml_string(str = '')
str << '<colorScale>'
value_objects.to_xml_string(str)
- @colors.each { |color| color.to_xml_string(str) }
+ colors.each { |color| color.to_xml_string(str) }
str << '</colorScale>'
end
private
-
- # creates the initial color objects
- def initialize_colors
- @colors = SimpleTypedList.new Color
- @colors.concat [Color.new(:rgb => "FFFF0000"), Color.new(:rgb => "FF0000FF")]
- @colors.lock
+ # There has got to be cleaner way of merging these arrays.
+ def initialize_default_cfvos(user_cfvos)
+ defaults = self.class.default_cfvos
+ user_cfvos.each_with_index do |cfvo, index|
+ if index < defaults.size
+ cfvo = defaults[index].merge(cfvo)
+ end
+ add cfvo
+ end
+ while colors.size < defaults.size
+ add defaults[colors.size - 1]
+ end
end
-
end
end
diff --git a/lib/axlsx/workbook/worksheet/data_bar.rb b/lib/axlsx/workbook/worksheet/data_bar.rb
index 90cd417a..7b4c492c 100644
--- a/lib/axlsx/workbook/worksheet/data_bar.rb
+++ b/lib/axlsx/workbook/worksheet/data_bar.rb
@@ -19,6 +19,10 @@ module Axlsx
@min_length = 10
@max_length = 90
@show_value = true
+ # TODO initialize using the same pattern as color_scale so consumers can override these values
+ # as they like.
+ value_objects << Cfvo.new(:type => :min, :val => 0)
+ value_objects << Cfvo.new(:type => :max, :val => 0)
parse_options options
yield self if block_given?
end
diff --git a/test/workbook/worksheet/tc_color_scale.rb b/test/workbook/worksheet/tc_color_scale.rb
index d7e66fcf..9dde93ab 100644
--- a/test/workbook/worksheet/tc_color_scale.rb
+++ b/test/workbook/worksheet/tc_color_scale.rb
@@ -5,6 +5,27 @@ class TestColorScale < Test::Unit::TestCase
@color_scale = Axlsx::ColorScale.new
end
+
+ def test_default_cfvo
+ first = Axlsx::ColorScale.default_cfvos.first
+ second = Axlsx::ColorScale.default_cfvos.last
+ assert_equal 'FFFF0000', first[:color]
+ assert_equal :min,first[:type]
+ assert_equal 0, first[:val]
+
+ assert_equal 'FF0000FF', second[:color]
+ assert_equal :max, second[:type]
+ assert_equal 0, second[:val]
+ end
+
+ def test_partial_default_cfvo_override
+ first_def = {:type => :percent, :val => "10.0", :color => 'FF00FF00'}
+ color_scale = Axlsx::ColorScale.new(first_def)
+ assert_equal color_scale.value_objects.first.val, first_def[:val]
+ assert_equal color_scale.value_objects.first.type, first_def[:type]
+ assert_equal color_scale.colors.first.rgb, first_def[:color]
+ end
+
def test_add
@color_scale.add :type => :max, :val => 5, :color => "FFDEDEDE"
assert_equal(@color_scale.value_objects.size,3)
@@ -12,8 +33,6 @@ class TestColorScale < Test::Unit::TestCase
end
def test_delete_at
- assert_raise(ArgumentError, "minimum two are protected") { @color_scale.delete_at 0 }
- assert_raise(ArgumentError, "minimum two are protected") { @color_scale.delete_at 1 }
@color_scale.add :type => :max, :val => 5, :color => "FFDEDEDE"
assert_nothing_raised {@color_scale.delete_at 2}
assert_equal(@color_scale.value_objects.size,2)