examples/conditional_format.rb in write_xlsx-0.85.11 vs examples/conditional_format.rb in write_xlsx-0.86.0

- old
+ new

@@ -10,10 +10,11 @@ worksheet4 = workbook.add_worksheet worksheet5 = workbook.add_worksheet worksheet6 = workbook.add_worksheet worksheet7 = workbook.add_worksheet worksheet8 = workbook.add_worksheet +worksheet9 = workbook.add_worksheet # Light red fill with dark red text. format1 = workbook.add_format( :bg_color => '#FFC7CE', :color => '#9C0006' @@ -23,10 +24,16 @@ format2 = workbook.add_format( :bg_color => '#C6EFCE', :color => '#006100' ) +# Blue fill with dark blue text. +format3 = workbook.add_format( + :bg_color => '#C6CEFF', + :color => '#0000FF' +) + # Some sample data to run the conditional formatting against. data = [ [ 34, 72, 38, 30, 75, 48, 75, 66, 84, 86 ], [ 6, 24, 1, 84, 54, 62, 60, 3, 26, 59 ], [ 28, 79, 97, 13, 85, 93, 93, 22, 5, 14 ], @@ -217,22 +224,23 @@ # # Example 7. # caption = 'Examples of color scales and data bars. Default colors.' -data = 1 .. 12 +# Use different sample data for examples 7 and 8 +data7 = 1 .. 12 worksheet7.write('A1', caption) worksheet7.write('B2', "2 Color Scale") -worksheet7.write_col('B3', data) +worksheet7.write_col('B3', data7) worksheet7.write('D2', "3 Color Scale") -worksheet7.write_col('D3', data) +worksheet7.write_col('D3', data7) worksheet7.write('F2', "Data Bars") -worksheet7.write_col('F3', data) +worksheet7.write_col('F3', data7) worksheet7.conditional_formatting('B3:B14', { :type => '2_color_scale' @@ -256,22 +264,20 @@ # # Example 8. # caption = 'Examples of color scales and data bars. Modified colors.' -data = 1 .. 12 - worksheet8.write('A1', caption) worksheet8.write('B2', "2 Color Scale") -worksheet8.write_col('B3', data) +worksheet8.write_col('B3', data7) worksheet8.write('D2', "3 Color Scale") -worksheet8.write_col('D3', data) +worksheet8.write_col('D3', data7) worksheet8.write('F2', "Data Bars") -worksheet8.write_col('F3', data) +worksheet8.write_col('F3', data7) worksheet8.conditional_formatting('B3:B14', { :type => '2_color_scale', @@ -291,9 +297,54 @@ worksheet8.conditional_formatting('F3:F14', { :type => 'data_bar', :bar_color => '#63C384' + } +) + +############################################################################### +# +# Example 9 +# +caption = 'Cells with values >= 100 are always in blue. ' + + 'Otherwise, cells with values >= 50 are in light red ' + + 'and values < 50 are in light green.' + +# Write the data. +worksheet9.write('A1', caption) +worksheet9.write_col('B3', data) + +# Write a conditional format over a range. +# Use stopIfTrue to prevent previous formats from being used +# if the conditions of this format are met. +worksheet9.conditional_formatting('B3:K12', + { + :type => 'cell', + :criteria => '>=', + :value => 100, + :format => format3, + :stop_if_true => 1 + } +) + +# Write another conditional format over the same range. +worksheet9.conditional_formatting('B3:K12', + { + :type => 'cell', + :criteria => '>=', + :value => 50, + :format => format1 + } +) + +# Write another conditional format over the same range. +worksheet9.conditional_formatting('B3:K12', + { + :type => 'cell', + :criteria => '<', + :value => 50, + :format => format2 } ) workbook.close