Permalink
Browse files

resolving #53 and adding specs for use_autowidth, show_gridlines

  • Loading branch information...
1 parent 26a8ad4 commit 0531fd7f4902f33e3ede66f7075dd739e5a434c5 @randym randym committed Mar 21, 2012
@@ -228,4 +228,6 @@ module Axlsx
# error message for duplicate sheet names
ERR_DUPLICATE_SHEET_NAME = "There is already a worksheet in this workbook named '%s'. Please use a unique name"
+
+ FIXED_COL_WIDTH = 8.43
end
@@ -46,18 +46,7 @@ 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.
+ # A collection of worksheets associated with this workbook.
# @note The recommended way to manage worksheets is add_worksheet
# @see Workbook#add_worksheet
# @see Worksheet
@@ -99,6 +88,7 @@ def styles
# Indicates if the epoc date for serialization should be 1904. If false, 1900 is used.
@@date1904 = false
+
# lets come back to this later when we are ready for parsing.
#def self.parse entry
# io = entry.get_input_stream
@@ -137,6 +127,14 @@ def self.date1904=(v) Axlsx::validate_boolean v; @@date1904 = v; end
# @return [Boolean]
def self.date1904() @@date1904; end
+ # Indicates if the workbook should use autowidths or not.
+ # this must be set before instantiating a worksheet to avoid Rmagix inclusion
+ # @return [Boolean]
+ def use_autowidth() @use_autowidth; end
+
+ # see @use_autowidth
+ def use_autowidth=(v) Axlsx::validate_boolean v; @use_autowidth = v; end
+
# Adds a worksheet to this workbook
# @return [Worksheet]
# @option options [String] name The name of the worksheet.
@@ -39,6 +39,13 @@ class Worksheet
# @return Boolean
attr_reader :show_gridlines
+
+ # Indicates if the worksheet is selected in the workbook
+ # It is possible to have more than one worksheet selected, however it might cause issues
+ # in some older versions of excel when using copy and paste.
+ # @return Boolean
+ attr_reader :selected
+
# Indicates if the worksheet should print in a single page
# @return Boolean
attr_reader :fit_to_page
@@ -73,23 +80,32 @@ def page_margins
# @option options [Hash] page_margins A hash containing page margins for this worksheet. @see PageMargins
# @option options [Boolean] show_gridlines indicates if gridlines should be shown for this sheet.
def initialize(wb, options={})
- @drawing = @page_margins = @auto_filter = nil
- @show_gridlines = true
- @rows = SimpleTypedList.new Row
self.workbook = wb
@workbook.worksheets << self
+
+ @drawing = @page_margins = @auto_filter = nil
+ @merged_cells = []
@auto_fit_data = []
- self.name = options[:name] || "Sheet" + (index+1).to_s
+
+ @selected = false
+ @show_gridlines = true
+ self.name = "Sheet" + (index+1).to_s
+ @page_margins = PageMargins.new options[:page_margins] if options[:page_margins]
+
+ @rows = SimpleTypedList.new Row
+ @cols = SimpleTypedList.new Cell
+
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 = []
- @page_margins = PageMargins.new options[:page_margins] if options[:page_margins]
+ options.each do |o|
+ self.send("#{o[0]}=", o[1]) if self.respond_to? "#{o[0]}="
+ end
+
end
# convinience method to access all cells in this worksheet
@@ -132,6 +148,13 @@ def show_gridlines=(v)
@show_gridlines = v
end
+ # @see selected
+ # @return [Boolean]
+ def selected=(v)
+ Axlsx::validate_boolean v
+ @selected = v
+ end
+
# Indicates if gridlines should be shown in the sheet.
# This is true by default.
@@ -369,10 +392,9 @@ def to_xml
# 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
- # however, it also seems to be causing some odd [Grouped] stuff in excel 2011 - so
- # removing until I understand it better.
+ # grouping issue resolved by keeping tabSelected set to 0
xml.sheetViews {
- xml.sheetView(:tabSelected => 1, :workbookViewId => 0, :showGridLines => show_gridlines) {
+ xml.sheetView(:tabSelected => @selected, :workbookViewId => 0, :showGridLines => show_gridlines) {
xml.selection :activeCell=>"A1", :sqref => "A1"
}
}
@@ -454,7 +476,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
+ return Axlsx::FIXED_COL_WIDTH unless self.workbook.use_autowidth
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
@@ -2,14 +2,21 @@
require 'axlsx.rb'
class TestWorkbook < Test::Unit::TestCase
- def setup
+ def setup
p = Axlsx::Package.new
- @wb = p.workbook
+ @wb = p.workbook
end
def teardown
end
+ def test_no_autowidth
+ assert_equal(@wb.use_autowidth, true)
+ assert_raise(ArgumentError) {@wb.use_autowidth = 0.1}
+ assert_nothing_raised {@wb.use_autowidth = false}
+ assert_equal(@wb.use_autowidth, false)
+ end
+
def test_date1904
assert_equal(Axlsx::Workbook.date1904, @wb.date1904)
@wb.date1904 = :false
@@ -18,6 +25,8 @@ def test_date1904
assert_equal(Axlsx::Workbook.date1904, @wb.date1904)
end
+
+
def test_shared_strings
assert_equal(@wb.use_shared_strings, nil)
assert_raise(ArgumentError) {@wb.use_shared_strings = 'bpb'}
@@ -2,19 +2,20 @@
require 'axlsx.rb'
class TestWorksheet < Test::Unit::TestCase
- def setup
+ def setup
p = Axlsx::Package.new
@ws = p.workbook.add_worksheet
end
+
def test_pn
assert_equal(@ws.pn, "worksheets/sheet1.xml")
ws = @ws.workbook.add_worksheet
assert_equal(ws.pn, "worksheets/sheet2.xml")
end
def test_page_margins
- assert(@ws.page_margins.is_a? Axlsx::PageMargins)
+ assert(@ws.page_margins.is_a? Axlsx::PageMargins)
end
def test_page_margins_yeild
@@ -24,16 +25,37 @@ def test_page_margins_yeild
end
end
+ def test_no_autowidth
+ @ws.workbook.use_autowidth = false
+ @ws.add_row [1,2,3,4]
+ assert_equal(@ws.send(:auto_width, @ws.auto_fit_data[0]), Axlsx::FIXED_COL_WIDTH)
+ end
+
def test_initialization_options
page_margins = {:left => 2, :right => 2, :bottom => 2, :top => 2, :header => 2, :footer => 2}
- optioned = @ws.workbook.add_worksheet(:name => 'bob', :page_margins => page_margins)
+ optioned = @ws.workbook.add_worksheet(:name => 'bob', :page_margins => page_margins, :selected => true, :show_gridlines => false)
assert_equal(optioned.page_margins.left, page_margins[:left])
assert_equal(optioned.page_margins.right, page_margins[:right])
assert_equal(optioned.page_margins.top, page_margins[:top])
assert_equal(optioned.page_margins.bottom, page_margins[:bottom])
assert_equal(optioned.page_margins.header, page_margins[:header])
assert_equal(optioned.page_margins.footer, page_margins[:footer])
assert_equal(optioned.name, 'bob')
+ assert_equal(optioned.selected, true)
+ assert_equal(optioned.show_gridlines, false)
+ end
+
+
+ def test_use_gridlines
+ assert_raise(ArgumentError) { @ws.show_gridlines = -1.1 }
+ assert_nothing_raised { @ws.show_gridlines = false }
+ assert_equal(@ws.show_gridlines, false)
+ end
+
+ def test_selected
+ assert_raise(ArgumentError) { @ws.selected = -1.1 }
+ assert_nothing_raised { @ws.selected = true }
+ assert_equal(@ws.selected, true)
end
def test_rels_pn
@@ -47,7 +69,7 @@ def test_rId
ws = @ws.workbook.add_worksheet
assert_equal(ws.rId, "rId2")
end
-
+
def test_index
assert_equal(@ws.index, @ws.workbook.worksheets.index(@ws))
end
@@ -68,7 +90,7 @@ def test_referencing
assert_equal(@ws.rows[1],last_row)
assert_equal(range.size, 6)
assert_equal(range.first, @ws.rows.first.cells.first)
- assert_equal(range.last, @ws.rows.last.cells.last)
+ assert_equal(range.last, @ws.rows.last.cells.last)
end
def test_add_row
@@ -94,7 +116,7 @@ def test_col_style
@ws.add_row [1,2,3,4]
@ws.add_row [1,2,3,4]
@ws.col_style( (1..2), 1, :row_offset=>1)
- @ws.rows[(1..-1)].each do | r |
+ @ws.rows[(1..-1)].each do | r |
assert_equal(r.cells[1].style, 1)
assert_equal(r.cells[2].style, 1)
end
@@ -115,7 +137,7 @@ def test_cols
@ws.add_row [1,2,3,4]
@ws.add_row [1,2,3,4]
c = @ws.cols[1]
- assert_equal(c.size, 4)
+ assert_equal(c.size, 4)
assert_equal(c[0].value, 2)
end
@@ -125,13 +147,13 @@ def test_row_style
@ws.add_row [1,2,3,4]
@ws.add_row [1,2,3,4]
@ws.row_style 1, 1, :col_offset=>1
- @ws.rows[1].cells[(1..-1)].each do | c |
+ @ws.rows[1].cells[(1..-1)].each do | c |
assert_equal(c.style, 1)
end
assert_equal(@ws.rows[1].cells[0].style, 0)
assert_equal(@ws.rows[2].cells[1].style, 0)
end
-
+
def test_to_xml
schema = Nokogiri::XML::Schema(File.open(Axlsx::SML_XSD))
doc = Nokogiri::XML(@ws.to_xml)
@@ -153,7 +175,7 @@ def test_valid_with_page_margins
puts error.message
end
assert(errors.empty?, "error free validation")
-
+
end
def test_relationships
@@ -164,7 +186,7 @@ def test_relationships
assert_equal(@ws.relationships.size, 1, "multiple charts still only result in one relationship")
end
-
+
def test_name_unique
assert_raise(ArgumentError, "worksheet name must be unique") { n = @ws.name; @ws.workbook.add_worksheet(:name=> @ws) }
end
@@ -205,7 +227,7 @@ def test_fixed_widths_with_merged_cells
@ws.add_row ["but Im Short!"], :widths=> [14.8]
assert_equal(@ws.send(:auto_width, @ws.auto_fit_data[0]), 14.8)
end
-
+
def test_fixed_width_to_auto
@ws.add_row ["hey, I'm like really long and stuff so I think you will merge me."]
@ws.merge_cells "A1:C1"
@@ -240,7 +262,7 @@ def test_set_column_width
def test_merge_cells
assert(@ws.merged_cells.is_a?(Array))
- assert_equal(@ws.merged_cells.size, 0)
+ assert_equal(@ws.merged_cells.size, 0)
@ws.add_row [1,2,3]
@ws.add_row [4,5,6]
@ws.add_row [7,8,9]

0 comments on commit 0531fd7

Please sign in to comment.