summaryrefslogtreecommitdiffhomepage
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/axlsx.rb12
-rw-r--r--lib/axlsx/package.rb8
-rw-r--r--lib/axlsx/workbook/workbook.rb12
-rw-r--r--lib/axlsx/workbook/worksheet/worksheet.rb25
4 files changed, 47 insertions, 10 deletions
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