Skip to content

Commit

Permalink
adding in an option to disable both the require of RMagick and auto-w…
Browse files Browse the repository at this point in the history
…idth processing. see Workbook#use_autowidth. Defaults to true for backward compatibility.
  • Loading branch information
Randy Morgan committed Mar 20, 2012
1 parent c7d715d commit 26a8ad4
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 10 deletions.
44 changes: 44 additions & 0 deletions README.md
Expand Up @@ -3,11 +3,19 @@ Axlsx: Office Open XML Spreadsheet Generation
[![Build Status](https://secure.travis-ci.org/randym/axlsx.png)](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
Expand Down Expand Up @@ -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 }
Expand All @@ -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
Expand All @@ -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
Expand Down
28 changes: 28 additions & 0 deletions examples/example.rb
Expand Up @@ -144,6 +144,18 @@
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|
Expand Down Expand Up @@ -201,6 +213,13 @@
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|
Expand Down Expand Up @@ -228,6 +247,15 @@
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")



12 changes: 5 additions & 7 deletions lib/axlsx.rb
Expand Up @@ -25,29 +25,27 @@

#required gems
require 'nokogiri'
require 'RMagick'
require 'zip/zip'

#core dependencies
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
Hash[instance_variables.map { |name| [name.to_s[1..-1], instance_variable_get(name)] }]
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
Expand All @@ -56,8 +54,8 @@ def self.cell_range(items)

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]
Expand Down
8 changes: 8 additions & 0 deletions lib/axlsx/package.rb
Expand Up @@ -29,6 +29,14 @@ def initialize(options={})
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)
Expand Down
12 changes: 12 additions & 0 deletions lib/axlsx/workbook/workbook.rb
Expand Up @@ -46,6 +46,17 @@ def use_shared_strings=(v)
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
Expand Down Expand Up @@ -106,6 +117,7 @@ def initialize(options={})
@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
Expand Down
25 changes: 22 additions & 3 deletions lib/axlsx/workbook/worksheet/worksheet.rb
Expand Up @@ -39,6 +39,10 @@ class Worksheet
# @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
Expand Down Expand Up @@ -76,8 +80,12 @@ def initialize(wb, options={})
@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 = []

Expand Down Expand Up @@ -125,6 +133,14 @@ def show_gridlines=(v)
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]
Expand Down Expand Up @@ -347,6 +363,9 @@ def to_xml
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
Expand Down Expand Up @@ -435,7 +454,7 @@ def update_auto_fit_data(cells, widths=[])
# @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
Expand Down

0 comments on commit 26a8ad4

Please sign in to comment.