From 4d16bfc43780e5d3f7368625700b583e3e98217a Mon Sep 17 00:00:00 2001 From: Randy Morgan Date: Sun, 27 Nov 2011 18:11:42 +0900 Subject: adding in row_style and col_style methods to worksheet and active record 'acts_as_axlsx' to provide to_xlsx. --- doc/Axlsx/Styles.html | 639 ++++++++++++++++++++++++++++---------------------- 1 file changed, 354 insertions(+), 285 deletions(-) (limited to 'doc/Axlsx/Styles.html') diff --git a/doc/Axlsx/Styles.html b/doc/Axlsx/Styles.html index f0eff10d..daac86c5 100644 --- a/doc/Axlsx/Styles.html +++ b/doc/Axlsx/Styles.html @@ -97,18 +97,20 @@
Note: -
-

The recommended way to manage styles is with add_style

+

+The recommended way to manage styles is with add_style +

- -

The Styles class manages worksheet styles In addition to creating the +

+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.

+and exLst, whic is used as a future feature data storage area. +

@@ -149,9 +151,10 @@ and exLst, whic is used as a future feature data storage area.

-
-

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.

+

+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. +

@@ -176,9 +179,10 @@ THIN_BORDER which can be used to put a border around all of your cells.

-
-

The collection of named styles, referencing cellStyleXfs items in the -workbook.

+

+The collection of named styles, referencing cellStyleXfs items in the +workbook. +

@@ -203,9 +207,10 @@ workbook.

-
-

The collection of master formatting records for named cell styles, which -means records defined in cellStyles, in the workbook.

+

+The collection of master formatting records for named cell styles, which +means records defined in cellStyles, in the workbook. +

@@ -230,8 +235,9 @@ means records defined in cellStyles, in the workbook.

-
-

The collection of master formatting records.

+

+The collection of master formatting records. +

@@ -256,8 +262,9 @@ means records defined in cellStyles, in the workbook.

-
-

The collection of non-cell formatting records used in the worksheet.

+

+The collection of non-cell formatting records used in the worksheet. +

@@ -282,8 +289,9 @@ means records defined in cellStyles, in the workbook.

-
-

The collection of fills used in this workbook.

+

+The collection of fills used in this workbook. +

@@ -308,8 +316,9 @@ means records defined in cellStyles, in the workbook.

-
-

The collection of fonts used in this workbook.

+

+The collection of fonts used in this workbook. +

@@ -334,8 +343,9 @@ means records defined in cellStyles, in the workbook.

-
-

numFmts for your styles.

+

+numFmts for your styles. +

@@ -360,9 +370,10 @@ means records defined in cellStyles, in the workbook.

-
-

The collection of table styles that will be available to the user in the -excel UI.

+

+The collection of table styles that will be available to the user in the +excel UI. +

@@ -397,8 +408,9 @@ excel UI.

-
-

Drastically simplifies style creation and management.

+

+Drastically simplifies style creation and management. +

@@ -422,9 +434,10 @@ excel UI.

-
-

Creates a new Styles object and prepopulates it with the requires objects -to generate a valid package style part.

+

+Creates a new Styles object and prepopulates it with the requires objects +to generate a valid package style part. +

@@ -446,8 +459,9 @@ to generate a valid package style part.

-
-

Serializes the styles document.

+

+Serializes the styles document. +

@@ -468,9 +482,10 @@ to generate a valid package style part.

- -

Creates a new Styles object and prepopulates it with the requires objects -to generate a valid package style part.

+

+Creates a new Styles object and prepopulates it with the requires objects +to generate a valid package style part. +

@@ -491,9 +506,10 @@ to generate a valid package style part.

# File 'lib/axlsx/stylesheet/styles.rb', line 118
 
-def initialize()
-  load_default_styles
-end
+def initialize() + load_default_styles +end + @@ -519,14 +535,16 @@ to generate a valid package style part.

Note: -
-

The recommended way to manage styles is with add_style

+

+The recommended way to manage styles is with add_style +

- -

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.

+

+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. +

@@ -567,9 +585,10 @@ THIN_BORDER which can be used to put a border around all of your cells.

# File 'lib/axlsx/stylesheet/styles.rb', line 85
 
-def borders
-  @borders
-end
+def borders + @borders +end + @@ -590,14 +609,16 @@ THIN_BORDER which can be used to put a border around all of your cells.

Note: -
-

The recommended way to manage styles is with add_style

+

+The recommended way to manage styles is with add_style +

- -

The collection of named styles, referencing cellStyleXfs items in the -workbook.

+

+The collection of named styles, referencing cellStyleXfs items in the +workbook. +

@@ -638,9 +659,10 @@ workbook.

# File 'lib/axlsx/stylesheet/styles.rb', line 97
 
-def cellStyles
-  @cellStyles
-end
+def cellStyles + @cellStyles +end + @@ -661,14 +683,16 @@ workbook.

Note: -
-

The recommended way to manage styles is with add_style

+

+The recommended way to manage styles is with add_style +

- -

The collection of master formatting records for named cell styles, which -means records defined in cellStyles, in the workbook

+

+The collection of master formatting records for named cell styles, which +means records defined in cellStyles, in the workbook +

@@ -709,9 +733,10 @@ means records defined in cellStyles, in the workbook

# File 'lib/axlsx/stylesheet/styles.rb', line 91
 
-def cellStyleXfs
-  @cellStyleXfs
-end
+def cellStyleXfs + @cellStyleXfs +end + @@ -732,14 +757,16 @@ means records defined in cellStyles, in the workbook

Note: -
-

The recommended way to manage styles is with add_style

+

+The recommended way to manage styles is with add_style +

- -

The collection of master formatting records. This is the list that you will -actually use in styling a workbook.

+

+The collection of master formatting records. This is the list that you will +actually use in styling a workbook. +

@@ -780,9 +807,10 @@ actually use in styling a workbook.

# File 'lib/axlsx/stylesheet/styles.rb', line 103
 
-def cellXfs
-  @cellXfs
-end
+def cellXfs + @cellXfs +end + @@ -803,13 +831,15 @@ actually use in styling a workbook.

Note: -
-

The recommended way to manage styles is with add_style

+

+The recommended way to manage styles is with add_style +

- -

The collection of non-cell formatting records used in the worksheet.

+

+The collection of non-cell formatting records used in the worksheet. +

@@ -850,9 +880,10 @@ actually use in styling a workbook.

# File 'lib/axlsx/stylesheet/styles.rb', line 109
 
-def dxfs
-  @dxfs
-end
+def dxfs + @dxfs +end + @@ -873,13 +904,15 @@ actually use in styling a workbook.

Note: -
-

The recommended way to manage styles is with add_style

+

+The recommended way to manage styles is with add_style +

- -

The collection of fills used in this workbook

+

+The collection of fills used in this workbook +

@@ -920,9 +953,10 @@ actually use in styling a workbook.

# File 'lib/axlsx/stylesheet/styles.rb', line 78
 
-def fills
-  @fills
-end
+def fills + @fills +end + @@ -943,13 +977,15 @@ actually use in styling a workbook.

Note: -
-

The recommended way to manage styles is with add_style

+

+The recommended way to manage styles is with add_style +

- -

The collection of fonts used in this workbook

+

+The collection of fonts used in this workbook +

@@ -990,9 +1026,10 @@ actually use in styling a workbook.

# File 'lib/axlsx/stylesheet/styles.rb', line 72
 
-def fonts
-  @fonts
-end
+def fonts + @fonts +end + @@ -1013,48 +1050,51 @@ actually use in styling a workbook.

Note: -
-

The recommended way to manage styles is with add_style

+

+The recommended way to manage styles is with add_style +

- -

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"
+

+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"
+
@@ -1097,9 +1137,10 @@ Axlsx also defines the following constants which you can use in add_style.
# File 'lib/axlsx/stylesheet/styles.rb', line 66
 
-def numFmts
-  @numFmts
-end
+def numFmts + @numFmts +end + @@ -1120,14 +1161,16 @@ Axlsx also defines the following constants which you can use in add_style.
Note: -
-

The recommended way to manage styles is with add_style

+

+The recommended way to manage styles is with add_style +

- -

The collection of table styles that will be available to the user in the -excel UI

+

+The collection of table styles that will be available to the user in the +excel UI +

@@ -1168,9 +1211,10 @@ excel UI

# File 'lib/axlsx/stylesheet/styles.rb', line 115
 
-def tableStyles
-  @tableStyles
-end
+def tableStyles + @tableStyles +end + @@ -1192,8 +1236,9 @@ excel UI

- -

Drastically simplifies style creation and management.

+

+Drastically simplifies style creation and management. +

@@ -1203,59 +1248,63 @@ excel UI

Examples:

-

-

You Got Style

+

+You Got Style +

-
require "rubygems" # if that is your preferred way to manage gems!
-require "axlsx"
+      
require "rubygems" # if that is your preferred way to manage gems!
+require "axlsx"
 
-p = Axlsx::Package.new
-ws = p.workbook.add_worksheet
+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
+# 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)
+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) +
-

-

Styling specifically

+

+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)
+
# 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)
+

Parameters:

@@ -1273,8 +1322,9 @@ p.serialize(f) — -
-

a customizable set of options

+

+a customizable set of options +

@@ -1293,8 +1343,9 @@ p.serialize(f) - —
-

The text color

+ —

+The text color +

@@ -1304,8 +1355,9 @@ p.serialize(f) - —
-

The text size

+ —

+The text size +

@@ -1315,8 +1367,9 @@ p.serialize(f) - —
-

Indicates if the text should be bold

+ —

+Indicates if the text should be bold +

@@ -1326,8 +1379,9 @@ p.serialize(f) - —
-

Indicates if the text should be italicised

+ —

+Indicates if the text should be italicised +

@@ -1337,8 +1391,9 @@ p.serialize(f) - —
-

Indicates if the text should be rendered with a strikethrough

+ —

+Indicates if the text should be rendered with a strikethrough +

@@ -1348,8 +1403,9 @@ p.serialize(f) - —
-

Indicates if the text should be rendered with a shadow

+ —

+Indicates if the text should be rendered with a shadow +

@@ -1359,8 +1415,9 @@ p.serialize(f) - —
-

The character set to use.

+ —

+The character set to use. +

@@ -1370,8 +1427,9 @@ p.serialize(f) - —
-

The font family to use.

+ —

+The font family to use. +

@@ -1381,8 +1439,9 @@ p.serialize(f) - —
-

The name of the font to use

+ —

+The name of the font to use +

@@ -1392,8 +1451,9 @@ p.serialize(f) - —
-

The number format to apply

+ —

+The number format to apply +

@@ -1403,8 +1463,9 @@ p.serialize(f) - —
-

The formatting to apply. If this is specified, num_fmt is ignored.

+ —

+The formatting to apply. If this is specified, num_fmt is ignored. +

@@ -1414,8 +1475,9 @@ p.serialize(f) - —
-

The border style to use.

+ —

+The border style to use. +

@@ -1425,8 +1487,9 @@ p.serialize(f) - —
-

The background color to apply to the cell

+ —

+The background color to apply to the cell +

@@ -1436,8 +1499,9 @@ p.serialize(f) - —
-

Indicates if the cell should be hidden

+ —

+Indicates if the cell should be hidden +

@@ -1447,8 +1511,9 @@ p.serialize(f) - —
-

Indicates if the cell should be locked

+ —

+Indicates if the cell should be locked +

@@ -1458,8 +1523,9 @@ p.serialize(f) - —
-

A hash defining any of the attributes used in CellAlignment

+ —

+A hash defining any of the attributes used in CellAlignment +

@@ -1555,51 +1621,52 @@ p.serialize(f)
# File 'lib/axlsx/stylesheet/styles.rb', line 191
 
-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
+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 + @@ -1614,8 +1681,9 @@ p.serialize(f)

- -

Serializes the styles document

+

+Serializes the styles document +

@@ -1656,16 +1724,17 @@ p.serialize(f)
# File 'lib/axlsx/stylesheet/styles.rb', line 239
 
-def to_xml()
-  builder = Nokogiri::XML::Builder.new(:encoding => ENCODING) do |xml|
-    xml.styleSheet(:xmlns => XML_NS) {
-      [:numFmts, :fonts, :fills, :borders, :cellStyleXfs, :cellXfs, :cellStyles, :dxfs, :tableStyles].each do |key|
-        self.instance_values[key.to_s].to_xml(xml)
-      end
-    }
-  end
-  builder.to_xml
-end
+def to_xml() + builder = Nokogiri::XML::Builder.new(:encoding => ENCODING) do |xml| + xml.styleSheet(:xmlns => XML_NS) { + [:numFmts, :fonts, :fills, :borders, :cellStyleXfs, :cellXfs, :cellStyles, :dxfs, :tableStyles].each do |key| + self.instance_values[key.to_s].to_xml(xml) + end + } + end + builder.to_xml +end + @@ -1676,9 +1745,9 @@ p.serialize(f)
-- cgit v1.2.3