diff options
| author | Randy Morgan <[email protected]> | 2012-03-21 01:24:30 +0900 |
|---|---|---|
| committer | Randy Morgan <[email protected]> | 2012-03-21 01:24:30 +0900 |
| commit | 26a8ad445d84b9dcb9fc36702ec761603a74ee20 (patch) | |
| tree | 170dce67745b8773850519ddf3603017ad820b5b | |
| parent | c7d715dea1e3829ea5110ec8bb4388f2633bf92a (diff) | |
| download | caxlsx-26a8ad445d84b9dcb9fc36702ec761603a74ee20.tar.gz caxlsx-26a8ad445d84b9dcb9fc36702ec761603a74ee20.zip | |
adding in an option to disable both the require of RMagick and auto-width processing. see Workbook#use_autowidth. Defaults to true for backward compatibility.
| -rw-r--r-- | README.md | 44 | ||||
| -rw-r--r-- | examples/example.rb | 28 | ||||
| -rw-r--r-- | lib/axlsx.rb | 12 | ||||
| -rw-r--r-- | lib/axlsx/package.rb | 8 | ||||
| -rw-r--r-- | lib/axlsx/workbook/workbook.rb | 12 | ||||
| -rw-r--r-- | lib/axlsx/workbook/worksheet/worksheet.rb | 25 |
6 files changed, 119 insertions, 10 deletions
@@ -3,11 +3,19 @@ Axlsx: Office Open XML Spreadsheet Generation [](http://travis-ci.org/randym/axlsx/) **IRC**: [irc.freenode.net / #axlsx](irc://irc.freenode.net/axlsx) + **Git**: [http://github.com/randym/axlsx](http://github.com/randym/axlsx) + +**Twitter**: [https://twitter.com/#!/morgan_randy](https://twitter.com/#!/morgan_randy) release announcements and news will be published here + **Author**: Randy Morgan + **Copyright**: 2011 + **License**: MIT License + **Latest Version**: 1.0.18 + **Ruby Version**: 1.8.7, 1.9.2, 1.9.3 **Release Date**: March 5th 2012 @@ -275,11 +283,27 @@ To install Axlsx, use the following command: end ##Specify Page Margins for printing + 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 +##Fit to page printing + + 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 + + wb.add_worksheet(:name => "No Gridlines") do |sheet| + sheet.add_row ["This", "Sheet", "Hides", "Gridlines"] + sheet.show_gridlines = false + end + ##Validate and Serialize p.validate.each { |e| puts e.message } @@ -294,6 +318,16 @@ To install Axlsx, use the following command: p.use_shared_strings = true p.serialize("shared_strings_example.xlsx") +##Disabling Autowidth + + 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'] + end + p.validate.each { |e| puts e.message } + p.serialize("no-use_autowidth.xlsx") #Documentation @@ -309,6 +343,16 @@ This gem has 100% test coverage using test/unit. To execute tests for this gem, #Changelog --------- +- ** March.??.12**: 1.0.19 release + - bugfix patch name_to_indecies to properly handle extended ranges. + - bugfix properly serialize chart title. + - lower rake minimum requirement for 1.8.7 apps that don't want to move on to 0.9 NOTE this will be reverted for 2.0.0 with workbook parsing! + - Added Fit to Page printing + - added support for turning off gridlines in charts. + - added support for turning off gridlines in worksheet. + - bugfix some apps like libraoffice require apply[x] attributes to be true. applyAlignment is not properly set. + - added option to *not* use RMagick - and default all assigned columns to the excel default of 8.43 + - ** March.5.12**: 1.0.18 release https://github.com/randym/axlsx/compare/1.0.17...1.0.18 - bugfix custom borders are not properly applied when using styles.add_style diff --git a/examples/example.rb b/examples/example.rb index df5a401f..d834a51c 100644 --- a/examples/example.rb +++ b/examples/example.rb @@ -144,6 +144,18 @@ wb.add_worksheet(:name => "Bar Chart") do |sheet| end end +##Hide Gridlines in chart +wb.add_worksheet(:name => "Chart With No Gridlines") do |sheet| + sheet.add_row ["A Simple Bar Chart"] + sheet.add_row ["First", "Second", "Third"] + sheet.add_row [1, 2, 3] + sheet.add_chart(Axlsx::Bar3DChart, :start_at => "A4", :end_at => "F17") do |chart| + chart.add_series :data => sheet["A3:C3"], :labels => sheet["A2:C2"], :title => sheet["A1"] + chart.valAxis.gridlines = false + chart.catAxis.gridlines = false + end +end + ##Generating A Pie Chart wb.add_worksheet(:name => "Pie Chart") do |sheet| @@ -201,6 +213,13 @@ wb.add_worksheet(:name => "custom column widths") do |sheet| sheet.column_widths nil, 3 end +##Fit to page printing + +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 wb.add_worksheet(:name => "No Gridlines") do |sheet| @@ -228,6 +247,15 @@ p.use_shared_strings = true p.serialize("shared_strings_example.xlsx") +##Disabling Autowidth +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'] +end +p.validate.each { |e| puts e.message } +p.serialize("no-use_autowidth.xlsx") diff --git a/lib/axlsx.rb b/lib/axlsx.rb index 8b8fb7c5..3c6361e0 100644 --- a/lib/axlsx.rb +++ b/lib/axlsx.rb @@ -25,7 +25,6 @@ require 'axlsx/package.rb' #required gems require 'nokogiri' -require 'RMagick' require 'zip/zip' #core dependencies @@ -33,8 +32,8 @@ require 'bigdecimal' require 'time' #if object does not have this already, I am borrowing it from active_support. -# I am a very big fan of activesupports instance_values method, but do not want to require nor include the entire -# library just for this one method. +# I am a very big fan of activesupports instance_values method, but do not want to require nor include the entire +# library just for this one method. if !Object.respond_to?(:instance_values) Object.send :public # patch for 1.8.7 as it uses private scope Object.send :define_method, :instance_values do @@ -42,12 +41,11 @@ if !Object.respond_to?(:instance_values) end end - # xlsx generation with charts, images, automated column width, customizable styles and full schema validation. Axlsx excels at helping you generate beautiful Office Open XML Spreadsheet documents without having to understand the entire ECMA specification. Check out the README for some examples of how easy it is. Best of all, you can validate your xlsx file before serialization so you know for sure that anything generated is going to load on your client's machine. module Axlsx # determines the cell range for the items provided def self.cell_range(items) - return "" unless items.first.is_a? Cell + return "" unless items.first.is_a? Cell ref = "'#{items.first.row.worksheet.name}'!" + "#{items.first.r_abs}" ref += ":#{items.last.r_abs}" if items.size > 1 @@ -56,8 +54,8 @@ module Axlsx def self.name_to_indices(name) raise ArgumentError, 'invalid cell name' unless name.size > 1 - v = name[/[A-Z]+/].reverse.chars.reduce({:base=>1, :i=>0}) do |val, c| - val[:i] += ((c.bytes.first - 65) + val[:base]); val[:base] *= 26; val + v = name[/[A-Z]+/].reverse.chars.reduce({:base=>1, :i=>0}) do |val, c| + val[:i] += ((c.bytes.first - 65) + val[:base]); val[:base] *= 26; val end [v[:i]-1, ((name[/[1-9][0-9]*/]).to_i)-1] diff --git a/lib/axlsx/package.rb b/lib/axlsx/package.rb index 6a8864f4..164b342b 100644 --- a/lib/axlsx/package.rb +++ b/lib/axlsx/package.rb @@ -29,6 +29,14 @@ module Axlsx yield self if block_given? end + # Shortcut to specify that the workbook should use autowidth + # @see Workbook#use_autowidth + def use_autowidth=(v) + Axlsx::validate_boolean(v); + workbook.use_autowidth = v + end + + # Shortcut to specify that the workbook should use shared strings # @see Workbook#use_shared_strings def use_shared_strings=(v) diff --git a/lib/axlsx/workbook/workbook.rb b/lib/axlsx/workbook/workbook.rb index 3350b3ff..8878526b 100644 --- a/lib/axlsx/workbook/workbook.rb +++ b/lib/axlsx/workbook/workbook.rb @@ -46,6 +46,17 @@ require 'axlsx/workbook/shared_strings_table.rb' end + # True by default. When you set this to false, the library will not include image magick nor will it attempt to set autowidths for your columns. + # @return [Boolean] + attr_reader :use_autowidth + + + # @see use_autowidth + def use_autowidth=(v) + Axlsx::validate_boolean(v) + @use_autowidth = v + end + # A collection of worksheets associated with this workbook. # @note The recommended way to manage worksheets is add_worksheet # @see Workbook#add_worksheet @@ -106,6 +117,7 @@ require 'axlsx/workbook/shared_strings_table.rb' @drawings = SimpleTypedList.new Drawing @charts = SimpleTypedList.new Chart @images = SimpleTypedList.new Pic + @use_autowidth = true self.date1904= !options[:date1904].nil? && options[:date1904] yield self if block_given? end diff --git a/lib/axlsx/workbook/worksheet/worksheet.rb b/lib/axlsx/workbook/worksheet/worksheet.rb index a514c31c..22711d15 100644 --- a/lib/axlsx/workbook/worksheet/worksheet.rb +++ b/lib/axlsx/workbook/worksheet/worksheet.rb @@ -39,6 +39,10 @@ module Axlsx # @return Boolean attr_reader :show_gridlines + # Indicates if the worksheet should print in a single page + # @return Boolean + attr_reader :fit_to_page + # Page margins for printing the worksheet. # @example # wb = Axlsx::Package.new.workbook @@ -76,8 +80,12 @@ module Axlsx @workbook.worksheets << self @auto_fit_data = [] self.name = options[:name] || "Sheet" + (index+1).to_s - - @magick_draw = Magick::Draw.new + if self.workbook.use_autowidth + require 'RMagick' unless defined?(Magick) + @magick_draw = Magick::Draw.new + else + @magick_draw = nil + end @cols = SimpleTypedList.new Cell @merged_cells = [] @@ -125,6 +133,14 @@ module Axlsx end + # Indicates if gridlines should be shown in the sheet. + # This is true by default. + # @return [Boolean] + def fit_to_page=(v) + Axlsx::validate_boolean v + @fit_to_page = v + end + # Returns the cell or cells defined using excel style A1:B3 references. # @param [String|Integer] cell_def the string defining the cell or range of cells, or the rownumber # @return [Cell, Array] @@ -347,6 +363,9 @@ module Axlsx builder = Nokogiri::XML::Builder.new(:encoding => ENCODING) do |xml| xml.worksheet(:xmlns => XML_NS, :'xmlns:r' => XML_NS_R) { + xml.sheetPr { + xml.pageSetUpPr :fitToPage => fit_to_page if fit_to_page + } # another patch for the folks at rubyXL as thier parser depends on this optional element. xml.dimension :ref=>dimension unless rows.size == 0 # this is required by rubyXL, spec says who cares - but it seems they didnt notice @@ -435,7 +454,7 @@ module Axlsx # @param [Hash] A hash of auto_fit_data def auto_width(col) return col[:fixed] unless col[:fixed] == nil - + return 8.43 unless @magick_draw mdw_count, font_scale, mdw = 0, col[:sz]/11.0, 6.0 mdw_count = col[:longest].scan(/./mu).reduce(0) do | count, char | count +=1 if @magick_draw.get_type_metrics(char).max_advance >= mdw |
