summaryrefslogtreecommitdiffhomepage
path: root/lib/axlsx/stylesheet
diff options
context:
space:
mode:
authorRandy Morgan <[email protected]>2011-11-20 23:22:04 +0900
committerRandy Morgan <[email protected]>2011-11-20 23:22:04 +0900
commite53f04284618713b0a90b7a691425c380e829476 (patch)
tree801fea138160f9af426d62bf94ad5bf97123ece9 /lib/axlsx/stylesheet
downloadcaxlsx-e53f04284618713b0a90b7a691425c380e829476.tar.gz
caxlsx-e53f04284618713b0a90b7a691425c380e829476.zip
first commit
Diffstat (limited to 'lib/axlsx/stylesheet')
-rw-r--r--lib/axlsx/stylesheet/border.rb52
-rw-r--r--lib/axlsx/stylesheet/border_pr.rb65
-rw-r--r--lib/axlsx/stylesheet/cell_alignment.rb96
-rw-r--r--lib/axlsx/stylesheet/cell_protection.rb33
-rw-r--r--lib/axlsx/stylesheet/cell_style.rb60
-rw-r--r--lib/axlsx/stylesheet/color.rb57
-rw-r--r--lib/axlsx/stylesheet/fill.rb31
-rw-r--r--lib/axlsx/stylesheet/font.rb128
-rw-r--r--lib/axlsx/stylesheet/gradient_fill.rb70
-rw-r--r--lib/axlsx/stylesheet/gradient_stop.rb31
-rw-r--r--lib/axlsx/stylesheet/num_fmt.rb61
-rw-r--r--lib/axlsx/stylesheet/pattern_fill.rb64
-rw-r--r--lib/axlsx/stylesheet/styles.rb296
-rw-r--r--lib/axlsx/stylesheet/table_style.rb44
-rw-r--r--lib/axlsx/stylesheet/table_style_element.rb66
-rw-r--r--lib/axlsx/stylesheet/table_styles.rb39
-rw-r--r--lib/axlsx/stylesheet/xf.rb117
17 files changed, 1310 insertions, 0 deletions
diff --git a/lib/axlsx/stylesheet/border.rb b/lib/axlsx/stylesheet/border.rb
new file mode 100644
index 00000000..167b42c9
--- /dev/null
+++ b/lib/axlsx/stylesheet/border.rb
@@ -0,0 +1,52 @@
+module Axlsx
+ # This class details a border used in Office Open XML spreadsheet styles.
+ class Border
+
+ # @return [Boolean] The diagonal up property for the border that indicates if the border should include a diagonal line from the bottom left to the top right of the cell.
+ attr_accessor :diagonalUp
+
+ # @return [Boolean] The diagonal down property for the border that indicates if the border should include a diagonal line from the top left to the top right of the cell.
+ attr_accessor :diagonalDown
+
+ # @return [Boolean] The outline property for the border indicating that top, left, right and bottom borders should only be applied to the outside border of a range of cells.
+ attr_accessor :outline
+
+ # @return [SimpleTypedList] A list of BorderPr objects for this border.
+ attr_reader :prs
+
+ # Creates a new Border object
+ # @option options [Boolean] diagonalUp
+ # @option options [Boolean] diagonalDown
+ # @option options [Boolean] outline
+ # @example Making a border
+ # p = Package.new
+ # red_border = Border.new
+ # [:left, :right, :top, :bottom].each do |item|
+ # red_border.prs << BorderPr.new(:name=>item, :style=>:thin, :color=>Color.new(:rgb=>"FFFF0000")) #
+ # end
+ # # this sets red_border to be the index for the created border.
+ # red_border = p.workbook.styles.@borders << red_border
+ # #used in row creation as follows. This will add a red border to each of the cells in the row.
+ # p.workbook.add_worksheet.rows << :values=>[1,2,3] :style=>red_border
+ def initialize(options={})
+ @prs = SimpleTypedList.new BorderPr
+ options.each do |o|
+ self.send("#{o[0]}=", o[1]) if self.respond_to? "#{o[0]}="
+ end
+ end
+
+ def diagonalUp=(v) Axlsx::validate_boolean v; @diagonalUp = v end
+ def diagonalDown=(v) Axlsx::validate_boolean v; @diagonalDown = v end
+ def outline=(v) Axlsx::validate_boolean v; @outline = v end
+
+ # Serializes the border element
+ # @param [Nokogiri::XML::Builder] xml The document builder instance this objects xml will be added to.
+ def to_xml(xml)
+ xml.border(self.instance_values.select{ |k,v| [:diagonalUp, :diagonalDown, :outline].include? k }) {
+ [:start, :end, :left, :right, :top, :bottom, :diagonal, :vertical, :horizontal].each do |k|
+ @prs.select { |pr| pr.name == k }.each { |pr| pr.to_xml(xml) }
+ end
+ }
+ end
+ end
+end
diff --git a/lib/axlsx/stylesheet/border_pr.rb b/lib/axlsx/stylesheet/border_pr.rb
new file mode 100644
index 00000000..63a1f9de
--- /dev/null
+++ b/lib/axlsx/stylesheet/border_pr.rb
@@ -0,0 +1,65 @@
+module Axlsx
+ # A border part.
+ class BorderPr
+
+ # @return [Color] The color of this border part.
+ attr_accessor :color
+
+ # @return [Symbol] The syle of this border part.
+ # @note
+ # The following are allowed
+ # :none
+ # :thin
+ # :medium
+ # :dashed
+ # :dotted
+ # :thick
+ # :double
+ # :hair
+ # :mediumDashed
+ # :dashDot
+ # :mediumDashDot
+ # :dashDotDot
+ # :mediumDashDotDot
+ # :slantDashDot
+ attr_accessor :style
+
+ # @return [Symbol] The name of this border part
+ # @note
+ # The following are allowed
+ # :start
+ # :end
+ # :left
+ # :right
+ # :top
+ # :bottom
+ # :diagonal
+ # :vertical
+ # :horizontal
+ attr_accessor :name
+
+ # Creates a new Border Part Object
+ # @option options [Color] color
+ # @option options [Symbol] name
+ # @option options [Symbol] style
+ # @see Axlsx::Border
+ def initialize(options={})
+ options.each do |o|
+ self.send("#{o[0]}=", o[1]) if self.respond_to? "#{o[0]}="
+ end
+ end
+
+ def name=(v) RestrictionValidator.validate "BorderPr.name", [:start, :end, :left, :right, :top, :bottom, :diagonal, :vertical, :horizontal], v; @name = v end
+ def color=(v) DataTypeValidator.validate(:color, Color, v); @color = v end
+ def style=(v) RestrictionValidator.validate "BorderPr.style", [:none, :thin, :medium, :dashed, :dotted, :thick, :double, :hair, :mediumDashed, :dashDot, :mediumDashDot, :dashDotDot, :mediumDashDotDot, :slantDashDot], v; @style = v end
+
+ # Serializes the border part
+ # @param [Nokogiri::XML::Builder] xml The document builder instance this objects xml will be added to.
+ # @return [String]
+ def to_xml(xml)
+ xml.send(@name, :style=>@style) {
+ @color.to_xml(xml) if @color.is_a? Color
+ }
+ end
+ end
+end
diff --git a/lib/axlsx/stylesheet/cell_alignment.rb b/lib/axlsx/stylesheet/cell_alignment.rb
new file mode 100644
index 00000000..071ce68f
--- /dev/null
+++ b/lib/axlsx/stylesheet/cell_alignment.rb
@@ -0,0 +1,96 @@
+module Axlsx
+ # CellAlignment stores information about the cell alignment of a style Xf Object.
+ # @note Using Styles#add_style is the recommended way to manage cell alignment.
+ # @see Styles#add_style
+ class CellAlignment
+ # The horizontal alignment of the cell.
+ # @note
+ # The horizontal cell alignement style must be one of
+ # :general
+ # :left
+ # :center
+ # :right
+ # :fill
+ # :justify
+ # :centerContinuous
+ # :distributed
+ # @return [Symbol]
+ attr_accessor :horizontal
+
+ # The vertical alignment of the cell.
+ # @note
+ # The vertical cell allingment style must be one of the following:
+ # :top
+ # :center
+ # :bottom
+ # :justify
+ # :distributed
+ # @return [Symbol]
+ attr_accessor :vertical
+
+ # The textRotation of the cell.
+ # @return [Integer]
+ attr_accessor :textRotation
+
+ # Indicate if the text of the cell should wrap
+ # @return [Boolean]
+ attr_accessor :wrapText
+
+ # The amount of indent
+ # @return [Integer]
+ attr_accessor :indent
+
+ # The amount of relativeIndent
+ # @return [Integer]
+ attr_accessor :relativeIndent
+
+ # Indicate if the last line should be justified.
+ # @return [Boolean]
+ attr_accessor :justifyLastLine
+
+ # Indicate if the text should be shrunk to the fit in the cell.
+ # @return [Boolean]
+ attr_accessor :shrinkToFit
+
+ # The reading order of the text
+ # 0 Context Dependent
+ # 1 Left-to-Right
+ # 2 Right-to-Left
+ # @return [Integer]
+ attr_accessor :readingOrder
+
+ # Create a new cell_alignment object
+ # @option options [Symbol] horizontal
+ # @option options [Symbol] vertical
+ # @option options [Integer] textRotation
+ # @option options [Boolean] wrapText
+ # @option options [Integer] indent
+ # @option options [Integer] relativeIndent
+ # @option options [Boolean] justifyLastLine
+ # @option options [Boolean] shrinkToFit
+ # @option options [Integer] readingOrder
+ def initialize(options={})
+ options.each do |o|
+ self.send("#{o[0]}=", o[1]) if self.respond_to? "#{o[0]}="
+ end
+ end
+
+ def horizontal=(v) Axlsx::validate_horizontal_alignment v; @horizontal = v end
+ def vertical=(v) Axlsx::validate_vertical_alignment v; @vertical = v end
+ def textRotation=(v) Axlsx::validate_unsigned_int v; @textRotation = v end
+ def wrapText=(v) Axlsx::validate_boolean v; @wrapText = v end
+ def indent=(v) Axlsx::validate_unsigned_int v; @indent = v end
+ def relativeIndent=(v) Axlsx::validate_int v; @relativeIndent = v end
+ def justifyLastLine=(v) Axlsx::validate_boolean v; @justifyLastLine = v end
+ def shrinkToFit=(v) Axlsx::validate_boolean v; @shrinkToFit = v end
+ def readingOrder=(v) Axlsx::validate_unsigned_int v; @readingOrder = v end
+
+ # Serializes the cell alignment
+ # @param [Nokogiri::XML::Builder] xml The document builder instance this objects xml will be added to.
+ # @return [String]
+ def to_xml(xml)
+ xml.alignment(self.instance_values)
+ end
+
+ end
+end
diff --git a/lib/axlsx/stylesheet/cell_protection.rb b/lib/axlsx/stylesheet/cell_protection.rb
new file mode 100644
index 00000000..7f0f4db8
--- /dev/null
+++ b/lib/axlsx/stylesheet/cell_protection.rb
@@ -0,0 +1,33 @@
+module Axlsx
+ # CellProtection stores information about locking or hiding cells in spreadsheet.
+ # @note Using Styles#add_style is the recommended way to manage cell protection.
+ # @see Styles#add_style
+ class CellProtection
+
+ # specifies locking for cells that have the style containing this protection
+ # @return [Boolean]
+ attr_accessor :hidden
+
+ # specifies if the cells that have the style containing this protection
+ # @return [Boolean]
+ attr_accessor :locked
+
+ # Creates a new CellProtection
+ # @option options [Boolean] hidden value for hidden protection
+ # @option options [Boolean] locked value for locked protection
+ def initialize(options={})
+ options.each do |o|
+ self.send("#{o[0]}=", o[1]) if self.respond_to? "#{o[0]}="
+ end
+ end
+ def hidden=(v) Axlsx::validate_boolean v; @hidden = v end
+ def locked=(v) Axlsx::validate_boolean v; @locked = v end
+
+ # Serializes the cell protection
+ # @param [Nokogiri::XML::Builder] xml The document builder instance this objects xml will be added to.
+ # @return [String]
+ def to_xml(xml)
+ xml.protection(self.instance_values)
+ end
+ end
+end
diff --git a/lib/axlsx/stylesheet/cell_style.rb b/lib/axlsx/stylesheet/cell_style.rb
new file mode 100644
index 00000000..11272075
--- /dev/null
+++ b/lib/axlsx/stylesheet/cell_style.rb
@@ -0,0 +1,60 @@
+module Axlsx
+ # CellStyle defines named styles that reference defined formatting records and can be used in your worksheet.
+ # @note Using Styles#add_style is the recommended way to manage cell styling.
+ # @see Styles#add_style
+ class CellStyle
+ # The name of this cell style
+ # @return [String]
+ attr_accessor :name
+
+ # The formatting record id this named style utilizes
+ # @return [Integer]
+ # @see Axlsx::Xf
+ attr_accessor :xfId
+
+ # The buildinId to use when this named style is applied
+ # @return [Integer]
+ # @see Axlsx::NumFmt
+ attr_accessor :builtinId
+
+ # Determines if this formatting is for an outline style, and what level of the outline it is to be applied to.
+ # @return [Integer]
+ attr_accessor :iLevel
+
+ # Determines if this named style should show in the list of styles when using excel
+ # @return [Boolean]
+ attr_accessor :hidden
+
+ # Indicates that the build in style reference has been customized.
+ # @return [Boolean]
+ attr_accessor :customBuiltin
+
+ # Creats a new CellStyle object
+ # @option options [String] name
+ # @option options [Integer] xfId
+ # @option options [Integer] buildinId
+ # @option options [Integer] iLevel
+ # @option options [Boolean] hidden
+ # @option options [Boolean] customBuiltIn
+ def initialize(options={})
+ options.each do |o|
+ self.send("#{o[0]}=", o[1]) if self.respond_to? o[0]
+ end
+ end
+
+ def name=(v) Axlsx::validate_string v; @name = v end
+ def xfId=(v) Axlsx::validate_unsigned_int v; @xfId = v end
+ def builtinId=(v) Axlsx::validate_unsigned_int v; @builtinId = v end
+ def iLevel=(v) Axlsx::validate_unsigned_int v; @iLevel = v end
+ def hidden=(v) Axlsx::validate_boolean v; @hidden = v end
+ def customBuiltin=(v) Axlsx::validate_boolean v; @customBuiltin = v end
+
+ # Serializes the cell style
+ # @param [Nokogiri::XML::Builder] xml The document builder instance this objects xml will be added to.
+ # @return [String]
+ def to_xml(xml)
+ xml.cellStyle(self.instance_values)
+ end
+ end
+
+end
diff --git a/lib/axlsx/stylesheet/color.rb b/lib/axlsx/stylesheet/color.rb
new file mode 100644
index 00000000..34567cd1
--- /dev/null
+++ b/lib/axlsx/stylesheet/color.rb
@@ -0,0 +1,57 @@
+module Axlsx
+ # The color class represents a color used for borders, fills an fonts
+ class Color
+ # Determines if the color is system color dependant
+ # @return [Boolean]
+ attr_accessor :auto
+
+ # Backwards compatability color index
+ # return [Integer]
+ #attr_accessor :indexed
+
+ # The color as defined in rgb terms.
+ # @note
+ # rgb colors need to conform to ST_UnsignedIntHex. That basically means put 'FF' before you color
+ # @example rgb colors
+ # "FF000000" is black
+ # "FFFFFFFF" is white
+ # @return [String]
+ attr_accessor :rgb
+
+ # no support for theme just yet
+ # @return [Integer]
+ #attr_accessor :theme
+
+ # The tint value.
+ # @note valid values are between -1.0 and 1.0
+ # @return [Float]
+ attr_accessor :tint
+
+ # Creates a new Color object
+ # @option options [Boolean] auto
+ # @option options [String] rgb
+ # @option options [Float] tint
+ def initialize(options={})
+ @rgb = "FF000000"
+ options.each do |o|
+ self.send("#{o[0]}=", o[1]) if self.respond_to? o[0]
+ end
+ end
+
+ def auto=(v) Axlsx::validate_boolean v; @auto = v end
+ def rgb=(v) Axlsx::validate_string v; @rgb = v end
+ def tint=(v) Axlsx::validate_float v; @tint = v end
+
+ # This version does not support themes
+ # def theme=(v) Axlsx::validate_unsigned_integer v; @theme = v end
+
+ # Indexed colors are for backward compatability which I am choosing not to support
+ # def indexed=(v) Axlsx::validate_unsigned_integer v; @indexed = v end
+
+
+ # Serializes the color
+ # @param [Nokogiri::XML::Builder] xml The document builder instance this objects xml will be added to.
+ # @return [String]
+ def to_xml(xml) xml.color(self.instance_values) end
+ end
+end
diff --git a/lib/axlsx/stylesheet/fill.rb b/lib/axlsx/stylesheet/fill.rb
new file mode 100644
index 00000000..b7477a88
--- /dev/null
+++ b/lib/axlsx/stylesheet/fill.rb
@@ -0,0 +1,31 @@
+module Axlsx
+ # The Fill is a formatting object that manages the background color, and pattern for cells.
+ # @note The recommended way to manage styles in your workbook is to use Styles#add_style.
+ # @see Styles#add_style
+ # @see PatternFill
+ # @see GradientFill
+ class Fill
+
+ # The type of fill
+ # @return [PatternFill, GradientFill]
+ attr_accessor :fill_type
+
+ # Creates a new Fill object
+ # @param [PatternFill, GradientFill] fill_type
+ # @raise [ArgumentError] if the fill_type parameter is not a PatternFill or a GradientFill instance
+ def initialize(fill_type)
+ self.fill_type = fill_type
+ end
+
+ # Serializes the fill
+ # @param [Nokogiri::XML::Builder] xml The document builder instance this objects xml will be added to.
+ # @return [String]
+ def to_xml(xml)
+ xml.fill { @fill_type.to_xml(xml) }
+ end
+
+ def fill_type=(v) DataTypeValidator.validate "Fill.fill_type", [PatternFill, GradientFill], v; @fill_type = v; end
+
+
+ end
+end
diff --git a/lib/axlsx/stylesheet/font.rb b/lib/axlsx/stylesheet/font.rb
new file mode 100644
index 00000000..fbd369a3
--- /dev/null
+++ b/lib/axlsx/stylesheet/font.rb
@@ -0,0 +1,128 @@
+module Axlsx
+ # The Font class details a font instance for use in styling cells.
+ # @note The recommended way to manage fonts, and other styles is Styles#add_style
+ # @see Styles#add_style
+ class Font
+ # The name of the font
+ # @return [String]
+ attr_accessor :name
+
+ # The charset of the font
+ # @return [Integer]
+ # @note
+ # The following values are defined in the OOXML specification and are OS dependant values
+ # 0 ANSI_CHARSET
+ # 1 DEFAULT_CHARSET
+ # 2 SYMBOL_CHARSET
+ # 77 MAC_CHARSET
+ # 128 SHIFTJIS_CHARSET
+ # 129 HANGUL_CHARSET
+ # 130 JOHAB_CHARSET
+ # 134 GB2312_CHARSET
+ # 136 CHINESEBIG5_CHARSET
+ # 161 GREEK_CHARSET
+ # 162 TURKISH_CHARSET
+ # 163 VIETNAMESE_CHARSET
+ # 177 HEBREW_CHARSET
+ # 178 ARABIC_CHARSET
+ # 186 BALTIC_CHARSET
+ # 204 RUSSIAN_CHARSET
+ # 222 THAI_CHARSET
+ # 238 EASTEUROPE_CHARSET
+ # 255 OEM_CHARSET
+ attr_accessor :charset
+
+ # The font's family
+ # @note
+ # The following are defined OOXML specification
+ # 0 Not applicable.
+ # 1 Roman
+ # 2 Swiss
+ # 3 Modern
+ # 4 Script
+ # 5 Decorative
+ # 6..14 Reserved for future use
+ # @return [Integer]
+ attr_accessor :family
+
+ # Indicates if the font should be rendered in *bold*
+ # @return [Boolean]
+ attr_accessor :b
+
+ # Indicates if the font should be rendered italicized
+ # @return [Boolean]
+ attr_accessor :i
+
+ # Indicates if the font should be rendered with a strikthrough
+ # @return [Boolean]
+ attr_accessor :strike
+
+ # Indicates if the font should be rendered with an outline
+ # @return [Boolean]
+ attr_accessor :outline
+
+ # Indicates if the font should be rendered with a shadow
+ # @return [Boolean]
+ attr_accessor :shadow
+
+ # Indicates if the font should be condensed
+ # @return [Boolean]
+ attr_accessor :condense
+
+ # The font's extend property
+ # @return [Boolean]
+ attr_accessor :extend
+
+ # The color of the font
+ # @return [Color]
+ attr_accessor :color
+
+ # The size of the font.
+ # @return [Integer]
+ attr_accessor :sz
+
+ # Creates a new Font
+ # @option options [String] name
+ # @option options [Integer] charset
+ # @option options [Integer] family
+ # @option options [Integer] family
+ # @option options [Boolean] b
+ # @option options [Boolean] i
+ # @option options [Boolean] strike
+ # @option options [Boolean] outline
+ # @option options [Boolean] shadow
+ # @option options [Boolean] condense
+ # @option options [Boolean] extend
+ # @option options [Color] color
+ # @option options [Integer] sz
+ def initialize(options={})
+ options.each do |o|
+ self.send("#{o[0]}=", o[1]) if self.respond_to? o[0]
+ end
+ end
+
+ def name=(v) Axlsx::validate_string v; @name = v end
+ def charset=(v) Axlsx::validate_unsigned_int v; @charset = v end
+ def family=(v) Axlsx::validate_unsigned_int v; @family = v end
+ def b=(v) Axlsx::validate_boolean v; @b = v end
+ def i=(v) Axlsx::validate_boolean v; @i = v end
+ def strike=(v) Axlsx::validate_boolean v; @strike = v end
+ def outline=(v) Axlsx::validate_boolean v; @outline = v end
+ def shadow=(v) Axlsx::validate_boolean v; @shadow = v end
+ def condense=(v) Axlsx::validate_boolean v; @condense = v end
+ def extend=(v) Axlsx::validate_boolean v; @extend = v end
+ def color=(v) DataTypeValidator.validate "Font.color", Color, v; @color=v end
+ def sz=(v) Axlsx::validate_unsigned_int v; @sz=v end
+
+ # Serializes the fill
+ # @param [Nokogiri::XML::Builder] xml The document builder instance this objects xml will be added to.
+ # @return [String]
+ def to_xml(xml)
+ xml.font {
+ self.instance_values.each do |k, v|
+ v.is_a?(Color) ? v.to_xml(xml) : xml.send(k, {:val => v})
+ end
+ }
+ end
+ end
+end
diff --git a/lib/axlsx/stylesheet/gradient_fill.rb b/lib/axlsx/stylesheet/gradient_fill.rb
new file mode 100644
index 00000000..8fc41a5d
--- /dev/null
+++ b/lib/axlsx/stylesheet/gradient_fill.rb
@@ -0,0 +1,70 @@
+# -*- coding: utf-8 -*-
+module Axlsx
+ # A GradientFill defines the color and positioning for gradiant cell fill.
+ # @see Open Office XML Part 1 §18.8.24
+ class GradientFill
+
+ # The type of gradient.
+ # @note
+ # valid options are
+ # :linear
+ # :path
+ # @return [Symbol]
+ attr_accessor :type
+
+ # Angle of the linear gradient
+ # @return [Float]
+ attr_accessor :degree
+
+ # Percentage format left
+ # @return [Float]
+ attr_accessor :left
+
+ # Percentage format right
+ # @return [Float]
+ attr_accessor :right
+
+ # Percentage format top
+ # @return [Float]
+ attr_accessor :top
+
+ # Percentage format bottom
+ # @return [Float]
+ attr_accessor :bottom
+
+ # Collection of stop objects
+ # @return [SimpleTypedList]
+ attr_reader :stop
+
+ # Creates a new GradientFill object
+ # @option options [Symbol] type
+ # @option options [Float] degree
+ # @option options [Float] left
+ # @option options [Float] right
+ # @option options [Float] top
+ # @option options [Float] bottom
+ def initialize(options={})
+ options[:type] ||= :linear
+ options.each do |o|
+ self.send("#{o[0]}=", o[1]) if self.respond_to? o[0]
+ end
+ @stop = SimpleTypedList.new GradientStop
+ end
+
+ def type=(v) Axlsx::validate_gradient_type v; @type = v end
+ def degree=(v) Axlsx::validate_float v; @degree = v end
+ def left=(v) DataTypeValidator.validate "GradientFill.left", Float, v, lambda { |v| v >= 0.0 && v <= 1.0}; @left = v end
+ def right=(v) DataTypeValidator.validate "GradientFill.right", Float, v, lambda { |v| v >= 0.0 && v <= 1.0}; @right = v end
+ def top=(v) DataTypeValidator.validate "GradientFill.top", Float, v, lambda { |v| v >= 0.0 && v <= 1.0}; @top = v end
+ def bottom=(v) DataTypeValidator.validate "GradientFill.bottom", Float, v, lambda { |v| v >= 0.0 && v <= 1.0}; @bottom= v end
+
+ # Serializes the gradientFill
+ # @param [Nokogiri::XML::Builder] xml The document builder instance this objects xml will be added to.
+ # @return [String]
+ def to_xml(xml)
+ xml.gradientFill(self.instance_values.reject { |k,v| k.to_sym == :stop }) {
+ @stop.each { |s| s.to_xml(xml) }
+ }
+ end
+ end
+end
diff --git a/lib/axlsx/stylesheet/gradient_stop.rb b/lib/axlsx/stylesheet/gradient_stop.rb
new file mode 100644
index 00000000..22786fcb
--- /dev/null
+++ b/lib/axlsx/stylesheet/gradient_stop.rb
@@ -0,0 +1,31 @@
+# -*- coding: utf-8 -*-
+module Axlsx
+ # The GradientStop object represents a color point in a gradient.
+ # @see Open Office XML Part 1 §18.8.24
+ class GradientStop
+ # The color for this gradient stop
+ # @return [Color]
+ # @see Color
+ attr_accessor :color
+
+ # The position of the color
+ # @return [Float]
+ attr_accessor :position
+
+ # Creates a new GradientStop object
+ # @param [Color] color
+ # @param [Float] position
+ def initialize(color, position)
+ self.color = color
+ self.position = position
+ end
+
+ def color=(v) DataTypeValidator.validate "GradientStop.color", Color, v; @color=v end
+ def position=(v) DataTypeValidator.validate "GradientStop.position", Float, v, lambda { |v| v >= 0 && v <= 1}; @position = v end
+
+ # Serializes the gradientStop
+ # @param [Nokogiri::XML::Builder] xml The document builder instance this objects xml will be added to.
+ # @return [String]
+ def to_xml(xml) xml.stop(:position => self.position) {self.color.to_xml(xml)} end
+ end
+end
diff --git a/lib/axlsx/stylesheet/num_fmt.rb b/lib/axlsx/stylesheet/num_fmt.rb
new file mode 100644
index 00000000..826599b6
--- /dev/null
+++ b/lib/axlsx/stylesheet/num_fmt.rb
@@ -0,0 +1,61 @@
+module Axlsx
+ # A NumFmt object defines an identifier and formatting code for data in cells.
+ # @note The recommended way to manage styles is Styles#add_style
+ class NumFmt
+ # @return [Integer] An unsinged integer referencing a standard or custom number format.
+ # @note
+ # These are the known formats I can dig up. The constant NUM_FMT_PERCENT is 9, and uses the default % formatting. Axlsx also defines a few formats for date and time that are commonly used in asia as NUM_FMT_YYYYMMDD and NUM_FRM_YYYYMMDDHHMMSS.
+ # 1 0
+ # 2 0.00
+ # 3 #,##0
+ # 4 #,##0.00
+ # 5 $#,##0_);($#,##0)
+ # 6 $#,##0_);[Red]($#,##0)
+ # 7 $#,##0.00_);($#,##0.00)
+ # 8 $#,##0.00_);[Red]($#,##0.00)
+ # 9 0%
+ # 10 0.00%
+ # 11 0.00E+00
+ # 12 # ?/?
+ # 13 # ??/??
+ # 14 m/d/yyyy
+ # 15 d-mmm-yy
+ # 16 d-mmm
+ # 17 mmm-yy
+ # 18 h:mm AM/PM
+ # 19 h:mm:ss AM/PM
+ # 20 h:mm
+ # 21 h:mm:ss
+ # 22 m/d/yyyy h:mm
+ # 37 #,##0_);(#,##0)
+ # 38 #,##0_);[Red](#,##0)
+ # 39 #,##0.00_);(#,##0.00)
+ # 40 #,##0.00_);[Red](#,##0.00)
+ # 45 mm:ss
+ # 46 [h]:mm:ss
+ # 47 mm:ss.0
+ # 48 ##0.0E+0
+ # 49 @
+ # @see Axlsx
+ attr_accessor :numFmtId
+
+ # @return [String] The formatting to use for this number format.
+ # @see http://support.microsoft.com/kb/264372
+ attr_accessor :formatCode
+ def initialize(options={})
+ @numFmtId = 0
+ @formatCode = ""
+ options.each do |o|
+ self.send("#{o[0]}=", o[1]) if self.respond_to? o[0]
+ end
+ end
+
+ def numFmtId=(v) Axlsx::validate_unsigned_int v; @numFmtId = v end
+ def formatCode=(v) Axlsx::validate_string v; @formatCode = v end
+
+ # Creates a numFmt element applying the instance values of this object as attributes.
+ # @param [Nokogiri::XML::Builder] xml The document builder instance this objects xml will be added to.
+ def to_xml(xml) xml.numFmt(self.instance_values) end
+
+ end
+end
diff --git a/lib/axlsx/stylesheet/pattern_fill.rb b/lib/axlsx/stylesheet/pattern_fill.rb
new file mode 100644
index 00000000..7d674e27
--- /dev/null
+++ b/lib/axlsx/stylesheet/pattern_fill.rb
@@ -0,0 +1,64 @@
+module Axlsx
+ # A PatternFill is the pattern and solid fill styling for a cell.
+ # @note The recommended way to manage styles is with Styles#add_style
+ # @see Style#add_style
+ class PatternFill
+
+ # The color to use for the the background in solid fills.
+ # @return [Color]
+ attr_accessor :fgColor
+
+ # The color to use for the background of the fill when the type is not solid.
+ # @return [Color]
+ attr_accessor :bgColor
+
+ # The pattern type to use
+ # @note
+ # patternType must be one of
+ # :none
+ # :solid
+ # :mediumGray
+ # :darkGray
+ # :lightGray
+ # :darkHorizontal
+ # :darkVertical
+ # :darkDown
+ # :darkUp
+ # :darkGrid
+ # :darkTrellis
+ # :lightHorizontal
+ # :lightVertical
+ # :lightDown
+ # :lightUp
+ # :lightGrid
+ # :lightTrellis
+ # :gray125
+ # :gray0625
+ # @see Office Open XML Part 1 18.18.55
+ attr_accessor :patternType
+
+ # Creates a new PatternFill Object
+ # @option options [Symbol] patternType
+ # @option options [Color] fgColor
+ # @option options [Color] bgColor
+ def initialize(options={})
+ @patternType = :none
+ options.each do |o|
+ self.send("#{o[0]}=", o[1]) if self.respond_to? "#{o[0]}="
+ end
+ end
+
+ def fgColor=(v) DataTypeValidator.validate "PatternFill.fgColor", Color, v; @fgColor=v end
+ def bgColor=(v) DataTypeValidator.validate "PatternFill.bgColor", Color, v; @bgColor=v end
+ def patternType=(v) Axlsx::validate_pattern_type v; @patternType = v end
+
+ # Serializes the pattern fill
+ # @param [Nokogiri::XML::Builder] xml The document builder instance this objects xml will be added to.
+ # @return [String]
+ def to_xml(xml)
+ xml.patternFill(:patternType => self.patternType) {
+ self.instance_values.reject { |k,v| k.to_sym == :patternType }.each { |k,v| xml.send(k, v.instance_values) }
+ }
+ end
+ end
+end
diff --git a/lib/axlsx/stylesheet/styles.rb b/lib/axlsx/stylesheet/styles.rb
new file mode 100644
index 00000000..6f428ecd
--- /dev/null
+++ b/lib/axlsx/stylesheet/styles.rb
@@ -0,0 +1,296 @@
+# -*- coding: utf-8 -*-
+module Axlsx
+ require 'axlsx/stylesheet/border.rb'
+ require 'axlsx/stylesheet/border_pr.rb'
+ require 'axlsx/stylesheet/cell_alignment.rb'
+ require 'axlsx/stylesheet/cell_style.rb'
+ require 'axlsx/stylesheet/color.rb'
+ require 'axlsx/stylesheet/fill.rb'
+ require 'axlsx/stylesheet/font.rb'
+ require 'axlsx/stylesheet/gradient_fill.rb'
+ require 'axlsx/stylesheet/gradient_stop.rb'
+ require 'axlsx/stylesheet/num_fmt.rb'
+ require 'axlsx/stylesheet/pattern_fill.rb'
+ require 'axlsx/stylesheet/table_style.rb'
+ require 'axlsx/stylesheet/table_styles.rb'
+ require 'axlsx/stylesheet/table_style_element.rb'
+ require 'axlsx/stylesheet/xf.rb'
+ require 'axlsx/stylesheet/cell_protection.rb'
+
+ #The Styles class manages worksheet styles
+ # In addition to creating the require style objects for a valid xlsx package, this class provides the key mechanism for adding styles to your workbook, and safely applying them to the cells of your worksheet.
+ # All portions of the stylesheet are implemented here exception colors, which specify legacy and modified pallete colors, and exLst, whic is used as a future feature data storage area.
+ # @see Office Open XML Part 1 18.8.11 for gory details on how this stuff gets put together
+ # @see Styles#add_style
+ # @note The recommended way to manage styles is with add_style
+ class Styles
+ # numFmts for your styles.
+ # The default styles, which change based on the system local, are as follows.
+ # id formatCode
+ # 0 General
+ # 1 0
+ # 2 0.00
+ # 3 #,##0
+ # 4 #,##0.00
+ # 9 0%
+ # 10 0.00%
+ # 11 0.00E+00
+ # 12 # ?/?
+ # 13 # ??/??
+ # 14 mm-dd-yy
+ # 15 d-mmm-yy
+ # 16 d-mmm
+ # 17 mmm-yy
+ # 18 h:mm AM/PM
+ # 19 h:mm:ss AM/PM
+ # 20 h:mm
+ # 21 h:mm:ss
+ # 22 m/d/yy h:mm
+ # 37 #,##0 ;(#,##0)
+ # 38 #,##0 ;[Red](#,##0)
+ # 39 #,##0.00;(#,##0.00)
+ # 40 #,##0.00;[Red](#,##0.00)
+ # 45 mm:ss
+ # 46 [h]:mm:ss
+ # 47 mmss.0
+ # 48 ##0.0E+0
+ # 49 @
+ # Axlsx also defines the following constants which you can use in add_style.
+ # NUM_FMT_PERCENT formats to "0%"
+ # NUM_FMT_YYYYMMDD formats to "yyyy/mm/dd"
+ # NUM_FMT_YYYYMMDDHHMMSS formats to "yyyy/mm/dd hh:mm:ss"
+ # @see Office Open XML Part 1 - 18.8.31 for more information on creating number formats
+ # @return [SimpleTypedList]
+ # @note The recommended way to manage styles is with add_style
+ # @see Styles#add_style
+ attr_reader :numFmts
+
+ # The collection of fonts used in this workbook
+ # @return [SimpleTypedList]
+ # @note The recommended way to manage styles is with add_style
+ # @see Styles#add_style
+ attr_reader :fonts
+
+ # The collection of fills used in this workbook
+ # @return [SimpleTypedList]
+ # @note The recommended way to manage styles is with add_style
+ # @see Styles#add_style
+ attr_reader :fills
+
+ # The collection of borders used in this workbook
+ # Axlsx predefines THIN_BORDER which can be used to put a border around all of your cells.
+ # @return [SimpleTypedList]
+ # @note The recommended way to manage styles is with add_style
+ # @see Styles#add_style
+ attr_reader :borders
+
+ # The collection of master formatting records for named cell styles, which means records defined in cellStyles, in the workbook
+ # @return [SimpleTypedList]
+ # @note The recommended way to manage styles is with add_style
+ # @see Styles#add_style
+ attr_reader :cellStyleXfs
+
+ # The collection of named styles, referencing cellStyleXfs items in the workbook.
+ # @return [SimpleTypedList]
+ # @note The recommended way to manage styles is with add_style
+ # @see Styles#add_style
+ attr_reader :cellStyles
+
+ # The collection of master formatting records. This is the list that you will actually use in styling a workbook.
+ # @return [SimpleTypedList]
+ # @note The recommended way to manage styles is with add_style
+ # @see Styles#add_style
+ attr_reader :cellXfs
+
+ # The collection of non-cell formatting records used in the worksheet.
+ # @return [SimpleTypedList]
+ # @note The recommended way to manage styles is with add_style
+ # @see Styles#add_style
+ attr_reader :dxfs
+
+ # The collection of table styles that will be available to the user in the excel UI
+ # @return [SimpleTypedList]
+ # @note The recommended way to manage styles is with add_style
+ # @see Styles#add_style
+ attr_reader :tableStyles
+
+ # Creates a new Styles object and prepopulates it with the requires objects to generate a valid package style part.
+ def initialize()
+ load_default_styles
+ end
+
+ # Drastically simplifies style creation and management.
+ # @return [Integer]
+ # @option options [String] fg_color The text color
+ # @option options [Integer] sz The text size
+ # @option options [Boolean] b Indicates if the text should be bold
+ # @option options [Boolean] i Indicates if the text should be italicised
+ # @option options [Boolean] strike Indicates if the text should be rendered with a strikethrough
+ # @option options [Boolean] strike Indicates if the text should be rendered with a shadow
+ # @option options [Integer] charset The character set to use.
+ # @option options [Integer] family The font family to use.
+ # @option options [String] name The name of the font to use
+ # @option options [Integer] num_fmt The number format to apply
+ # @option options [String] format_code The formatting to apply. If this is specified, num_fmt is ignored.
+ # @option options [Integer] border The border style to use.
+ # @option options [String] bg_color The background color to apply to the cell
+ # @option options [Boolean] hidden Indicates if the cell should be hidden
+ # @option options [Boolean] locked Indicates if the cell should be locked
+ # @option options [Hash] alignment A hash defining any of the attributes used in CellAlignment
+ # @see CellAlignment
+ #
+ # @example You Got Style
+ # require "rubygems" # if that is your preferred way to manage gems!
+ # require "axlsx"
+ #
+ # p = Axlsx::Package.new
+ # ws = p.workbook.add_worksheet
+ #
+ # # black text on a white background at 14pt with thin borders!
+ # title = ws.style.add_style(:bg_color => "FFFF0000", :fg_color=>"#FF000000", :sz=>14, :border=>Axlsx::STYLE_THIN_BORDER
+ #
+ # ws.add_row :values => ["Least Popular Pets"]
+ # ws.add_row :values => ["", "Dry Skinned Reptiles", "Bald Cats", "Violent Parrots"], :style=>title
+ # ws.add_row :values => ["Votes", 6, 4, 1], :style=>Axlsx::STYLE_THIN_BORDER
+ # f = File.open('example_you_got_style.xlsx', 'w')
+ # p.serialize(f)
+ #
+ # @example Styling specifically
+ # # an example of applying specific styles to specific cells
+ # require "rubygems" # if that is your preferred way to manage gems!
+ # require "axlsx"
+ #
+ # p = Axlsx::Package.new
+ # ws = p.workbook.add_worksheet
+ #
+ # # define your styles
+ # title = ws.style.add_style(:bg_color => "FFFF0000",
+ # :fg_color=>"#FF000000",
+ # :border=>Axlsx::STYLE_THIN_BORDER,
+ # :alignment=>{:horizontal => :center})
+ #
+ # date_time = ws.style.add_style(:num_fmt => Axlsx::NUM_FMT_YYYYMMDDHHMMSS,
+ # :border=>Axlsx::STYLE_THIN_BORDER)
+ #
+ # percent = ws.style.add_style(:num_fmt => Axlsx::NUM_FMT_PERCENT,
+ # :border=>Axlsx::STYLE_THIN_BORDER)
+ #
+ # currency = ws.style.add_style(:format_code=>"¥#,##0;[Red]¥-#,##0",
+ # :border=>Axlsx::STYLE_THIN_BORDER)
+ #
+ # # build your rows
+ # ws.add_row :values => ["Genreated At:", Time.now], :styles=>[nil, date_time]
+ # ws.add_row :values => ["Previous Year Quarterly Profits (JPY)"], :style=>title
+ # ws.add_row :values => ["Quarter", "Profit", "% of Total"], :style=>title
+ # ws.add_row :values => ["Q1", 4000, 40], :style=>[title, currency, percent]
+ # ws.add_row :values => ["Q2", 3000, 30], :style=>[title, currency, percent]
+ # ws.add_row :values => ["Q3", 1000, 10], :style=>[title, currency, percent]
+ # ws.add_row :values => ["Q4", 2000, 20], :style=>[title, currency, percent]
+ # f = File.open('example_you_got_style.xlsx', 'w')
+ # p.serialize(f)
+ def add_style(options={})
+
+ numFmtId = if options[:format_code]
+ n = @numFmts.map{ |f| f.numFmtId }.max + 1
+ numFmts << NumFmt.new(:numFmtId => n, :formatCode=> options[:format_code])
+ n
+ else
+ options[:num_fmt] || 0
+ end
+
+ borderId = options[:border] || 0
+ raise ArgumentError, "Invalid borderId" unless borderId < borders.size
+
+ fill = if options[:bg_color]
+ color = Color.new(:rgb=>options[:bg_color])
+ pattern = PatternFill.new(:patternType =>:solid, :fgColor=>color)
+ fills << Fill.new(pattern)
+ else
+ 0
+ end
+
+ fontId = if (options.values_at(:fg_color, :sz, :b, :i, :strike, :outline, :shadow, :charset, :family, :font_name).length)
+ font = Font.new()
+ [:b, :i, :strike, :outline, :shadow, :charset, :family, :sz].each { |k| font.send("#{k}=", options[k]) unless options[k].nil? }
+ font.color = Color.new(:rgb => options[:fg_color]) unless options[:fg_color].nil?
+ font.name = options[:font_name] unless options[:font_name].nil?
+ fonts << font
+ else
+ 0
+ end
+
+ applyProtection = (options[:hidden] || options[:locked]) ? 1 : 0
+
+ xf = Xf.new(:fillId => fill, :fontId=>fontId, :applyFill=>1, :applyFont=>1, :numFmtId=>numFmtId, :borderId=>borderId, :applyProtection=>applyProtection)
+
+ if options[:alignment]
+ xf.alignment = CellAlignment.new(options[:alignment])
+ end
+
+ if applyProtection
+ xf.protection = CellProtection.new(options)
+ end
+
+ cellXfs << xf
+ end
+
+ # Serializes the styles document
+ # @return [String]
+ def to_xml()
+ builder = Nokogiri::XML::Builder.new(:encoding => ENCODING) do |xml|
+ xml.styleSheet(:xmlns => XML_NS) {
+ [:numFmts, :fonts, :fills, :borders, :cellStyleXfs, :cellXfs, :dxfs, :tableStyles].each do |key|
+ self.instance_values[key.to_s].to_xml(xml)
+ end
+ }
+ end
+ builder.to_xml
+ end
+
+ private
+ # Creates the default set of styles the exel requires to be valid as well as setting up the
+ # Axlsx::STYLE_THIN_BORDER
+ def load_default_styles
+ @numFmts = SimpleTypedList.new NumFmt
+ @numFmts << NumFmt.new(:numFmtId => NUM_FMT_YYYYMMDD, :formatCode=> "yyyy/mm/dd")
+ @numFmts << NumFmt.new(:numFmtId => NUM_FMT_YYYYMMDDHHMMSS, :formatCode=> "yyyy/mm/dd hh:mm:ss")
+
+ @numFmts.lock
+
+ @fonts = SimpleTypedList.new Font
+ @fonts << Font.new(:name => "Arial", :sz => 11, :family=>1)
+ @fonts.lock
+
+ @fills = SimpleTypedList.new Fill
+ @fills << Fill.new(Axlsx::PatternFill.new(:patternType=>:none))
+ @fills << Fill.new(Axlsx::PatternFill.new(:patternType=>:gray125))
+ @fills.lock
+
+ @borders = SimpleTypedList.new Border
+ @borders << Border.new
+ black_border = Border.new
+ [:left, :right, :top, :bottom].each do |item|
+ black_border.prs << BorderPr.new(:name=>item, :style=>:thin, :color=>Color.new(:rgb=>"FF000000"))
+ end
+ @borders << black_border
+ @borders.lock
+
+ @cellStyleXfs = SimpleTypedList.new Xf, "cellStyleXfs"
+ @cellStyleXfs << Xf.new(:borderId=>0, :xfId=>0, :numFmtId=>0, :fontId=>0, :fillId=>0)
+ @cellStyleXfs.lock
+
+ @cellStyles = SimpleTypedList.new CellStyle
+ @cellStyles << CellStyle.new(:name =>"標準", :builtinId =>0, :xfId=>0)
+ @cellStyles.lock
+
+ @cellXfs = SimpleTypedList.new Xf, "cellXfs"
+ @cellXfs << Xf.new(:borderId=>0, :xfId=>0, :numFmtId=>0, :fontId=>0, :fillId=>0)
+ @cellXfs << Xf.new(:borderId=>1, :xfId=>0, :numFmtId=>0, :fontId=>0, :fillId=>0)
+ @cellXfs.lock
+
+ @dxfs = SimpleTypedList.new(Xf, "dxfs"); @dxfs.lock
+ @tableStyles = TableStyles.new(:defaultTableStyle => "TableStyleMedium9", :defaultPivotStyle => "PivotStyleLight16"); @tableStyles.lock
+ end
+ end
+end
+
diff --git a/lib/axlsx/stylesheet/table_style.rb b/lib/axlsx/stylesheet/table_style.rb
new file mode 100644
index 00000000..ca138b1f
--- /dev/null
+++ b/lib/axlsx/stylesheet/table_style.rb
@@ -0,0 +1,44 @@
+module Axlsx
+ # A single table style definition and is a collection for tableStyleElements
+ # @note Table are not supported in this version and only the defaults required for a valid workbook are created.
+ class TableStyle < SimpleTypedList
+
+ # The name of this table style
+ # @return [string]
+ attr_accessor :name
+
+ # indicates if this style should be applied to pivot tables
+ # @return [Boolean]
+ attr_accessor :pivot
+
+ # indicates if this style should be applied to tables
+ # @return [Boolean]
+ attr_accessor :table
+
+ # creates a new TableStyle object
+ # @raise [ArgumentError] if name option is not provided.
+ # @param [String] name
+ # @option options [Boolean] pivot
+ # @option options [Boolean] table
+ def initialize(name, options={})
+ self.name = name
+ options.each do |o|
+ self.send("#{o[0]}=", o[1]) if self.respond_to? o[0]
+ end
+ super TableStyleElement
+ end
+
+ def name=(v) Axlsx::validate_string v; @name=v end
+ def pivot=(v) Axlsx::validate_boolean v; @pivot=v end
+ def table=(v) Axlsx::validate_boolean v; @table=v end
+
+ # Serializes the table style
+ # @param [Nokogiri::XML::Builder] xml The document builder instance this objects xml will be added to.
+ # @return [String]
+ def to_xml(xml)
+ attr = self.instance_values.select { |k, v| [:name, :pivot, :table].include? k }
+ attr[:count] = self.size
+ xml.tableStyle(attr) { self.each { |table_style_el| table_style_el.to_xml(xml) } }
+ end
+ end
+end
diff --git a/lib/axlsx/stylesheet/table_style_element.rb b/lib/axlsx/stylesheet/table_style_element.rb
new file mode 100644
index 00000000..146a6bad
--- /dev/null
+++ b/lib/axlsx/stylesheet/table_style_element.rb
@@ -0,0 +1,66 @@
+module Axlsx
+ # an element of style that belongs to a table style.
+ # @note tables and table styles are not supported in this version. This class exists in preparation for that support.
+ class TableStyleElement
+ # The type of style element. The following type are allowed
+ # :wholeTable
+ # :headerRow
+ # :totalRow
+ # :firstColumn
+ # :lastColumn
+ # :firstRowStripe
+ # :secondRowStripe
+ # :firstColumnStripe
+ # :secondColumnStripe
+ # :firstHeaderCell
+ # :lastHeaderCell
+ # :firstTotalCell
+ # :lastTotalCell
+ # :firstSubtotalColumn
+ # :secondSubtotalColumn
+ # :thirdSubtotalColumn
+ # :firstSubtotalRow
+ # :secondSubtotalRow
+ # :thirdSubtotalRow
+ # :blankRow
+ # :firstColumnSubheading
+ # :secondColumnSubheading
+ # :thirdColumnSubheading
+ # :firstRowSubheading
+ # :secondRowSubheading
+ # :thirdRowSubheading
+ # :pageFieldLabels
+ # :pageFieldValues
+ # @return [Symbol]
+ attr_accessor :type
+
+ # Number of rows or columns used in striping when the type is firstRowStripe, secondRowStripe, firstColumnStripe, or secondColumnStripe.
+ # @return [Integer]
+ attr_accessor :size
+
+ # The dxfId this style element points to
+ # @return [Integer]
+ attr_accessor :dxfId
+
+ # creates a new TableStyleElement object
+ # @option options [Symbol] type
+ # @option options [Integer] size
+ # @option options [Integer] dxfId
+ def initialize(options={})
+ options.each do |o|
+ self.send("#{o[0]}=", o[1]) if self.respond_to? o[0]
+ end
+ end
+
+ def type=(v) Axlsx::validate_table_element_type v; @type = v end
+ def size=(v) Axlsx::validate_unsigned_int v; @size = v end
+ def dxfId=(v) Axlsx::validate_unsigned_int v; @dxfId = v end
+
+ # Serializes the table style element
+ # @param [Nokogiri::XML::Builder] xml The document builder instance this objects xml will be added to.
+ # @return [String]
+ def to_xml(xml)
+ xml.tableStyleElement self.instance_values
+ end
+ end
+end
diff --git a/lib/axlsx/stylesheet/table_styles.rb b/lib/axlsx/stylesheet/table_styles.rb
new file mode 100644
index 00000000..cc2005f6
--- /dev/null
+++ b/lib/axlsx/stylesheet/table_styles.rb
@@ -0,0 +1,39 @@
+module Axlsx
+ # TableStyles represents a collection of style definitions for table styles and pivot table styles.
+ # @note Support for custom table styles does not exist in this version. Many of the classes required are defined in preparation for future release. Please do not attempt to add custom table styles.
+ class TableStyles < SimpleTypedList
+
+ # The default table style. The default value is 'TableStyleMedium9'
+ # @return [String]
+ #
+ attr_accessor :defaultTableStyle
+
+ # The default pivot table style. The default value is 'PivotStyleLight6'
+ # @return [String]
+ attr_accessor :defaultPivotStyle
+
+ # Creates a new TableStyles object that is a container for TableStyle objects
+ # @option options [String] defaultTableStyle
+ # @option options [String] defaultPivotStyle
+ def initialize(options={})
+ @defaultTableStyle = options[:defaultTableStyle] || "TableStyleMedium9"
+ @defaultPivotStyle = options[:defaultPivotStyle] || "PivotStyleLight16"
+ super TableStyle
+ end
+
+ def defaultTableStyle=(v) Axlsx::validate_string(v); @defaultTableStyle = v; end
+ def defaultPivotStyle=(v) Axlsx::validate_string(v); @defaultPivotStyle = v; end
+
+ # Serializes the table styles element
+ # @param [Nokogiri::XML::Builder] xml The document builder instance this objects xml will be added to.
+ # @return [String]
+ def to_xml(xml)
+ attr = self.instance_values.reject {|k, v| ![:defaultTableStyle, :defaultPivotStyle].include?(k.to_sym) }
+ attr[:count] = self.size
+ xml.tableStyles(attr) {
+ self.each { |table_style| table_style.to_xml(xml) }
+ }
+ end
+ end
+
+end
diff --git a/lib/axlsx/stylesheet/xf.rb b/lib/axlsx/stylesheet/xf.rb
new file mode 100644
index 00000000..905eda2c
--- /dev/null
+++ b/lib/axlsx/stylesheet/xf.rb
@@ -0,0 +1,117 @@
+module Axlsx
+ # The Xf class defines a formatting record for use in Styles
+ class Xf
+ #does not support extList (ExtensionList)
+
+ # The cell alignment for this style
+ # @return [CellAlignment]
+ # @see CellAlignment
+ attr_accessor :alignment
+
+ # The cell protection for this style
+ # @return [CellProtection]
+ # @see CellProtection
+ attr_accessor :protection
+
+ # id of the numFmt to apply to this style
+ # @return [Integer]
+ attr_accessor :numFmtId
+
+ # index (0 based) of the font to be used in this style
+ # @return [Integer]
+ attr_accessor :fontId
+
+ # index (0 based) of the fill to be used in this style
+ # @return [Integer]
+ attr_accessor :fillId
+
+ # index (0 based) of the border to be used in this style
+ # @return [Integer]
+ attr_accessor :borderId
+
+ # index (0 based) of cellStylesXfs item to be used in this style. Only applies to cellXfs items
+ # @return [Integer]
+ attr_accessor :xfId
+
+ # indecates if text should be prefixed by a single quote in the cell
+ # @return [Boolean]
+ attr_accessor :quotePrefix
+
+ # indicates if the cell has a pivot table drop down button
+ # @return [Boolean]
+ attr_accessor :pivotButton
+
+ # indicates if the numFmtId should be applied
+ # @return [Boolean]
+ attr_accessor :applyNumberFormat
+
+ # indicates if the fontId should be applied
+ # @return [Boolean]
+ attr_accessor :applyFont
+
+ # indicates if the fillId should be applied
+ # @return [Boolean]
+ attr_accessor :applyFill
+
+ # indicates if the borderId should be applied
+ # @return [Boolean]
+ attr_accessor :applyBorder
+
+ # Indicates if the alignment options should be applied
+ # @return [Boolean]
+ attr_accessor :applyAlignment
+
+ # Indicates if the protection options should be applied
+ # @return [Boolean]
+ attr_accessor :applyProtection
+
+ # Creates a new Xf object
+ # @option [Integer] numFmtId
+ # @option [Integer] fontId
+ # @option [Integer] fillId
+ # @option [Integer] borderId
+ # @option [Integer] xfId
+ # @option [Boolean] quotePrefix
+ # @option [Boolean] pivotButton
+ # @option [Boolean] applyNumberFormat
+ # @option [Boolean] applyFont
+ # @option [Boolean] applyFill
+ # @option [Boolean] applyBorder
+ # @option [Boolean] applyAlignment
+ # @option [Boolean] applyProtection
+ # @option [CellAlignment] alignment
+ # @option [CellProtection] protection
+ def initialize(options={})
+ options.each do |o|
+ self.send("#{o[0]}=", o[1]) if self.respond_to? "#{o[0]}="
+ end
+ end
+
+ def alignment=(v) DataTypeValidator.validate "Xf.alignment", CellAlignment, v; @alignment = v end
+ def protection=(v) DataTypeValidator.validate "Xf.protection", CellProtection, v; @protection = v end
+
+ def numFmtId=(v) Axlsx::validate_unsigned_int v; @numFmtId = v end
+ def fontId=(v) Axlsx::validate_unsigned_int v; @fontId = v end
+ def fillId=(v) Axlsx::validate_unsigned_int v; @fillId = v end
+ def borderId=(v) Axlsx::validate_unsigned_int v; @borderId = v end
+ def xfId=(v) Axlsx::validate_unsigned_int v; @xfId = v end
+ def quotePrefix=(v) Axlsx::validate_boolean v; @quotePrefix = v end
+ def pivotButton=(v) Axlsx::validate_boolean v; @pivotButton = v end
+ def applyNumberFormat=(v) Axlsx::validate_boolean v; @applyNumberFormat = v end
+ def applyFont=(v) Axlsx::validate_boolean v; @applyFont = v end
+ def applyFill=(v) Axlsx::validate_boolean v; @applyFill = v end
+ def applyBorder=(v) Axlsx::validate_boolean v; @applyBorder = v end
+ def applyAlignment=(v) Axlsx::validate_boolean v; @applyAlignment = v end
+ def applyProtection=(v) Axlsx::validate_boolean v; @applyProtection = v end
+
+ # Serializes the xf elemen
+ # @param [Nokogiri::XML::Builder] xml The document builder instance this objects xml will be added to.
+ # @return [String]
+ def to_xml(xml)
+ xml.xf(self.instance_values.reject { |k, v| [:alignment, :protection, :extList, :name].include? k.to_sym}) {
+ alignment.to_xml(xml) if self.alignment
+ protection.to_xml(xml) if self.protection
+ }
+ end
+ end
+end