axlsx 1.0.4 → 1.0.5

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -6,8 +6,9 @@ Axlsx: Office Open XML Spreadsheet Generation
6
6
  **Author**: Randy Morgan
7
7
  **Copyright**: 2011
8
8
  **License**: MIT License
9
- **Latest Version**: 1.0.2
10
- **Release Date**: November 20th 2011
9
+ **Latest Version**: 1.0.5
10
+ **Ruby Version**: 1.8.7
11
+ **Release Date**: November 22nd 2011
11
12
 
12
13
  Synopsis
13
14
  --------
@@ -40,88 +41,94 @@ To install Axlsx, use the following command:
40
41
 
41
42
  Usage
42
43
  -----
43
- Simple Workbook
44
44
 
45
- p = Axlsx::Package.new do |package|
46
- package.workbook.add_worksheet do |sheet|
47
- sheet.add_row ["First", "Second", "Third"]
48
- sheet.add_row [1, 2, 3]
49
- end
50
- package.serialize("example1.xlsx")
51
- end
52
-
53
- Generating A Bar Chart
54
-
55
- p = Axlsx::Package.new do |package|
56
- package.workbook.add_worksheet do |sheet|
57
- sheet.add_row ["First", "Second", "Third"]
58
- sheet.add_row [1, 2, 3]
59
- sheet.add_chart(Axlsx::Bar3DChart, :start_at => [0,2], :end_at => [5, 15], :title=>"example 1: Chart") do |chart|
60
- chart.add_series :data=>sheet.rows.last.cells, :labels=> sheet.rows.first.cells
61
- end
62
- end
63
- package.serialize("example1.xlsx")
64
- end
45
+ require 'rubygems'
46
+ require 'axlsx'
65
47
 
66
- Generating A Pie Chart
48
+ ##A Simple Workbook
67
49
 
68
- p = Axlsx::Package.new do |package|
69
- package.workbook.add_worksheet do |sheet|
70
- sheet.add_row ["First", "Second", "Third"]
71
- sheet.add_row [1, 2, 3]
72
- sheet.add_chart(Axlsx::Pie3DChart, :start_at => [0,2], :end_at => [5, 15], :title=>"example 2: Pie Chart") do |chart|
73
- chart.add_series :data=>sheet.rows.last.cells, :labels=> sheet.rows.first.cells
74
- end
75
- end
76
- package.serialize("example3.xlsx")
77
- end
50
+ p = Axlsx::Package.new
51
+ p.workbook.add_worksheet do |sheet|
52
+ sheet.add_row ["First", "Second", "Third"]
53
+ sheet.add_row [1, 2, 3]
54
+ end
55
+ p.serialize("example1.xlsx")
78
56
 
79
- Using Custom Styles
57
+ ##Generating A Bar Chart
80
58
 
81
- p = Axlsx::Package.new do |package|
82
- style_options = { :bg_color => "FF000000", :fg_color => "FFFFFFFF", :sz=>14, :alignment => { :horizontal=> :center } }
83
- header_style = package.workbook.styles.add_style style_options
84
- package.workbook.add_worksheet do |sheet|
85
- sheet.add_row ["Text Autowidth", "Second", "Third"], :style => header_style
86
- sheet.add_row [1, 2, 3], :style => Axlsx::STYLE_THIN_BORDER
59
+ p = Axlsx::Package.new
60
+ p.workbook.add_worksheet do |sheet|
61
+ sheet.add_row ["First", "Second", "Third"]
62
+ sheet.add_row [1, 2, 3]
63
+ sheet.add_chart(Axlsx::Bar3DChart, :start_at => [0,2], :end_at => [5, 15], :title=>"example 2: Chart") do |chart|
64
+ chart.add_series :data=>sheet.rows.last.cells, :labels=> sheet.rows.first.cells
87
65
  end
88
- package.serialize("example3.xlsx")
89
66
  end
67
+ p.serialize("example2.xlsx")
90
68
 
91
- Cell Specifc Styles
92
-
93
- p = Axlsx::Package.new do |package|
69
+ ##Generating A Pie Chart
94
70
 
95
- black_cell_spec = { :bg_color => "FF000000", :fg_color => "FFFFFFFF", :sz=>14, :alignment => { :horizontal=> :center } }
96
- blue_cell_spec = { :bg_color => "FF0000FF", :fg_color => "FFFFFFFF", :sz=>14, :alignment => { :horizontal=> :center } }
97
-
98
- black_cell = package.workbook.styles.add_style black_cell_spec
99
- blue_cell = package.workbook.styles.add_style blue_cell_spec
100
-
101
- # date1904 support. uncomment the line below if you are working on a mac.
102
- # package.workbook.date1904 = true
103
-
104
- package.workbook.add_worksheet do |sheet|
105
- sheet.add_row ["Text Autowidth", "Second", "Third"], :style => [black_cell, blue_cell, black_cell]
106
- sheet.add_row [1, 2, 3], :style => Axlsx::STYLE_THIN_BORDER
71
+ p = Axlsx::Package.new
72
+ p.workbook.add_worksheet do |sheet|
73
+ sheet.add_row ["First", "Second", "Third"]
74
+ sheet.add_row [1, 2, 3]
75
+ sheet.add_chart(Axlsx::Pie3DChart, :start_at => [0,2], :end_at => [5, 15], :title=>"example 3: Pie Chart") do |chart|
76
+ chart.add_series :data=>sheet.rows.last.cells, :labels=> sheet.rows.first.cells
107
77
  end
108
- package.serialize("example3.xlsx")
109
78
  end
110
-
111
- Number and Date formatting
112
-
113
- p = Axlsx::Package.new do |package|
114
- date = package.workbook.styles.add_style :format_code=>"yyyy-mm-dd", :border => Axlsx::STYLE_THIN_BORDER
115
- padded = package.workbook.styles.add_style :format_code=>"00#", :border => Axlsx::STYLE_THIN_BORDER
116
- percent = package.workbook.styles.add_style :format_code=>"0%", :border => Axlsx::STYLE_THIN_BORDER
117
-
118
- package.workbook.add_worksheet do |sheet|
119
- sheet.add_row
120
- sheet.add_row ["Custom Formatted Date", "Percent Formatted Float", "Padded Numbers"], :style => Axlsx::STYLE_THIN_BORDER
121
- sheet.add_row [Time.now, 0.2, 32], :style => [date, percent, padded]
79
+ p.serialize("example3.xlsx")
80
+
81
+ ##Using Custom Styles
82
+
83
+ p = Axlsx::Package.new
84
+ wb = p.workbook
85
+ black_cell = wb.styles.add_style :bg_color => "FF000000", :fg_color => "FFFFFFFF", :sz=>14, :alignment => { :horizontal=> :center }
86
+ blue_cell = wb.styles.add_style :bg_color => "FF0000FF", :fg_color => "FFFFFFFF", :sz=>14, :alignment => { :horizontal=> :center }
87
+ wb.add_worksheet do |sheet|
88
+ sheet.add_row ["Text Autowidth", "Second", "Third"], :style => [black_cell, blue_cell, black_cell]
89
+ sheet.add_row [1, 2, 3], :style => Axlsx::STYLE_THIN_BORDER
90
+ end
91
+ p.serialize("example4.xlsx")
92
+
93
+ ##Using Custom Formatting and date1904
94
+
95
+ p = Axlsx::Package.new
96
+ wb = p.workbook
97
+ date = wb.styles.add_style :format_code=>"yyyy-mm-dd", :border => Axlsx::STYLE_THIN_BORDER
98
+ padded = wb.styles.add_style :format_code=>"00#", :border => Axlsx::STYLE_THIN_BORDER
99
+ percent = wb.styles.add_style :format_code=>"0%", :border => Axlsx::STYLE_THIN_BORDER
100
+ wb.date1904 = true # required for generation on mac
101
+ wb.add_worksheet do |sheet|
102
+ sheet.add_row ["Custom Formatted Date", "Percent Formatted Float", "Padded Numbers"], :style => Axlsx::STYLE_THIN_BORDER
103
+ sheet.add_row [Time.now, 0.2, 32], :style => [date, percent, padded]
104
+ end
105
+ p.serialize("example5.xlsx")
106
+
107
+ ##Validation
108
+
109
+ p = Axlsx::Package.new
110
+ p.workbook.add_worksheet do |sheet|
111
+ sheet.add_row ["First", "Second", "Third"]
112
+ sheet.add_row [1, 2, 3]
113
+ end
114
+
115
+ p.validate.each do |error|
116
+ puts error.inspect
117
+ end
118
+
119
+ #Generating A Line Chart
120
+
121
+ p = Axlsx::Package.new
122
+ p.workbook.add_worksheet do |sheet|
123
+ sheet.add_row ["First", 1, 5, 7, 9]
124
+ sheet.add_row ["Second", 5, 2, 14, 9]
125
+ sheet.add_chart(Axlsx::Line3DChart, :start_at => [0,2], :end_at => [10, 15], :title=>"example 6: Line Chart") do |chart|
126
+ chart.add_series :data=>sheet.rows.first.cells[(1..-1)], :title=> sheet.rows.first.cells.first
127
+ chart.add_series :data=>sheet.rows.last.cells[(1..-1)], :title=> sheet.rows.last.cells.first
122
128
  end
123
- package.serialize("example5.xlsx")
124
129
  end
130
+ p.serialize("example6.xlsx")
131
+
125
132
 
126
133
  ### Documentation
127
134
  This gem is 100% documented with YARD, an exceptional documentation library. To see documentation for this, and all the gems installed on your system use:
@@ -130,7 +137,7 @@ This gem is 100% documented with YARD, an exceptional documentation library. To
130
137
 
131
138
 
132
139
  ### Specs
133
- This gem has 100% test coverage. To execute tests for this gem, simply run rake in the gem directory.
140
+ This gem has 100% test coverage using test/unit. To execute tests for this gem, simply run rake in the gem directory.
134
141
 
135
142
  Changelog
136
143
  ---------
@@ -138,8 +145,24 @@ Changelog
138
145
  - **October.20.11**: 0.1.0 release
139
146
  - **October.21.11**: 1.0.3 release
140
147
  - Updated documentation
148
+ - **October.21.11**: 1.0.4
141
149
  - altered package to accept a filename string for serialization instead of a File object.
142
150
  - Updated specs to conform
151
+ - More examples for readme
152
+ - **October.22.11**: 1.05
153
+ - Added support for line charts
154
+ - Updated examples and readme
155
+ - Updated series title to be a real title ** NOTE ** If you are accessing titles directly you will need to update text assignation to title.text = v
156
+ - BugFix: shape attribute for bar chart is now properly serialized
157
+ - BugFix: date1904 property now properly set for charts
158
+ - Added style property to charts
159
+ - Removed serialization write test as it most commonly fails when run from the gem's intalled directory
160
+
161
+ On Deck
162
+ -------
163
+
164
+ - Verification with ruby 1.9.3
165
+ - Active Record support via package::serialize_ar so you can dump an AR result into a worksheet in one go.
143
166
 
144
167
  Copyright
145
168
  ---------
@@ -0,0 +1,83 @@
1
+ require 'rubygems'
2
+ require 'axlsx'
3
+
4
+ #A Simple Workbook
5
+ p = Axlsx::Package.new
6
+ p.workbook.add_worksheet do |sheet|
7
+ sheet.add_row ["First", "Second", "Third"]
8
+ sheet.add_row [1, 2, 3]
9
+ end
10
+ p.serialize("example1.xlsx")
11
+
12
+ #Generating A Bar Chart
13
+ p = Axlsx::Package.new
14
+ p.workbook.add_worksheet do |sheet|
15
+ sheet.add_row ["First", "Second", "Third"]
16
+ sheet.add_row [1, 2, 3]
17
+ sheet.add_chart(Axlsx::Bar3DChart, :start_at => [0,2], :end_at => [5, 15], :title=>"example 2: Chart") do |chart|
18
+ chart.add_series :data=>sheet.rows.last.cells, :labels=> sheet.rows.first.cells
19
+ end
20
+ end
21
+ p.serialize("example2.xlsx")
22
+
23
+ #Generating A Pie Chart
24
+ p = Axlsx::Package.new
25
+ p.workbook.add_worksheet do |sheet|
26
+ sheet.add_row ["First", "Second", "Third"]
27
+ sheet.add_row [1, 2, 3]
28
+ sheet.add_chart(Axlsx::Pie3DChart, :start_at => [0,2], :end_at => [5, 15], :title=>"example 3: Pie Chart") do |chart|
29
+ chart.add_series :data=>sheet.rows.last.cells, :labels=> sheet.rows.first.cells
30
+ end
31
+ end
32
+ p.serialize("example3.xlsx")
33
+
34
+
35
+ #Using Custom Styles
36
+
37
+ p = Axlsx::Package.new
38
+ wb = p.workbook
39
+ black_cell = wb.styles.add_style :bg_color => "FF000000", :fg_color => "FFFFFFFF", :sz=>14, :alignment => { :horizontal=> :center }
40
+ blue_cell = wb.styles.add_style :bg_color => "FF0000FF", :fg_color => "FFFFFFFF", :sz=>14, :alignment => { :horizontal=> :center }
41
+ wb.add_worksheet do |sheet|
42
+ sheet.add_row ["Text Autowidth", "Second", "Third"], :style => [black_cell, blue_cell, black_cell]
43
+ sheet.add_row [1, 2, 3], :style => Axlsx::STYLE_THIN_BORDER
44
+ end
45
+ p.serialize("example4.xlsx")
46
+
47
+ #Using Custom Formatting and date1904
48
+
49
+ p = Axlsx::Package.new
50
+ wb = p.workbook
51
+ date = wb.styles.add_style :format_code=>"yyyy-mm-dd", :border => Axlsx::STYLE_THIN_BORDER
52
+ padded = wb.styles.add_style :format_code=>"00#", :border => Axlsx::STYLE_THIN_BORDER
53
+ percent = wb.styles.add_style :format_code=>"0%", :border => Axlsx::STYLE_THIN_BORDER
54
+ wb.date1904 = true # required for generation on mac
55
+ wb.add_worksheet do |sheet|
56
+ sheet.add_row ["Custom Formatted Date", "Percent Formatted Float", "Padded Numbers"], :style => Axlsx::STYLE_THIN_BORDER
57
+ sheet.add_row [Time.now, 0.2, 32], :style => [date, percent, padded]
58
+ end
59
+ p.serialize("example5.xlsx")
60
+
61
+ #Validation
62
+ p = Axlsx::Package.new
63
+ p.workbook.add_worksheet do |sheet|
64
+ sheet.add_row ["First", "Second", "Third"]
65
+ sheet.add_row [1, 2, 3]
66
+ end
67
+
68
+ p.validate.each do |error|
69
+ puts error.inspect
70
+ end
71
+
72
+ #Generating A Line Chart
73
+
74
+ p = Axlsx::Package.new
75
+ p.workbook.add_worksheet do |sheet|
76
+ sheet.add_row ["First", 1, 5, 7, 9]
77
+ sheet.add_row ["Second", 5, 2, 14, 9]
78
+ sheet.add_chart(Axlsx::Line3DChart, :start_at => [0,2], :end_at => [10, 15], :title=>"example 6: Line Chart") do |chart|
79
+ chart.add_series :data=>sheet.rows.first.cells[(1..-1)], :title=> sheet.rows.first.cells.first
80
+ chart.add_series :data=>sheet.rows.last.cells[(1..-1)], :title=> sheet.rows.last.cells.first
81
+ end
82
+ end
83
+ p.serialize("example6.xlsx")
@@ -0,0 +1,79 @@
1
+ require 'rubygems'
2
+ require 'axlsx'
3
+
4
+ wb =Axlsx::Workbook.new(:date1904=>true)
5
+ p = Axlsx::Package.new(:workbook=>wb)
6
+
7
+ styles = wb.styles
8
+
9
+ header_style = styles.add_style(:bg_color=>"FF000000", :fg_color=>"FFFFFFFF", :sz=>14, :alignment=>{:horizontal=>:center})
10
+ table_title = styles.add_style :sz=>22
11
+ date_time = styles.add_style(:num_fmt=>Axlsx::NUM_FMT_YYYYMMDDHHMMSS)
12
+ date = styles.add_style :num_fmt=>Axlsx::NUM_FMT_YYYYMMDD
13
+ bordered = styles.add_style :border=>Axlsx::STYLE_THIN_BORDER
14
+
15
+ wb.add_worksheet do |sheet|
16
+ sheet.name = "Example 1"
17
+ # add cells as a row
18
+ sheet.add_row :values=>['Example 1 Styling and Bar Chart'], :style=>table_title
19
+ sheet.add_row :values=>["Formatted Time Stamp", Time.now],:style=>[nil, date_time]
20
+ # adding cells one at a time
21
+ sheet.add_row do |row|
22
+ row.add_cell "Formatted Date"
23
+ row.add_cell Time.now, :style => date, :type=>:time
24
+ end
25
+
26
+ # Bar 3D Chart
27
+ sheet.add_row
28
+ label_row = sheet.add_row :values=>["0 ~ 10", "11 ~ 20", "21 ~ 60"], :style=>header_style
29
+ data_row = sheet.add_row :values=>[12, 60, 25], :style=>bordered
30
+ sheet.add_chart(Axlsx::Bar3DChart, :show_legend=>false) do |chart|
31
+ chart.title = Axlsx::Title.new(sheet.rows.first.cells.first)
32
+ chart.add_series :data => data_row.cells, :labels => label_row.cells
33
+ chart.start_at.coord(0, 7)
34
+ chart.end_at.coord(3, 17)
35
+ end
36
+
37
+ end
38
+
39
+ # Blocks are not required
40
+ percent = styles.add_style :num_fmt=>Axlsx::NUM_FMT_PERCENT, :border=>Axlsx::STYLE_THIN_BORDER
41
+ wb.add_worksheet(:name=>"Example 2") do |sheet|
42
+ sheet.add_row :values=>["Example 2 - Styling and Pie Chart"], :style=>table_title
43
+ label_row = sheet.add_row(:values=>["Summer", "Fall", "Winter", "Spring"], :style=>header_style)
44
+ data_row = sheet.add_row :values=>[0.5, 0.2, 0.2, 0.1], :style=>percent
45
+ chart = sheet.add_chart(Axlsx::Pie3DChart, :start_at => [0,4], :end_at => [4,14], :title => "Pie Consumption per Season") do |chart|
46
+ chart.add_series :data => data_row.cells, :labels=>label_row.cells
47
+ end
48
+ end
49
+ # Charts can be build with out data in the sheet
50
+ wb.add_worksheet(:name=>"Example 3") do |ws|
51
+ ws.add_row :values=>["Charts can be build without any data in the worksheet"]
52
+ ws.add_chart(Axlsx::Pie3DChart, :title=>"free chart 1") do |chart|
53
+ chart.start_at.coord(0,2)
54
+ chart.end_at.coord(3,12)
55
+ chart.add_series :data => [13,54,1], :labels=>["nothing","in","the sheet"]
56
+ end
57
+ ws.add_chart(Axlsx::Pie3DChart) do |chart|
58
+ chart.start_at.coord(0,13)
59
+ chart.end_at.coord(3,23)
60
+ chart.add_series :data => [1,4,5], :labels=>["nothing","in","the sheet"], :title=>"free chart 2"
61
+ end
62
+ end
63
+
64
+ # wb.add_worksheet(:name=>"Example 4") do |sheet|
65
+ # sheet.add_row :values=>["lots of data!"]
66
+ # (1..100).each do |i|
67
+ # cells = (1..50).map { |i| rand(5 ** 10) }
68
+ # sheet.add_row :values=>cells
69
+ # end
70
+ # end
71
+
72
+ errors = p.validate
73
+ if errors.empty?
74
+ f = File.open('test.xlsx', 'w')
75
+ p.serialize(f)
76
+ else
77
+ puts errors.to_s
78
+ end
79
+
Binary file
@@ -77,8 +77,6 @@ module Axlsx
77
77
  # @option options [String] gapDepth
78
78
  # @option options [Symbol] shape
79
79
  def initialize(frame, options={})
80
- super(frame, options)
81
- @series_type = BarSeries
82
80
  @barDir = :bar
83
81
  @grouping = :clustered
84
82
  @catAxId = rand(8 ** 8)
@@ -86,6 +84,8 @@ module Axlsx
86
84
  @catAxis = CatAxis.new(@catAxId, @valAxId)
87
85
  @valAxis = ValAxis.new(@valAxId, @catAxId)
88
86
  @view3D = View3D.new(:rAngAx=>1)
87
+ super(frame, options)
88
+ @series_type = BarSeries
89
89
  end
90
90
 
91
91
  def barDir=(v)
@@ -73,6 +73,7 @@ module Axlsx
73
73
  }
74
74
  }
75
75
  }
76
+ xml.send('c:shape', :val=>@shape)
76
77
  end
77
78
  end
78
79
 
@@ -1,3 +1,4 @@
1
+ # -*- coding: utf-8 -*-
1
2
  module Axlsx
2
3
  # A Chart is the superclass for specific charts
3
4
  # @note Worksheet#add_chart is the recommended way to create charts for your worksheets.
@@ -7,6 +8,11 @@ module Axlsx
7
8
  # @return [Title]
8
9
  attr_accessor :title
9
10
 
11
+ # The style for the chart.
12
+ # see ECMA Part 1 §21.2.2.196
13
+ # @return [Integer]
14
+ attr_accessor :style
15
+
10
16
  # The 3D view properties for the chart
11
17
  attr_accessor :view3D
12
18
 
@@ -50,6 +56,7 @@ module Axlsx
50
56
  # @option options [Cell, String] title
51
57
  # @option options [Boolean] show_legend
52
58
  def initialize(frame, options={})
59
+ @style = 2
53
60
  @graphic_frame=frame
54
61
  @graphic_frame.anchor.drawing.worksheet.workbook.charts << self
55
62
  @series = SimpleTypedList.new Series
@@ -79,6 +86,8 @@ module Axlsx
79
86
 
80
87
  def show_legend=(v) Axlsx::validate_boolean(v); @show_legend = v; end
81
88
 
89
+ def style=(v) DataTypeValidator.validate "Chart.style", Integer, v, lambda { |v| v >= 1 && v <= 48 }; @style = v; end
90
+
82
91
  # Adds a new series to the chart's series collection.
83
92
  # @return [Series]
84
93
  # @see Series
@@ -92,6 +101,8 @@ module Axlsx
92
101
  def to_xml
93
102
  builder = Nokogiri::XML::Builder.new(:encoding => ENCODING) do |xml|
94
103
  xml.send('c:chartSpace',:'xmlns:c' => XML_NS_C, :'xmlns:a' => XML_NS_A) {
104
+ xml.send('c:date1904', :val=>Axlsx::Workbook.date1904)
105
+ xml.send('c:style', :val=>style)
95
106
  xml.send('c:chart') {
96
107
  @title.to_xml(xml) unless @title.nil?
97
108
  @view3D.to_xml(xml) unless @view3D.nil?
@@ -1,14 +1,17 @@
1
1
  module Axlsx
2
+ require 'axlsx/drawing/title.rb'
3
+ require 'axlsx/drawing/series_title.rb'
2
4
  require 'axlsx/drawing/series.rb'
3
5
  require 'axlsx/drawing/pie_series.rb'
4
6
  require 'axlsx/drawing/bar_series.rb'
7
+ require 'axlsx/drawing/line_series.rb'
5
8
 
6
9
  require 'axlsx/drawing/axis.rb'
10
+ require 'axlsx/drawing/ser_axis.rb'
7
11
  require 'axlsx/drawing/cat_axis.rb'
8
12
  require 'axlsx/drawing/val_axis.rb'
9
13
  require 'axlsx/drawing/view_3D.rb'
10
14
  require 'axlsx/drawing/scaling.rb'
11
- require 'axlsx/drawing/title.rb'
12
15
 
13
16
  require 'axlsx/drawing/graphic_frame.rb'
14
17
  require 'axlsx/drawing/marker.rb'
@@ -17,6 +20,7 @@ module Axlsx
17
20
  require 'axlsx/drawing/chart.rb'
18
21
  require 'axlsx/drawing/pie_3D_chart.rb'
19
22
  require 'axlsx/drawing/bar_3D_chart.rb'
23
+ require 'axlsx/drawing/line_3D_chart.rb'
20
24
 
21
25
  # A Drawing is a canvas for charts. Each worksheet has a single drawing that can specify multiple anchors which reference charts.
22
26
  # @note The recommended way to manage drawings is to use the Worksheet.add_chart method, specifying the chart class, start and end marker locations.