diff options
| author | Randy Morgan <[email protected]> | 2012-04-29 09:40:39 +0900 |
|---|---|---|
| committer | Randy Morgan <[email protected]> | 2012-04-29 09:40:39 +0900 |
| commit | 7816e99565d79a20b6154dfb68333162c7dcdfe3 (patch) | |
| tree | 70bbfa0c35582603a2d457ff00c9f2e90922e845 | |
| parent | ec22639ba22da773b132c0d4cb10c4fee7925d2e (diff) | |
| download | caxlsx-7816e99565d79a20b6154dfb68333162c7dcdfe3.tar.gz caxlsx-7816e99565d79a20b6154dfb68333162c7dcdfe3.zip | |
examples and read me clean up as well as implementing outlineLevel for row and col.
| -rw-r--r-- | README.md | 225 | ||||
| -rw-r--r-- | examples/example.rb | 289 | ||||
| -rw-r--r-- | lib/axlsx/workbook/worksheet/col.rb | 3 | ||||
| -rw-r--r-- | lib/axlsx/workbook/worksheet/row.rb | 47 |
4 files changed, 328 insertions, 236 deletions
@@ -16,7 +16,7 @@ Axlsx: Office Open XML Spreadsheet Generation **License**: MIT License -**Latest Version**: 1.1.3 +**Latest Version**: 1.1.4 **Ruby Version**: 1.8.7, 1.9.2, 1.9.3 @@ -46,7 +46,7 @@ Feature List **1. Author xlsx documents: Axlsx is made to let you easily and quickly generate professional xlsx based reports that can be validated before serialization. -**2. Generate 3D Pie, Line and Bar Charts: With Axlsx chart generation and management is as easy as a few lines of code. You can build charts based off data in your worksheet or generate charts without any data in your sheet at all. +**2. Generate 3D Pie, Line, Scatter and Bar Charts: With Axlsx chart generation and management is as easy as a few lines of code. You can build charts based off data in your worksheet or generate charts without any data in your sheet at all. Customize gridlines, label rotation and series colors as well. **3. Custom Styles: With guaranteed document validity, you can style borders, alignment, fills, fonts, and number formats in a single line of code. Those styles can be applied to an entire row, or a single cell anywhere in your workbook. @@ -66,7 +66,7 @@ Feature List **11. Support for cell merging via worksheet.merged_cells -**12. Auto filtering tables with worksheet.auto_filter +**12. Auto filtering tables with worksheet.auto_filter as well as support for Tables **13. Export using shared strings or inline strings so we can inter-op with iWork Numbers (sans charts for now). @@ -103,22 +103,47 @@ end #Using Custom Styles ```ruby +#Each cell allows a single, predified style. When using add_row, the value in the :style array at the same index as the cell's column will be applied to that cell. Alternatively, you can apply a style to an entire row by using an integer value for :style. + wb.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 } wb.add_worksheet(:name => "Custom 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 ``` +##Styling Cell Overrides + +```ruby +#Some of the style attributes can also be set at the cell level. Cell level styles take precedence over Custom Styles shown in the previous example. + +wb.add_worksheet(:name => "Cell Level Style Overrides") do |sheet| + + # this will set the font size for each cell. + sheet.add_row ['col 1', 'col 2', 'col 3', 'col 4'], :sz => 16 + + sheet.add_row [1, 2, 3, "=SUM(A2:C2)"] + + # You can also apply cell style overrides to a range of cells + sheet["A1:D1"].each { |c| c.color = "FF0000" } + sheet['A1:D2'].each { |c| c.style = Axlsx::STYLE_THIN_BORDER } +end +``` + #Using Custom Border Styles ```ruby +#Axlsx defines a thin border style, but you can easily create and use your own. wb.styles do |s| - red_border = s.add_style :border => { :style => :thin, :color =>"FFFF0000" } - blue_border = s.add_style :border => { :style => :thin, :color =>"FF0000FF" } + red_border = s.add_style :border => { :style => :thick, :color =>"FFFF0000" } + blue_border = s.add_style :border => { :style => :thick, :color =>"FF0000FF" } wb.add_worksheet(:name => "Custom Borders") do |sheet| sheet.add_row ["wrap", "me", "Up in Red"], :style => red_border @@ -127,34 +152,56 @@ wb.styles do |s| end ``` - -##Using Custom Formatting and date1904 +##Styling Rows and Columns ```ruby -require 'date' wb.styles do |s| - date = s.add_style(:format_code => "yyyy-mm-dd", :border => Axlsx::STYLE_THIN_BORDER) - padded = s.add_style(:format_code => "00#", :border => Axlsx::STYLE_THIN_BORDER) - percent = s.add_style(:format_code => "0000%", :border => Axlsx::STYLE_THIN_BORDER) - # wb.date1904 = true # Use the 1904 date system (Used by Excel for Mac < 2011) - wb.add_worksheet(:name => "Formatting Data") do |sheet| - sheet.add_row ["Custom Formatted Date", "Percent Formatted Float", "Padded Numbers"], :style => Axlsx::STYLE_THIN_BORDER - sheet.add_row [Date::strptime('2012-01-19','%Y-%m-%d'), 0.2, 32], :style => [date, percent, padded] + head = s.add_style :bg_color => "00", :fg_color => "FF" + percent = s.add_style :num_fmt => 9 + wb.add_worksheet(:name => "Hidden Column") do |sheet| + sheet.add_row ['col 1', 'col 2', 'col 3', 'col 4', 'col5'] + sheet.add_row [1, 2, 0.3, 4, 5.0] + sheet.add_row [1, 2, 0.2, 4, 5.0] + sheet.add_row [1, 2, 0.1, 4, 5.0] + + #apply the percent style to the column at index 2 skipping the first row. + sheet.col_style 2, percent, :row_offset => 1 + + # apply the head style to the first row. + sheet.row_style 0, head + + #Hide the 5th column + sheet.column_info[4].hidden = true + + #Set the second column outline level + sheet.column_info[1].outlineLevel = 2 + end end ``` +##Specifying Column Widths -##Add an Image +```ruby +wb.add_worksheet(:name => "custom column widths") do |sheet| + sheet.add_row ["I use autowidth and am very wide", "I use a custom width and am narrow"] + sheet.add_row ['abcdefg', 'This is a very long text and should flow into the right cell', nil, 'xxx' ] + sheet.column_widths nil, 3, 5, nil +end +``` + +##Merging Cells. ```ruby -wb.add_worksheet(:name => "Images") do |sheet| - img = File.expand_path('../image1.jpeg', __FILE__) - sheet.add_image(:image_src => img, :noSelect => true, :noMove => true) do |image| - image.width=720 - image.height=666 - image.start_at 2, 2 - end +wb.add_worksheet(:name => 'Merging Cells') do |sheet| + # cell level style overides when adding cells + sheet.add_row ["col 1", "col 2", "col 3", "col 4"], :sz => 16 + sheet.add_row [1, 2, 3, "=SUM(A2:C2)"] + sheet.add_row [2, 3, 4, "=SUM(A3:C3)"] + sheet.add_row ["total", "", "", "=SUM(D2:D3)"] + sheet.merge_cells("A4:C4") + sheet["A1:D1"].each { |c| c.color = "FF0000"} + sheet["A1:D4"].each { |c| c.style = Axlsx::STYLE_THIN_BORDER } end ``` @@ -163,6 +210,8 @@ end ```ruby wb.add_worksheet(:name => "Image with Hyperlink") do |sheet| img = File.expand_path('../image1.jpeg', __FILE__) + # specifying the :hyperlink option will add a hyper link to your image. + # @note - Numbers does not support this part of the specification. sheet.add_image(:image_src => img, :noSelect => true, :noMove => true, :hyperlink=>"http://axlsx.blogspot.com") do |image| image.width=720 image.height=666 @@ -172,83 +221,51 @@ wb.add_worksheet(:name => "Image with Hyperlink") do |sheet| end ``` -##Asian Language Support - -```ruby -wb.add_worksheet(:name => "日本語でのシート名") do |sheet| - sheet.add_row ["日本語"] - sheet.add_row ["华语/華語"] - sheet.add_row ["한국어/조선말"] -end -``` - -##Styling Columns - -```ruby -wb.styles do |s| - percent = s.add_style :num_fmt => 9 - wb.add_worksheet(:name => "Styling Columns") do |sheet| - sheet.add_row ['col 1', 'col 2', 'col 3', 'col 4'] - sheet.add_row [1, 2, 0.3, 4] - sheet.add_row [1, 2, 0.2, 4] - sheet.add_row [1, 2, 0.1, 4] - sheet.col_style 2, percent, :row_offset => 1 - end -end -``` - -##Hiding Columns +##Using Custom Formatting and date1904 ```ruby +require 'date' wb.styles do |s| - percent = s.add_style :num_fmt => 9 - wb.add_worksheet(:name => "Hidden Column") do |sheet| - sheet.add_row ['col 1', 'col 2', 'col 3', 'col 4'] - sheet.add_row [1, 2, 0.3, 4] - sheet.add_row [1, 2, 0.2, 4] - sheet.add_row [1, 2, 0.1, 4] - sheet.col_style 2, percent, :row_offset => 1 - sheet.column_info[1].hidden = true + date = s.add_style(:format_code => "yyyy-mm-dd", :border => Axlsx::STYLE_THIN_BORDER) + padded = s.add_style(:format_code => "00#", :border => Axlsx::STYLE_THIN_BORDER) + percent = s.add_style(:format_code => "0000%", :border => Axlsx::STYLE_THIN_BORDER) + # wb.date1904 = true # Use the 1904 date system (Used by Excel for Mac < 2011) + wb.add_worksheet(:name => "Formatting Data") do |sheet| + sheet.add_row ["Custom Formatted Date", "Percent Formatted Float", "Padded Numbers"], :style => Axlsx::STYLE_THIN_BORDER + sheet.add_row [Date::strptime('2012-01-19','%Y-%m-%d'), 0.2, 32], :style => [date, percent, padded] end end ``` -##Styling Rows +##Asian Language Support ```ruby -wb.styles do |s| - head = s.add_style :bg_color => "00", :fg_color => "FF" - percent = s.add_style :num_fmt => 9 - wb.add_worksheet(:name => "Styling Rows") do |sheet| - sheet.add_row ['col 1', 'col 2', 'col 3', 'col 4'] - sheet.add_row [1, 2, 0.3, 4] - sheet.add_row [1, 2, 0.2, 4] - sheet.add_row [1, 2, 0.1, 4] - sheet.col_style 2, percent, :row_offset => 1 - sheet.row_style 0, head - end +wb.add_worksheet(:name => "日本語でのシート名") do |sheet| + sheet.add_row ["日本語"] + sheet.add_row ["华语/華語"] + sheet.add_row ["한국어/조선말"] end ``` -##Styling Cell Overrides +##Using formula ```ruby -wb.add_worksheet(:name => "Cell Level Style Overrides") do |sheet| - # cell level style overides when adding cells - sheet.add_row ['col 1', 'col 2', 'col 3', 'col 4'], :sz => 16 +wb.add_worksheet(:name => "Using Formulas") do |sheet| + sheet.add_row ['col 1', 'col 2', 'col 3', 'col 4'] sheet.add_row [1, 2, 3, "=SUM(A2:C2)"] - # cell level style overrides via sheet range - sheet["A1:D1"].each { |c| c.color = "FF0000"} - sheet['A1:D2'].each { |c| c.style = Axlsx::STYLE_THIN_BORDER } end ``` -##Using formula +##Auto Filter ```ruby -wb.add_worksheet(:name => "Using Formulas") do |sheet| - sheet.add_row ['col 1', 'col 2', 'col 3', 'col 4'] - sheet.add_row [1, 2, 3, "=SUM(A2:C2)"] +wb.add_worksheet(:name => "Auto Filter") do |sheet| + sheet.add_row ["Build Matrix"] + sheet.add_row ["Build", "Duration", "Finished", "Rvm"] + sheet.add_row ["19.1", "1 min 32 sec", "about 10 hours ago", "1.8.7"] + sheet.add_row ["19.2", "1 min 28 sec", "about 10 hours ago", "1.9.2"] + sheet.add_row ["19.3", "1 min 35 sec", "about 10 hours ago", "1.9.3"] + sheet.auto_filter = "A2:D5" end ``` @@ -261,20 +278,6 @@ wb.add_worksheet(:name => "Automatic cell types") do |sheet| end ``` -##Merging Cells. - -```ruby -wb.add_worksheet(:name => 'Merging Cells') do |sheet| - # cell level style overides when adding cells - sheet.add_row ["col 1", "col 2", "col 3", "col 4"], :sz => 16 - sheet.add_row [1, 2, 3, "=SUM(A2:C2)"] - sheet.add_row [2, 3, 4, "=SUM(A3:C3)"] - sheet.add_row ["total", "", "", "=SUM(D2:D3)"] - sheet.merge_cells("A4:C4") - sheet["A1:D1"].each { |c| c.color = "FF0000"} - sheet["A1:D4"].each { |c| c.style = Axlsx::STYLE_THIN_BORDER } -end -``` ##Generating A Bar Chart @@ -368,18 +371,6 @@ wb.add_worksheet(:name => "Scatter Chart") do |sheet| end ``` -##Auto Filter - -```ruby -wb.add_worksheet(:name => "Auto Filter") do |sheet| - sheet.add_row ["Build Matrix"] - sheet.add_row ["Build", "Duration", "Finished", "Rvm"] - sheet.add_row ["19.1", "1 min 32 sec", "about 10 hours ago", "1.8.7"] - sheet.add_row ["19.2", "1 min 28 sec", "about 10 hours ago", "1.9.2"] - sheet.add_row ["19.3", "1 min 35 sec", "about 10 hours ago", "1.9.3"] - sheet.auto_filter = "A2:D5" -end -``` ##Tables @@ -394,15 +385,6 @@ wb.add_worksheet(:name => "Table") do |sheet| end ``` -##Specifying Column Widths - -```ruby -wb.add_worksheet(:name => "custom column widths") do |sheet| - sheet.add_row ["I use autowidth and am very wide", "I use a custom width and am narrow"] - sheet.add_row ['abcdefg', 'This is a very long text and should flow into the right cell', nil, 'xxx' ] - sheet.column_widths nil, 3, 5, nil -end -``` ##Fit to page printing @@ -413,6 +395,7 @@ wb.add_worksheet(:name => "fit to page") do |sheet| end ``` + ##Hide Gridlines in worksheet ```ruby @@ -422,6 +405,7 @@ wb.add_worksheet(:name => "No Gridlines") do |sheet| end ``` + ##Specify Page Margins for printing ```ruby @@ -431,11 +415,16 @@ wb.add_worksheet(:name => "print margins", :page_margins => margins) do |sheet| end ``` + ##Validate and Serialize ```ruby +# Serialize directly to file p.serialize("example.xlsx") +# or + +#Serialize to a stream s = p.to_stream() File.open('example_streamed.xlsx', 'w') { |f| f.write(s.read) } ``` @@ -443,10 +432,12 @@ File.open('example_streamed.xlsx', 'w') { |f| f.write(s.read) } ##Using Shared Strings ```ruby +# This is required by Numbers p.use_shared_strings = true p.serialize("shared_strings_example.xlsx") ``` + ##Disabling Autowidth ```ruby @@ -460,6 +451,8 @@ p.validate.each { |e| puts e.message } p.serialize("no-use_autowidth.xlsx") ``` +There is much, much more you can do with this gem. If you get stuck, grab me on IRC or submit an issue to Github. Chances are that it has already been implemented. If it hasn't - let's take a look at adding it in. + #Documentation -------------- This gem is 100% documented with YARD, an exceptional documentation library. To see documentation for this, and all the gems installed on your system use: @@ -474,7 +467,7 @@ This gem has 100% test coverage using test/unit. To execute tests for this gem, #Change log --------- -- ** April.??.12:**: 1.1.4 release +- ** April.29.12:**: 1.1.4 release - bugfix in val_axis_data to properly serialize value axis data. Excel does not mind as it reads from the sheet, but nokogiri has a fit if the elements are empty. - Added support for specifying the color of data series in charts. - bugfix using add_cell on row mismanaged calls to update_column_info. diff --git a/examples/example.rb b/examples/example.rb index 0a4f3095..8ad5f2a7 100644 --- a/examples/example.rb +++ b/examples/example.rb @@ -1,71 +1,140 @@ #!/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 +#``` #A Simple Workbook +#```ruby wb.add_worksheet(:name => "Basic Worksheet") do |sheet| sheet.add_row ["First Column", "Second", "Third"] sheet.add_row [1, 2, 3] end +#``` #Using Custom Styles +#```ruby +#Each cell allows a single, predified style. When using add_row, the value in the :style array at the same index as the cell's column will be applied to that cell. Alternatively, you can apply a style to an entire row by using an integer value for :style. + wb.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 } wb.add_worksheet(:name => "Custom 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 +#``` + +##Styling Cell Overrides + +#```ruby +#Some of the style attributes can also be set at the cell level. Cell level styles take precedence over Custom Styles shown in the previous example. + +wb.add_worksheet(:name => "Cell Level Style Overrides") do |sheet| + + # this will set the font size for each cell. + sheet.add_row ['col 1', 'col 2', 'col 3', 'col 4'], :sz => 16 + + sheet.add_row [1, 2, 3, "=SUM(A2:C2)"] + + # You can also apply cell style overrides to a range of cells + sheet["A1:D1"].each { |c| c.color = "FF0000" } + sheet['A1:D2'].each { |c| c.style = Axlsx::STYLE_THIN_BORDER } +end +#``` #Using Custom Border Styles +#```ruby +#Axlsx defines a thin border style, but you can easily create and use your own. wb.styles do |s| - red_border = s.add_style :border => {:style=>:thin, :color =>"FFFF0000"} - blue_border = s.add_style :border => {:style=>:thin, :color =>"FF0000FF"} + red_border = s.add_style :border => { :style => :thick, :color =>"FFFF0000" } + blue_border = s.add_style :border => { :style => :thick, :color =>"FF0000FF" } wb.add_worksheet(:name => "Custom Borders") do |sheet| sheet.add_row ["wrap", "me", "Up in Red"], :style => red_border sheet.add_row [1, 2, 3], :style => blue_border end end +#``` -##Using Custom Formatting and date1904 -require 'date' +##Styling Rows and Columns + +#```ruby wb.styles do |s| - date = s.add_style(:format_code => "yyyy-mm-dd", :border => Axlsx::STYLE_THIN_BORDER) - padded = s.add_style(:format_code => "00#", :border => Axlsx::STYLE_THIN_BORDER) - percent = s.add_style(:format_code => "###0%", :border => Axlsx::STYLE_THIN_BORDER) - # wb.date1904 = true # Use the 1904 date system (Used by Excel for Mac < 2011) - wb.add_worksheet(:name => "Formatting Data") do |sheet| - sheet.add_row ["Custom Formatted Date", "Percent Formatted Float", "Padded Numbers"], :style => Axlsx::STYLE_THIN_BORDER - sheet.add_row [Date::strptime('2012-01-19','%Y-%m-%d'), 0.2, 32], :style => [date, percent, padded] + head = s.add_style :bg_color => "00", :fg_color => "FF" + percent = s.add_style :num_fmt => 9 + wb.add_worksheet(:name => "Columns and Rows") do |sheet| + sheet.add_row ['col 1', 'col 2', 'col 3', 'col 4', 'col5'] + sheet.add_row [1, 2, 0.3, 4, 5.0] + sheet.add_row [1, 2, 0.2, 4, 5.0] + sheet.add_row [1, 2, 0.1, 4, 5.0] + + #apply the percent style to the column at index 2 skipping the first row. + sheet.col_style 2, percent, :row_offset => 1 + + # apply the head style to the first row. + sheet.row_style 0, head + + #Hide the 5th column + sheet.column_info[4].hidden = true + + #Set the second column outline level + sheet.column_info[1].outlineLevel = 2 + + #TODO rows hidden, outline etc. + sheet.rows[3].hidden = true + sheet.rows[1].outlineLevel = 2 end end +#``` -##Add an Image +##Specifying Column Widths -wb.add_worksheet(:name => "Images") do |sheet| - img = File.expand_path('../image1.jpeg', __FILE__) - sheet.add_image(:image_src => img, :noSelect => true, :noMove => true) do |image| - image.width=720 - image.height=666 - image.start_at 2, 2 - end +#```ruby +wb.add_worksheet(:name => "custom column widths") do |sheet| + sheet.add_row ["I use autowidth and am very wide", "I use a custom width and am narrow"] + sheet.add_row ['abcdefg', 'This is a very long text and should flow into the right cell', nil, 'xxx' ] + sheet.column_widths nil, 3, 5, nil +end +#``` + +##Merging Cells. + +#```ruby +wb.add_worksheet(:name => 'Merging Cells') do |sheet| + # cell level style overides when adding cells + sheet.add_row ["col 1", "col 2", "col 3", "col 4"], :sz => 16 + sheet.add_row [1, 2, 3, "=SUM(A2:C2)"] + sheet.add_row [2, 3, 4, "=SUM(A3:C3)"] + sheet.add_row ["total", "", "", "=SUM(D2:D3)"] + sheet.merge_cells("A4:C4") + sheet["A1:D1"].each { |c| c.color = "FF0000"} + sheet["A1:D4"].each { |c| c.style = Axlsx::STYLE_THIN_BORDER } end +#``` ##Add an Image with a hyperlink +#```ruby wb.add_worksheet(:name => "Image with Hyperlink") do |sheet| img = File.expand_path('../image1.jpeg', __FILE__) + # specifying the :hyperlink option will add a hyper link to your image. + # @note - Numbers does not support this part of the specification. sheet.add_image(:image_src => img, :noSelect => true, :noMove => true, :hyperlink=>"http://axlsx.blogspot.com") do |image| image.width=720 image.height=666 @@ -73,99 +142,71 @@ wb.add_worksheet(:name => "Image with Hyperlink") do |sheet| image.start_at 2, 2 end end +#``` -##Asian Language Support - -wb.add_worksheet(:name => "日本語でのシート名") do |sheet| - sheet.add_row ["日本語"] - sheet.add_row ["华语/華語"] - sheet.add_row ["한국어/조선말"] -end - -##Styling Columns - -wb.styles do |s| - percent = s.add_style :num_fmt => 9 - wb.add_worksheet(:name => "Styling Columns") do |sheet| - sheet.add_row ['col 1', 'col 2', 'col 3', 'col 4'] - sheet.add_row [1, 2, 0.3, 4] - sheet.add_row [1, 2, 0.2, 4] - sheet.add_row [1, 2, 0.1, 4] - sheet.col_style 2, percent, :row_offset => 1 - end -end - -##Hiding Columns - -wb.styles do |s| - percent = s.add_style :num_fmt => 9 - wb.add_worksheet(:name => "Hidden Column") do |sheet| - sheet.add_row ['col 1', 'col 2', 'col 3', 'col 4'] - sheet.add_row [1, 2, 0.3, 4] - sheet.add_row [1, 2, 0.2, 4] - sheet.add_row [1, 2, 0.1, 4] - sheet.col_style 2, percent, :row_offset => 1 - sheet.column_info[1].hidden = true - end -end - -##Styling Rows +##Using Custom Formatting and date1904 +#```ruby +require 'date' wb.styles do |s| - head = s.add_style :bg_color => "00", :fg_color => "FF" - percent = s.add_style :num_fmt => 9 - wb.add_worksheet(:name => "Styling Rows") do |sheet| - sheet.add_row ['col 1', 'col 2', 'col 3', 'col 4'] - sheet.add_row [1, 2, 0.3, 4] - sheet.add_row [1, 2, 0.2, 4] - sheet.add_row [1, 2, 0.1, 4] - sheet.col_style 2, percent, :row_offset => 1 - sheet.row_style 0, head + date = s.add_style(:format_code => "yyyy-mm-dd", :border => Axlsx::STYLE_THIN_BORDER) + padded = s.add_style(:format_code => "00#", :border => Axlsx::STYLE_THIN_BORDER) + percent = s.add_style(:format_code => "0000%", :border => Axlsx::STYLE_THIN_BORDER) + # wb.date1904 = true # Use the 1904 date system (Used by Excel for Mac < 2011) + wb.add_worksheet(:name => "Formatting Data") do |sheet| + sheet.add_row ["Custom Formatted Date", "Percent Formatted Float", "Padded Numbers"], :style => Axlsx::STYLE_THIN_BORDER + sheet.add_row [Date::strptime('2012-01-19','%Y-%m-%d'), 0.2, 32], :style => [date, percent, padded] end end +#``` -##Styling Cell Overrides +##Asian Language Support -wb.add_worksheet(:name => "Cell Level Style Overrides") do |sheet| - # cell level style overides when adding cells - sheet.add_row ['col 1', 'col 2', 'col 3', 'col 4'], :sz => 16 - sheet.add_row [1, 2, 3, "=SUM(A2:C2)"] - # cell level style overrides via sheet range - sheet["A1:D1"].each { |c| c.color = "FF0000"} - sheet['A1:D2'].each { |c| c.style = Axlsx::STYLE_THIN_BORDER } +#```ruby +wb.add_worksheet(:name => "日本語でのシート名") do |sheet| + sheet.add_row ["日本語"] + sheet.add_row ["华语/華語"] + sheet.add_row ["한국어/조선말"] end +#``` ##Using formula +#```ruby wb.add_worksheet(:name => "Using Formulas") do |sheet| sheet.add_row ['col 1', 'col 2', 'col 3', 'col 4'] sheet.add_row [1, 2, 3, "=SUM(A2:C2)"] end +#``` + +##Auto Filter + +#```ruby +wb.add_worksheet(:name => "Auto Filter") do |sheet| + sheet.add_row ["Build Matrix"] + sheet.add_row ["Build", "Duration", "Finished", "Rvm"] + sheet.add_row ["19.1", "1 min 32 sec", "about 10 hours ago", "1.8.7"] + sheet.add_row ["19.2", "1 min 28 sec", "about 10 hours ago", "1.9.2"] + sheet.add_row ["19.3", "1 min 35 sec", "about 10 hours ago", "1.9.3"] + sheet.auto_filter = "A2:D5" +end +#``` ##Automatic cell types +#```ruby wb.add_worksheet(:name => "Automatic cell types") do |sheet| + date_format = wb.styles.add_style :format_code => 'YYYY-MM-DD' + time_format = wb.styles.add_style :format_code => 'hh:mm:ss' sheet.add_row ["Date", "Time", "String", "Boolean", "Float", "Integer"] - date_format = wb.styles.add_style :format_code => 'YYYY-MMM-DD' - time_format = wb.styles.add_style :format_code => 'HH:MM:SS' sheet.add_row [Date.today, Time.now, "value", true, 0.1, 1], :style => [date_format, time_format] end +#``` -##Merging Cells. - -wb.add_worksheet(:name => 'Merging Cells') do |sheet| - # cell level style overides when adding cells - sheet.add_row ["col 1", "col 2", "col 3", "col 4"], :sz => 16 - sheet.add_row [1, 2, 3, "=SUM(A2:C2)"] - sheet.add_row [2, 3, 4, "=SUM(A3:C3)"] - sheet.add_row ["total", "", "", "=SUM(D2:D3)"] - sheet.merge_cells("A4:C4") - sheet["A1:D1"].each { |c| c.color = "FF0000"} - sheet["A1:D4"].each { |c| c.style = Axlsx::STYLE_THIN_BORDER } -end ##Generating A Bar Chart +#```ruby wb.add_worksheet(:name => "Bar Chart") do |sheet| sheet.add_row ["A Simple Bar Chart"] sheet.add_row ["First", "Second", "Third"] @@ -176,8 +217,11 @@ wb.add_worksheet(:name => "Bar Chart") do |sheet| chart.catAxis.label_rotation = 45 end end +#``` ##Hide Gridlines in chart + +#```ruby wb.add_worksheet(:name => "Chart With No Gridlines") do |sheet| sheet.add_row ["A Simple Bar Chart"] sheet.add_row ["First", "Second", "Third"] @@ -188,9 +232,11 @@ wb.add_worksheet(:name => "Chart With No Gridlines") do |sheet| chart.catAxis.gridlines = false end end +#``` ##Generating A Pie Chart +#```ruby wb.add_worksheet(:name => "Pie Chart") do |sheet| sheet.add_row ["First", "Second", "Third", "Fourth"] sheet.add_row [1, 2, 3, "=PRODUCT(A2:C2)"] @@ -198,9 +244,11 @@ wb.add_worksheet(:name => "Pie Chart") do |sheet| chart.add_series :data => sheet["A2:D2"], :labels => sheet["A1:D1"] end end +#``` ##Data over time +#```ruby wb.add_worksheet(:name=>'Charting Dates') do |sheet| # cell level style overides when adding cells sheet.add_row ['Date', 'Value'], :sz => 16 @@ -211,25 +259,29 @@ wb.add_worksheet(:name=>'Charting Dates') do |sheet| sheet.add_chart(Axlsx::Bar3DChart) do |chart| chart.start_at "B7" chart.end_at "H27" - chart.add_series(:data => sheet["B2:B5"], :labels => sheet["A2:A5"], :title => sheet["B1"], :color => 'FF0000') + chart.add_series(:data => sheet["B2:B5"], :labels => sheet["A2:A5"], :title => sheet["B1"]) end end +#``` ##Generating A Line Chart +#```ruby wb.add_worksheet(:name => "Line Chart") do |sheet| sheet.add_row ["First", 1, 5, 7, 9] sheet.add_row ["Second", 5, 2, 14, 9] sheet.add_chart(Axlsx::Line3DChart, :title => "example 6: Line Chart", :rotX => 30, :rotY => 20) do |chart| chart.start_at 0, 2 chart.end_at 10, 15 - chart.add_series :data => sheet["B1:E1"], :title => sheet["A1"], :color => "FF0000" - chart.add_series :data => sheet["B2:E2"], :title => sheet["A2"], :color => "DEDEDE" + chart.add_series :data => sheet["B1:E1"], :title => sheet["A1"] + chart.add_series :data => sheet["B2:E2"], :title => sheet["A2"] end end +#``` ##Generating A Scatter Chart +#```ruby wb.add_worksheet(:name => "Scatter Chart") do |sheet| sheet.add_row ["First", 1, 5, 7, 9] sheet.add_row ["", 1, 25, 49, 81] @@ -242,81 +294,88 @@ wb.add_worksheet(:name => "Scatter Chart") do |sheet| chart.add_series :xData => sheet["B3:E3"], :yData => sheet["B4:E4"], :title => sheet["A3"] end end +#``` -##Auto Filter -wb.add_worksheet(:name => "Auto Filter") do |sheet| +##Tables + +#```ruby +wb.add_worksheet(:name => "Table") do |sheet| sheet.add_row ["Build Matrix"] sheet.add_row ["Build", "Duration", "Finished", "Rvm"] sheet.add_row ["19.1", "1 min 32 sec", "about 10 hours ago", "1.8.7"] sheet.add_row ["19.2", "1 min 28 sec", "about 10 hours ago", "1.9.2"] sheet.add_row ["19.3", "1 min 35 sec", "about 10 hours ago", "1.9.3"] - sheet.auto_filter = "A2:D5" + sheet.add_table "A2:D5", :name => 'Build Matrix' end +#``` -##Tables - - wb.add_worksheet(:name => "Table") do |sheet| - sheet.add_row ["Build Matrix"] - sheet.add_row ["Build", "Duration", "Finished", "Rvm"] - sheet.add_row ["19.1", "1 min 32 sec", "about 10 hours ago", "1.8.7"] - sheet.add_row ["19.2", "1 min 28 sec", "about 10 hours ago", "1.9.2"] - sheet.add_row ["19.3", "1 min 35 sec", "about 10 hours ago", "1.9.3"] - sheet.add_table "A2:D5", :name => 'Build Matrix' - end - - -##Specifying Column Widths - -wb.add_worksheet(:name => "custom column widths") do |sheet| - sheet.add_row ["I use autowidth and am very wide", "I use a custom width and am narrow"] - sheet.add_row ['abcdefg', 'This is a very long text and should flow into the right cell', nil, 'xxx' ] - sheet.column_widths nil, 3, 5, nil -end ##Fit to page printing +#```ruby wb.add_worksheet(:name => "fit to page") do |sheet| sheet.add_row ['this all goes on one page'] sheet.fit_to_page = true end +#``` ##Hide Gridlines in worksheet + +#```ruby wb.add_worksheet(:name => "No Gridlines") do |sheet| sheet.add_row ["This", "Sheet", "Hides", "Gridlines"] sheet.show_gridlines = false end +#``` + ##Specify Page Margins for printing + +#```ruby margins = {:left => 3, :right => 3, :top => 1.2, :bottom => 1.2, :header => 0.7, :footer => 0.7} wb.add_worksheet(:name => "print margins", :page_margins => margins) do |sheet| sheet.add_row ["this sheet uses customized page margins for printing"] end +#``` + ##Validate and Serialize -#p.validate.each { |e| puts e.message } +#```ruby +# Serialize directly to file p.serialize("example.xlsx") +# or + +#Serialize to a stream s = p.to_stream() File.open('example_streamed.xlsx', 'w') { |f| f.write(s.read) } - +#``` ##Using Shared Strings + +#```ruby +# This is required by Numbers p.use_shared_strings = true p.serialize("shared_strings_example.xlsx") +#``` ##Disabling Autowidth + +#```ruby p = Axlsx::Package.new p.use_autowidth = false wb = p.workbook -wb.add_worksheet(:name => "No Magick") do | sheet | - sheet.add_row ['oh look! no autowidth - and no magick loaded in your process'] +wb.add_worksheet(:name => "Manual Widths") do | sheet | + sheet.add_row ['oh look! no autowidth'] end p.validate.each { |e| puts e.message } p.serialize("no-use_autowidth.xlsx") +#``` + diff --git a/lib/axlsx/workbook/worksheet/col.rb b/lib/axlsx/workbook/worksheet/col.rb index 7a34ad40..fb19a1bd 100644 --- a/lib/axlsx/workbook/worksheet/col.rb +++ b/lib/axlsx/workbook/worksheet/col.rb @@ -60,7 +60,8 @@ module Axlsx # @see Col#outline def outlineLevel=(v) - Axlsx.validate_boolean(v) + Axlsx.validate_unsigned_numeric(v) + raise ArgumentError, 'outlineLevel must be between 0 and 7' unless 0 <= v && v <= 7 @outlineLevel = v end diff --git a/lib/axlsx/workbook/worksheet/row.rb b/lib/axlsx/workbook/worksheet/row.rb index cdfa1a05..0112da75 100644 --- a/lib/axlsx/workbook/worksheet/row.rb +++ b/lib/axlsx/workbook/worksheet/row.rb @@ -5,6 +5,7 @@ module Axlsx # @see Worksheet#add_row class Row + SERIALIZABLE_ATTRIBUTES = [:hidden, :outlineLevel, :collapsed, :style] # The worksheet this row belongs to # @return [Worksheet] attr_reader :worksheet @@ -17,13 +18,29 @@ module Axlsx # @return [Float] attr_reader :height + # Flag indicating if the outlining of the affected column(s) is in the collapsed state. + # @return [Boolean] + attr_reader :collapsed + + # Flag indicating if the affected column(s) are hidden on this worksheet. + # @return [Boolean] + attr_reader :hidden + + # Outline level of affected column(s). Range is 0 to 7. + # @return [Integer] + attr_reader :outlineLevel + + # Default style for the affected column(s). Affects cells not yet allocated in the column(s). In other words, this style applies to new columns. + # @return [Integer] + attr_reader :style + # TODO 18.3.1.73 - # collapsed + # # collapsed # customFormat - # hidden - # outlineLevel + # # hidden + # # outlineLevel # ph - # s (style) + # # s (style) # spans # thickTop # thickBottom @@ -53,6 +70,25 @@ module Axlsx array_to_cells(values, options) end + # @see Row#collapsed + def collapsed=(v) + Axlsx.validate_boolean(v) + @collapsed = v + end + + # @see Row#hidden + def hidden=(v) + Axlsx.validate_boolean(v) + @hidden = v + end + + # @see Row#outline + def outlineLevel=(v) + Axlsx.validate_unsigned_numeric(v) + @outlineLevel = v + end + + # The index of this row in the worksheet # @return [Integer] def index @@ -65,6 +101,9 @@ module Axlsx # @return [String] def to_xml_string(r_index, str = '') str << '<row r="' << (r_index + 1 ).to_s << '" ' + instance_values.select { |key, value| SERIALIZABLE_ATTRIBUTES.include? key.to_sym }.each do |key, value| + str << key << '="' << value.to_s << '" ' + end if custom_height? str << 'customHeight="1" ht="' << height.to_s << '">' else |
