summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorRandy Morgan <[email protected]>2013-09-29 13:14:53 +0900
committerRandy Morgan <[email protected]>2013-09-29 13:14:53 +0900
commit097b7da7eb6b29907da28c174e83ff22a6358176 (patch)
tree152f5bf7c0a85568eb1beb230290d397cc103af0
parent90fcca29df390de244e2b16ef0279fd373482981 (diff)
downloadcaxlsx-097b7da7eb6b29907da28c174e83ff22a6358176.tar.gz
caxlsx-097b7da7eb6b29907da28c174e83ff22a6358176.zip
Added workbook views collection and workbook view object
-rw-r--r--lib/axlsx/workbook/workbook_view.rb78
-rw-r--r--lib/axlsx/workbook/workbook_views.rb22
-rw-r--r--test/workbook/tc_workbook_view.rb47
3 files changed, 147 insertions, 0 deletions
diff --git a/lib/axlsx/workbook/workbook_view.rb b/lib/axlsx/workbook/workbook_view.rb
new file mode 100644
index 00000000..11eae571
--- /dev/null
+++ b/lib/axlsx/workbook/workbook_view.rb
@@ -0,0 +1,78 @@
+# <xsd:complexType name="CT_BookView">
+# <xsd:sequence>
+# <xsd:element name="extLst" type="CT_ExtensionList" minOccurs="0" maxOccurs="1"/>
+# </xsd:sequence>
+# <xsd:attribute name="visibility" type="ST_Visibility" use="optional" default="visible"/>
+# <xsd:attribute name="minimized" type="xsd:boolean" use="optional" default="false"/>
+# <xsd:attribute name="showHorizontalScroll" type="xsd:boolean" use="optional" default="true"/>
+# <xsd:attribute name="showVerticalScroll" type="xsd:boolean" use="optional" default="true"/>
+# <xsd:attribute name="showSheetTabs" type="xsd:boolean" use="optional" default="true"/>
+# <xsd:attribute name="xWindow" type="xsd:int" use="optional"/>
+# <xsd:attribute name="yWindow" type="xsd:int" use="optional"/>
+# <xsd:attribute name="windowWidth" type="xsd:unsignedInt" use="optional"/>
+# <xsd:attribute name="windowHeight" type="xsd:unsignedInt" use="optional"/>
+# <xsd:attribute name="tabRatio" type="xsd:unsignedInt" use="optional" default="600"/>
+# <xsd:attribute name="firstSheet" type="xsd:unsignedInt" use="optional" default="0"/>
+# <xsd:attribute name="activeTab" type="xsd:unsignedInt" use="optional" default="0"/>
+# <xsd:attribute name="autoFilterDateGrouping" type="xsd:boolean" use="optional"
+# default="true"/>
+# </xsd:complexType>
+
+module Axlsx
+
+ # A BookView defines the display properties for a workbook.
+ # Units for window widths and other dimensions are expressed in twips.
+ # Twip measurements are portable between different display resolutions.
+ # The formula is (screen pixels) * (20 * 72) / (logical device dpi),
+ # where the logical device dpi can be different for x and y coordinates.
+ class WorkbookView
+
+ include Axlsx::SerializedAttributes
+ include Axlsx::OptionsParser
+ include Axlsx::Accessors
+
+
+ # Creates a new BookView object
+ # @params [Hash] options A hash of key/value pairs that will be mapped to this instances attributes.
+ # @option [Symbol] visibility Specifies visible state of the workbook window. The default value for this attribute is :visible.
+ # @option [Boolean] minimized Specifies a boolean value that indicates whether the workbook window is minimized.
+ # @option [Boolean] show_horizontal_scroll Specifies a boolean value that indicates whether to display the horizontal scroll bar in the user interface.
+ # @option [Boolean] show_vertical_scroll Specifies a boolean value that indicates whether to display the vertical scroll bar.
+ # @option [Boolean] show_sheet_tabs Specifies a boolean value that indicates whether to display the sheet tabs in the user interface.
+ # @option [Integer] tab_ratio Specifies ratio between the workbook tabs bar and the horizontal scroll bar.
+ # @option [Integer] first_sheet Specifies the index to the first sheet in this book view.
+ # @option [Integer] active_tab Specifies an unsignedInt that contains the index to the active sheet in this book view.
+ # @option [Integer] x_window Specifies the X coordinate for the upper left corner of the workbook window. The unit of measurement for this value is twips.
+ # @option [Integer] y_window Specifies the Y coordinate for the upper left corner of the workbook window. The unit of measurement for this value is twips.
+ # @option [Integer] window_width Specifies the width of the workbook window. The unit of measurement for this value is twips.
+ # @option [Integer] window_height Specifies the height of the workbook window. The unit of measurement for this value is twips.
+ # @option [Boolean] auto_filter_date_grouping Specifies a boolean value that indicates whether to group dates when presenting the user with filtering options in the user interface.
+ def initialize(options={})
+ parse_options options
+ yield self if block_given?
+ end
+
+
+ unsigned_int_attr_accessor :x_window, :y_window, :window_width, :window_height,
+ :tab_ratio, :first_sheet, :active_tab
+
+ validated_attr_accessor [:visibility], :validate_view_visibility
+
+ serializable_attributes :visibility, :minimized,
+ :show_horizontal_scroll, :show_vertical_scroll,
+ :show_sheet_tabs, :tab_ratio, :first_sheet, :active_tab,
+ :x_window, :y_window, :window_width, :window_height,
+ :auto_filter_date_grouping
+
+ boolean_attr_accessor :minimized, :show_horizontal_scroll, :show_vertical_scroll,
+ :show_sheet_tabs, :auto_filter_date_grouping
+
+
+
+ def to_xml_string(str = '')
+ str << '<workbookView '
+ serialized_attributes str
+ str << '></workbookView>'
+ end
+ end
+end
diff --git a/lib/axlsx/workbook/workbook_views.rb b/lib/axlsx/workbook/workbook_views.rb
new file mode 100644
index 00000000..4e69f19e
--- /dev/null
+++ b/lib/axlsx/workbook/workbook_views.rb
@@ -0,0 +1,22 @@
+module Axlsx
+ # a simple types list of BookView objects
+ class WorkbookViews < SimpleTypedList
+
+ # creates the book views object
+ def initialize
+ super WorkbookView
+ end
+
+ # Serialize to xml
+ # @param [String] str
+ # @return [String]
+ def to_xml_string(str = '')
+ return if @list.empty?
+ str << "<bookViews>"
+ each { |view| view.to_xml_string(str) }
+ str << '</bookViews>'
+ end
+ end
+end
+
+
diff --git a/test/workbook/tc_workbook_view.rb b/test/workbook/tc_workbook_view.rb
new file mode 100644
index 00000000..73f39a9c
--- /dev/null
+++ b/test/workbook/tc_workbook_view.rb
@@ -0,0 +1,47 @@
+require 'tc_helper'
+
+class TestWorkbookView < Test::Unit::TestCase
+
+ def setup
+ @options = { visibility: :hidden, minimized: true, show_horizontal_scroll: true, show_vertical_scroll: true,
+ show_sheet_tabs: true, tab_ratio: 750, first_sheet: 0, active_tab: 1, x_window: 500, y_window: 400,
+ window_width: 800, window_height: 600, auto_filter_date_grouping: true }
+ @book_view = Axlsx::WorkbookView.new @options
+ end
+
+ def test_options_assignation
+ @options.each do |key, value|
+ assert_equal(value, @book_view.send(key))
+ end
+ end
+
+ def test_boolean_attribute_validation
+ %w(minimized show_horizontal_scroll show_vertical_scroll show_sheet_tabs auto_filter_date_grouping).each do |attr|
+ assert_raise(ArgumentError, 'only booleanish allowed in boolean attributes') { @book_view.send("#{attr}=", "banana") }
+ assert_nothing_raised { @book_view.send("#{attr}=", false )}
+ end
+ end
+
+ def test_integer_attribute_validation
+ %w(tab_ratio first_sheet active_tab x_window y_window window_width window_height).each do |attr|
+ assert_raise(ArgumentError, 'only integer allowed in integer attributes') { @book_view.send("#{attr}=", "b") }
+ assert_nothing_raised { @book_view.send("#{attr}=", 7 )}
+ end
+ end
+
+ def test_visibility_attribute_validation
+ assert_raise(ArgumentError) { @book_view.visibility = :foobar }
+ assert_nothing_raised { @book_view.visibility = :hidden }
+ assert_nothing_raised { @book_view.visibility = :very_hidden }
+ assert_nothing_raised { @book_view.visibility = :visible }
+ end
+
+ def test_to_xml_string
+ xml = @book_view.to_xml_string
+ doc = Nokogiri::XML(xml)
+ @options.each do |key, value|
+ path = "workbookView[@#{Axlsx.camel(key, false)}='#{value}']"
+ assert_equal(1, doc.xpath(path).size)
+ end
+ end
+end