write_xlsx 0.77.2 → 0.78.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (55) hide show
  1. checksums.yaml +4 -4
  2. data/Changes +7 -0
  3. data/README.md +1 -1
  4. data/examples/chart_doughnut.rb +131 -0
  5. data/lib/write_xlsx/chart.rb +4 -1
  6. data/lib/write_xlsx/chart/doughnut.rb +90 -0
  7. data/lib/write_xlsx/chart/pie.rb +16 -2
  8. data/lib/write_xlsx/format.rb +6 -2
  9. data/lib/write_xlsx/utility.rb +31 -2
  10. data/lib/write_xlsx/version.rb +1 -1
  11. data/lib/write_xlsx/workbook.rb +55 -10
  12. data/lib/write_xlsx/worksheet.rb +32 -12
  13. data/lib/write_xlsx/worksheet/page_setup.rb +14 -7
  14. data/test/perl_output/chart_doughnut.xlsx +0 -0
  15. data/test/regression/test_chart_column09.rb +43 -0
  16. data/test/regression/test_chart_column10.rb +43 -0
  17. data/test/regression/test_chart_doughnut01.rb +43 -0
  18. data/test/regression/test_chart_doughnut02.rb +42 -0
  19. data/test/regression/test_chart_doughnut03.rb +42 -0
  20. data/test/regression/test_chart_doughnut04.rb +42 -0
  21. data/test/regression/test_chart_doughnut05.rb +42 -0
  22. data/test/regression/test_chart_doughnut06.rb +38 -0
  23. data/test/regression/test_chart_pie05.rb +38 -0
  24. data/test/regression/test_default_format01.rb +26 -0
  25. data/test/regression/test_excel2003_style01.rb +21 -0
  26. data/test/regression/test_excel2003_style02.rb +37 -0
  27. data/test/regression/test_excel2003_style03.rb +40 -0
  28. data/test/regression/test_excel2003_style04.rb +24 -0
  29. data/test/regression/test_excel2003_style05.rb +31 -0
  30. data/test/regression/test_excel2003_style06.rb +31 -0
  31. data/test/regression/test_excel2003_style07.rb +31 -0
  32. data/test/regression/test_excel2003_style08.rb +26 -0
  33. data/test/regression/xlsx_files/chart_column09.xlsx +0 -0
  34. data/test/regression/xlsx_files/chart_column10.xlsx +0 -0
  35. data/test/regression/xlsx_files/chart_doughnut01.xlsx +0 -0
  36. data/test/regression/xlsx_files/chart_doughnut02.xlsx +0 -0
  37. data/test/regression/xlsx_files/chart_doughnut03.xlsx +0 -0
  38. data/test/regression/xlsx_files/chart_doughnut04.xlsx +0 -0
  39. data/test/regression/xlsx_files/chart_doughnut05.xlsx +0 -0
  40. data/test/regression/xlsx_files/chart_doughnut06.xlsx +0 -0
  41. data/test/regression/xlsx_files/chart_pie05.xlsx +0 -0
  42. data/test/regression/xlsx_files/default_format01.xlsx +0 -0
  43. data/test/regression/xlsx_files/excel2003_style01.xlsx +0 -0
  44. data/test/regression/xlsx_files/excel2003_style02.xlsx +0 -0
  45. data/test/regression/xlsx_files/excel2003_style03.xlsx +0 -0
  46. data/test/regression/xlsx_files/excel2003_style04.xlsx +0 -0
  47. data/test/regression/xlsx_files/excel2003_style05.xlsx +0 -0
  48. data/test/regression/xlsx_files/excel2003_style06.xlsx +0 -0
  49. data/test/regression/xlsx_files/excel2003_style07.xlsx +0 -0
  50. data/test/regression/xlsx_files/excel2003_style08.xlsx +0 -0
  51. data/test/test_example_match.rb +116 -0
  52. data/test/test_option_hash_for_workbook.rb +72 -0
  53. data/test/workbook/test_write_calc_pr.rb +41 -0
  54. data/test/worksheet/test_write_phonetic_pr.rb +1 -1
  55. metadata +83 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4fff0ad728ab04118c85e81ac51f896a2977b5dc
4
- data.tar.gz: 0c47ce7030705137f1f737c52586a75134a3fad0
3
+ metadata.gz: 97af755aa6f07672a642c569fa9916a0e18365d8
4
+ data.tar.gz: f3ca578778d1725fe6a7936e37d5586bdb834eba
5
5
  SHA512:
6
- metadata.gz: bd8f0752cfd08e0ce6fa02d73f9652e811f47a06f96b5ca3e6c1c4e99a35cf8f7490d9f23497f688cb8c6270d16f45c0de8920aec9e06f68404f169a7a22e7b7
7
- data.tar.gz: a1a9bdd2c569b3b81773f08b911e044501d29534d05f9b9c0d10b34fad8b240bb1df3c73e8e6666cfa4841fe6b974b1296354a87bc648965228006b9a54d5d82
6
+ metadata.gz: 21399bff146d040567803600ed057b6b2e7eb5d49beee2a4744aefe19ef6987958d4152e050e1d0ee7ec58ad87454ece4bc90250f316f5bbdfb50b7f0a420bbb
7
+ data.tar.gz: 376f41e382cf9c8f88c35ee1dbe69a567c1189ec20e2b010d9cfa9ef39507abd5eb728893ee2aca931f6dffe5caef1aa23568ab3095fb1468e7208deeb76d0d4
data/Changes CHANGED
@@ -1,4 +1,11 @@
1
1
  Change history of write_xlsx rubygem.
2
+ 2014-11-22 v0.78.0
3
+ Added Doughnut chart with set_rotation and set_hole_size methods.
4
+ Added set_rotation method to Pie charts.
5
+ Added set_calc_mode method to control automatic calculation of
6
+ formulas when worksheet is opened.
7
+
8
+
2
9
  2014-08-19 v0.77.2
3
10
  Fix for issue #16.
4
11
 
data/README.md CHANGED
@@ -4,7 +4,7 @@
4
4
 
5
5
  gem to create a new file in the Excel 2007+ XLSX format, and you can use the
6
6
  same interface as writeexcel gem. write_xlsx is converted from Perl's module
7
- [Excel::Writer::XLSX-0.77](https://github.com/jmcnamara/excel-writer-xlsx) .
7
+ [Excel::Writer::XLSX-0.78](https://github.com/jmcnamara/excel-writer-xlsx) .
8
8
 
9
9
  ## Description
10
10
 
@@ -0,0 +1,131 @@
1
+ #!/usr/bin/env ruby
2
+ # -*- coding: utf-8 -*-
3
+
4
+ #######################################################################
5
+ #
6
+ # A demo of a Doughnut chart in Excel::Writer::XLSX.
7
+ #
8
+ # The demo also shows how to set segment colours. It is possible to define
9
+ # chart colors for most types of Excel::Writer::XLSX charts via the
10
+ # add_series() method. However, Pie and Doughtnut charts are a special case
11
+ # since each segment is represented as a point so it is necessary to assign
12
+ # formatting to each point in the series.
13
+ #
14
+ # reverse ('(c)'), March 2011, John McNamara, jmcnamara@cpan.org
15
+ # convert to ruby by Hideo NAKAMURA, cxn03651@msj.biglobe.ne.jp
16
+ #
17
+
18
+ require 'rubygems'
19
+ require 'write_xlsx'
20
+
21
+ workbook = WriteXLSX.new('chart_doughnut.xlsx')
22
+ worksheet = workbook.add_worksheet
23
+ bold = workbook.add_format(:bold => 1)
24
+
25
+ # Add the worksheet data that the charts will refer to.
26
+ headings = [ 'Category', 'Values' ]
27
+ data = [
28
+ [ 'Glazed', 'Chocolate', 'Cream' ],
29
+ [ 50, 35, 15 ]
30
+ ]
31
+
32
+ worksheet.write('A1', headings, bold)
33
+ worksheet.write('A2', data)
34
+
35
+ # Create a new chart object. In this case an embedded chart.
36
+ chart1 = workbook.add_chart(:type => 'doughnut', :embedded => 1)
37
+
38
+ # Configure the series. Note the use of the array ref to define ranges:
39
+ # [ $sheetname, $row_start, $row_end, $col_start, $col_end ].
40
+ # See below for an alternative syntax.
41
+ chart1.add_series(
42
+ :name => 'Doughnut sales data',
43
+ :categories => [ 'Sheet1', 1, 3, 0, 0 ],
44
+ :values => [ 'Sheet1', 1, 3, 1, 1 ]
45
+ );
46
+
47
+ # Add a title.
48
+ chart1.set_title(:name => 'Popular Doughnut Types')
49
+
50
+ # Set an Excel chart style. Colors with white outline and shadow.
51
+ chart1.set_style(10)
52
+
53
+ # Insert the chart into the worksheet (with an offset).
54
+ worksheet.insert_chart('C2', chart1, 25, 10)
55
+
56
+
57
+ #
58
+ # Create a Doughnut chart with user defined segment colors.
59
+ #
60
+
61
+ # Create an example Doughnut chart like above.
62
+ chart2 = workbook.add_chart(:type => 'doughnut', :embedded => 1)
63
+
64
+ # Configure the series and add user defined segment colours.
65
+ chart2.add_series(
66
+ :name => 'Doughnut sales data',
67
+ :categories => '=Sheet1!$A$2:$A$4',
68
+ :values => '=Sheet1!$B$2:$B$4',
69
+ :points => [
70
+ { :fill => { :color => '#FA58D0' } },
71
+ { :fill => { :color => '#61210B' } },
72
+ { :fill => { :color => '#F5F6CE' } }
73
+ ]
74
+ )
75
+
76
+ # Add a title.
77
+ chart2.set_title(:name => 'Doughnut Chart with user defined colors')
78
+
79
+
80
+ # Insert the chart into the worksheet (with an offset).
81
+ worksheet.insert_chart('C18', chart2, 25, 10)
82
+
83
+
84
+ #
85
+ # Create a Doughnut chart with rotation of the segments.
86
+ #
87
+
88
+ # Create an example Doughnut chart like above.
89
+ chart3 = workbook.add_chart(:type => 'doughnut', :embedded => 1)
90
+
91
+ # Configure the series.
92
+ chart3.add_series(
93
+ :name => 'Doughnut sales data',
94
+ :categories => '=Sheet1!$A$2:$A$4',
95
+ :values => '=Sheet1!$B$2:$B$4'
96
+ )
97
+
98
+ # Add a title.
99
+ chart3.set_title(:name => 'Doughnut Chart with segment rotation')
100
+
101
+ # Change the angle/rotation of the first segment.
102
+ chart3.set_rotation(90)
103
+
104
+ # Insert the chart into the worksheet (with an offset).
105
+ worksheet.insert_chart('C34', chart3, 25, 10)
106
+
107
+
108
+ #
109
+ # Create a Doughnut chart with user defined hole size.
110
+ #
111
+
112
+ # Create an example Doughnut chart like above.
113
+ chart4 = workbook.add_chart(:type => 'doughnut', :embedded => 1)
114
+
115
+ # Configure the series.
116
+ chart4.add_series(
117
+ :name => 'Doughnut sales data',
118
+ :categories => '=Sheet1!$A$2:$A$4',
119
+ :values => '=Sheet1!$B$2:$B$4'
120
+ )
121
+
122
+ # Add a title.
123
+ chart4.set_title(:name => 'Doughnut Chart with user defined hole size')
124
+
125
+ # Change the hole size.
126
+ chart4.set_hole_size(33)
127
+
128
+ # Insert the chart into the worksheet (with an offset).
129
+ worksheet.insert_chart('C50', chart4, 25, 10)
130
+
131
+ workbook.close
@@ -156,6 +156,9 @@ def self.factory(current_subclass, subtype = nil) # :nodoc:
156
156
  when 'Column'
157
157
  require 'write_xlsx/chart/column'
158
158
  Chart::Column.new(subtype)
159
+ when 'Doughnut'
160
+ require 'write_xlsx/chart/doughnut'
161
+ Chart::Doughnut.new(subtype)
159
162
  when 'Line'
160
163
  require 'write_xlsx/chart/line'
161
164
  Chart::Line.new(subtype)
@@ -1926,7 +1929,7 @@ def write_sp_pr(series) # :nodoc:
1926
1929
  (!fill || !ptrue?(fill[:_defined]))
1927
1930
 
1928
1931
  @writer.tag_elements('c:spPr') do
1929
- # Write the fill elements for solid charts such as pie and bar.
1932
+ # Write the fill elements for solid charts such as pie/doughnut and bar.
1930
1933
  if fill && fill[:_defined] != 0
1931
1934
  if ptrue?(fill[:none])
1932
1935
  # Write the a:noFill element.
@@ -0,0 +1,90 @@
1
+ # -*- coding: utf-8 -*-
2
+ ###############################################################################
3
+ #
4
+ # Doughnut - A class for writing Excel Doughnut charts.
5
+ #
6
+ # Used in conjunction with Excel::Writer::XLSX::Chart.
7
+ #
8
+ # See formatting note in Excel::Writer::XLSX::Chart.
9
+ #
10
+ # Copyright 2000-2014, John McNamara, jmcnamara@cpan.org
11
+ # Convert to ruby by Hideo NAKAMURA, cxn03651@msj.biglobe.ne.jp
12
+ #
13
+
14
+ require 'write_xlsx/package/xml_writer_simple'
15
+ require 'write_xlsx/chart'
16
+ require 'write_xlsx/chart/pie'
17
+ require 'write_xlsx/utility'
18
+
19
+ module Writexlsx
20
+ class Chart
21
+ class Doughnut < Pie
22
+ include Writexlsx::Utility
23
+
24
+ def initialize(subtype)
25
+ super(subtype)
26
+ @vary_data_color = 1
27
+ @hole_size = 50
28
+ @rotation = 0
29
+ end
30
+
31
+ #
32
+ # set_hole_size
33
+ #
34
+ # Set the Doughnut chart hole size.
35
+ #
36
+ def set_hole_size(size)
37
+ return unless size
38
+
39
+ if size >= 10 && size <= 90
40
+ @hole_size = size
41
+ else
42
+ raise "Hole size $size outside Excel range: 10 <= size <= 90";
43
+ end
44
+ end
45
+
46
+ private
47
+
48
+ #
49
+ # write_chart_type
50
+ #
51
+ # Override the virtual superclass method with a chart specific method.
52
+ #
53
+ def write_chart_type
54
+ # Write the c:doughnutChart element.
55
+ write_doughnut_chart
56
+ end
57
+
58
+ #
59
+ # write_doughnut_chart
60
+ #
61
+ # Write the <c:doughnutChart> element. Over-ridden method to remove axis_id code
62
+ # since Doughnut charts don't require val and cat axes.
63
+ #
64
+ def write_doughnut_chart
65
+ @writer.tag_elements('c:doughnutChart') do
66
+ # Write the c:varyColors element.
67
+ write_vary_colors
68
+
69
+ # Write the series elements.
70
+ @series.each { |s| write_ser(s) }
71
+
72
+ # Write the c:firstSliceAng element.
73
+ write_first_slice_ang
74
+
75
+ # Write the c:holeSize element.
76
+ write_hole_size
77
+ end
78
+ end
79
+
80
+ #
81
+ # write_hole_size
82
+ #
83
+ # Write the <c:holeSize> element.
84
+ #
85
+ def write_hole_size
86
+ @writer.empty_tag('c:holeSize', [ ['val', @hole_size] ])
87
+ end
88
+ end
89
+ end
90
+ end
@@ -28,6 +28,20 @@ class Pie < self
28
28
 
29
29
  def initialize(subtype)
30
30
  super(subtype)
31
+ @vary_data_color = 1
32
+ @rotation = 0
33
+ end
34
+
35
+ #
36
+ # Set the Pie/Doughnut chart rotation: the angle of the first slice.
37
+ #
38
+ def set_rotation(rotation)
39
+ return unless rotation
40
+ if rotation >= 0 && rotation <= 360
41
+ @rotation = rotation
42
+ else
43
+ raise "Chart rotation $rotation outside range: 0 <= rotation <= 360"
44
+ end
31
45
  end
32
46
 
33
47
  #
@@ -55,7 +69,7 @@ def write_pie_chart
55
69
 
56
70
  #
57
71
  # Over-ridden method to remove the cat_axis() and val_axis() code since
58
- # Pie charts don't require those axes.
72
+ # Pie/Doughnut charts don't require those axes.
59
73
  #
60
74
  # Write the <c:plotArea> element.
61
75
  #
@@ -156,7 +170,7 @@ def write_vary_colors
156
170
  # Write the <c:firstSliceAng> element.
157
171
  #
158
172
  def write_first_slice_ang
159
- @writer.empty_tag('c:firstSliceAng', [ ['val', 0] ])
173
+ @writer.empty_tag('c:firstSliceAng', [ ['val', @rotation] ])
160
174
  end
161
175
  end
162
176
  end
@@ -745,7 +745,9 @@ def write_font(writer, worksheet, dxf_format = nil) #:nodoc:
745
745
 
746
746
  writer.empty_tag('sz', [ ['val', size] ]) unless dxf_format
747
747
 
748
- if ptrue?(theme)
748
+ if theme == -1
749
+ # Ignore for excel2003_style
750
+ elsif ptrue?(theme)
749
751
  write_color(writer, 'theme', theme)
750
752
  elsif ptrue?(@color_indexed)
751
753
  write_color(writer, 'indexed', @color_indexed)
@@ -799,7 +801,9 @@ def write_font_shapes(writer)
799
801
  end
800
802
 
801
803
  def write_font_family_scheme(writer)
802
- writer.empty_tag('family', [ ['val', @font_family] ])
804
+ if ptrue?(@font_family)
805
+ writer.empty_tag('family', [ ['val', @font_family] ])
806
+ end
803
807
 
804
808
  if @font == 'Calibri' && !ptrue?(@hyperlink)
805
809
  writer.empty_tag('scheme', [ ['val', @font_scheme] ])
@@ -353,8 +353,11 @@ def layout_properties(args, is_text = false)
353
353
  properties = is_text ? [:x, :y] : [:x, :y, :width, :height]
354
354
 
355
355
  # Check for valid properties.
356
- allowable = Hash.new
357
- allowable[properties.size] = nil
356
+ args.keys.each do |key|
357
+ unless properties.include?(key.to_sym)
358
+ raise "Property '#{key}' not allowed in layout options\n"
359
+ end
360
+ end
358
361
 
359
362
  # Set the layout properties
360
363
  layout = Hash.new
@@ -559,6 +562,32 @@ def palette_color(index)
559
562
  rgb = @palette[idx]
560
563
  sprintf("%02X%02X%02X", *rgb)
561
564
  end
565
+
566
+ #
567
+ # Workbook の生成時のオプションハッシュを解析する
568
+ #
569
+ def process_workbook_options(*params)
570
+ case params.size
571
+ when 0
572
+ [{}, {}]
573
+ when 1 # one hash
574
+ options_keys = [:tempdir, :date_1904, :optimization, :excel2003_style]
575
+
576
+ hash = params.first
577
+ options = hash.reject{|k,v| !options_keys.include?(k)}
578
+
579
+ default_format_properties =
580
+ hash[:default_format_properties] ||
581
+ hash.reject{|k,v| options_keys.include?(k)}
582
+
583
+ [options, default_format_properties.dup]
584
+ when 2 # array which includes options and default_format_properties
585
+ options, default_format_properties = params
586
+ default_format_properties ||= {}
587
+
588
+ [options.dup, default_format_properties.dup]
589
+ end
590
+ end
562
591
  end
563
592
 
564
593
  module WriteDPtPoint
@@ -1,5 +1,5 @@
1
1
  require 'write_xlsx/workbook'
2
2
 
3
3
  class WriteXLSX < Writexlsx::Workbook
4
- VERSION = "0.77.2"
4
+ VERSION = "0.78.0"
5
5
  end
@@ -46,6 +46,7 @@ class Workbook
46
46
  attr_reader :image_types, :images # :nodoc:
47
47
  attr_reader :shared_strings # :nodoc:
48
48
  attr_reader :vba_project # :nodoc:
49
+ attr_reader :excel2003_style # :nodoc:
49
50
  #
50
51
  # A new Excel workbook is created using the +new+ constructor
51
52
  # which accepts either a filename or an IO object as a parameter.
@@ -87,12 +88,14 @@ class Workbook
87
88
  # formats = { :font => 'Arial', :size => 10.5 }
88
89
  # workbook = WriteXLSX.new('file.xlsx', formats)
89
90
  #
90
- def initialize(file, default_formats = {})
91
+ def initialize(file, *option_params)
92
+ options, default_formats = process_workbook_options(*option_params)
91
93
  @writer = Package::XMLWriterSimple.new
92
94
 
93
- @tempdir = File.join(Dir.tmpdir, Digest::MD5.hexdigest(Time.now.to_s))
95
+ @tempdir = options[:tempdir] ||
96
+ File.join(Dir.tmpdir, Digest::MD5.hexdigest(Time.now.to_s))
94
97
  setup_filename(file)
95
- @date_1904 = false
98
+ @date_1904 = options[:date_1904] || false
96
99
  @activesheet = 0
97
100
  @firstsheet = 0
98
101
  @selected = 0
@@ -110,12 +113,13 @@ def initialize(file, default_formats = {})
110
113
  @custom_colors = []
111
114
  @doc_properties = {}
112
115
  @local_time = Time.now
113
- @optimization = 0
116
+ @optimization = options[:optimization] || 0
114
117
  @x_window = 240
115
118
  @y_window = 15
116
119
  @window_width = 16095
117
120
  @window_height = 9660
118
121
  @tab_ratio = 500
122
+ @excel2003_style = options[:excel2003_style] || false
119
123
  @table_count = 0
120
124
  @image_types = {}
121
125
  @images = []
@@ -123,7 +127,17 @@ def initialize(file, default_formats = {})
123
127
  # Structures for the shared strings data.
124
128
  @shared_strings = Package::SharedStrings.new
125
129
 
126
- add_format(default_formats.merge(:xf_index => 0))
130
+ # Formula calculation default settings.
131
+ @calc_id = 124519
132
+ @calc_mode = 'auto'
133
+ @calc_on_load = true
134
+
135
+ if @excel2003_style
136
+ add_format(default_formats
137
+ .merge(:xf_index => 0, :font_family => 0, :font => 'Arial', :size => 10, :theme => -1))
138
+ else
139
+ add_format(default_formats.merge(:xf_index => 0))
140
+ end
127
141
  set_color_palette
128
142
  end
129
143
 
@@ -404,7 +418,13 @@ def add_chart(params = {})
404
418
  # See the {Format Class's rdoc}[Format.html] for more details about
405
419
  # Format properties and how to set them.
406
420
  #
407
- def add_format(properties = {})
421
+ def add_format(property_hash = {})
422
+ properties = {}
423
+ if @excel2003_style
424
+ properties.update(:font => 'Arial', :size => 10, :theme => -1)
425
+ end
426
+ properties.update(property_hash)
427
+
408
428
  format = Format.new(@formats, properties)
409
429
 
410
430
  @formats.formats.push(format) # Store format reference
@@ -813,6 +833,23 @@ def add_vba_project(vba_project)
813
833
  @vba_project = vba_project
814
834
  end
815
835
 
836
+ #
837
+ # set_calc_mode()
838
+ #
839
+ # Set the Excel caclcuation mode for the workbook.
840
+ #
841
+ def set_calc_mode(mode, calc_id = nil)
842
+ @calc_mode = mode || 'auto'
843
+
844
+ if mode == 'manual'
845
+ @calc_on_load = false
846
+ elsif mode == 'auto_except_tables'
847
+ @calc_mode = 'autoNoTable'
848
+ end
849
+
850
+ @calc_id = calc_id if calc_id
851
+ end
852
+
816
853
  #
817
854
  # Change the RGB components of the elements in the colour palette.
818
855
  #
@@ -1139,10 +1176,18 @@ def write_workbook_view #:nodoc:
1139
1176
  end
1140
1177
 
1141
1178
  def write_calc_pr #:nodoc:
1142
- attributes = [
1143
- ['calcId', 124519],
1144
- ['fullCalcOnLoad', 1]
1145
- ]
1179
+ attributes = [ ['calcId', @calc_id] ]
1180
+
1181
+ case @calc_mode
1182
+ when 'manual'
1183
+ attributes << ['calcMode', 'manual']
1184
+ attributes << ['calcOnSave', 0]
1185
+ when 'autoNoTable'
1186
+ attributes << ['calcMode', 'autoNoTable']
1187
+ end
1188
+
1189
+ attributes << ['fullCalcOnLoad', 1] if @calc_on_load
1190
+
1146
1191
  @writer.empty_tag('calcPr', attributes)
1147
1192
  end
1148
1193