write_xlsx 0.61.0 → 0.62.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.rdoc CHANGED
@@ -1,6 +1,6 @@
1
1
  = write_xlsx
2
2
 
3
- gem to create a new file in the Excel 2007+ XLSX format, and you can use the same interface as writeexcel gem. write_xlsx is converted from Perl's module Excel::Writer::XLSX-0.60, https://github.com/jmcnamara/excel-writer-xlsx .
3
+ gem to create a new file in the Excel 2007+ XLSX format, and you can use the same interface as writeexcel gem. write_xlsx is converted from Perl's module Excel::Writer::XLSX-0.62, https://github.com/jmcnamara/excel-writer-xlsx .
4
4
 
5
5
  == Description
6
6
 
@@ -75,6 +75,10 @@ the first worksheet in an Excel XML spreadsheet called ruby.xlsx:
75
75
  workbook.close
76
76
 
77
77
  == Recent change
78
+ 2013-02-24 v0.62.0
79
+ Added option for adding a data table to a Chart X-axis.
80
+ See output from chart_data_table.rb example.
81
+
78
82
  2013-02-23 v0.61.0
79
83
  Allow a cell url string to be over written with a number or formula
80
84
  using a second write() call to the same cell. The url remains intact.
@@ -0,0 +1,94 @@
1
+ #!/usr/bin/env ruby
2
+ # -*- coding: utf-8 -*-
3
+
4
+ #######################################################################
5
+ #
6
+ # A demo of an Column chart with a data table on the X-axis using
7
+ # WriteXLSX.
8
+ #
9
+ # reverse ('(c)'), December 2012, John McNamara, jmcnamara@cpan.org
10
+ # convert to ruby by Hideo NAKAMURA, cxn03651@msj.biglobe.ne.jp
11
+ #
12
+
13
+ require 'rubygems'
14
+ require 'write_xlsx'
15
+
16
+ workbook = WriteXLSX.new('chart_data_table.xlsx')
17
+ worksheet = workbook.add_worksheet
18
+ bold = workbook.add_format(:bold => 1)
19
+
20
+ # Add the worksheet data that the charts will refer to.
21
+ headings = [ 'Number', 'Batch 1', 'Batch 2' ]
22
+ data = [
23
+ [ 2, 3, 4, 5, 6, 7 ],
24
+ [ 10, 40, 50, 20, 10, 50 ],
25
+ [ 30, 60, 70, 50, 40, 30 ]
26
+ ]
27
+
28
+ worksheet.write('A1', headings, bold)
29
+ worksheet.write('A2', data)
30
+
31
+ # Create a new column chart with a data table.
32
+ chart1 = workbook.add_chart(:type => 'column', :embedded => 1)
33
+
34
+ # Configure the first series.
35
+ chart1.add_series(
36
+ :name => '=Sheet1!$B$1',
37
+ :categories => '=Sheet1!$A$2:$A$7',
38
+ :values => '=Sheet1!$B$2:$B$7'
39
+ )
40
+
41
+ # Configure second series. Note alternative use of array ref to define
42
+ # ranges: [ sheetname, row_start, row_end, col_start, col_end ].
43
+ chart1.add_series(
44
+ :name => '=Sheet1!$C$1',
45
+ :categories => [ 'Sheet1', 1, 6, 0, 0 ],
46
+ :values => [ 'Sheet1', 1, 6, 2, 2 ]
47
+ )
48
+
49
+ # Add a chart title and some axis labels.
50
+ chart1.set_title(:name => 'Chart with Data Table')
51
+ chart1.set_x_axis(:name => 'Test number')
52
+ chart1.set_y_axis(:name => 'Sample length (mm)')
53
+
54
+ # Set a default data table on the X-Axis.
55
+ chart1.set_table
56
+
57
+ # Insert the chart into the worksheet (with an offset).
58
+ worksheet.insert_chart('D2', chart1, 25, 10)
59
+
60
+ #
61
+ # Create a second charat.
62
+ #
63
+ chart2 = workbook.add_chart(:type => 'column', :embedded => 1)
64
+
65
+ # Configure the first series.
66
+ chart2.add_series(
67
+ :name => '=Sheet1!$B$1',
68
+ :categories => '=Sheet1!$A$2:$A$7',
69
+ :values => '=Sheet1!$B$2:$B$7'
70
+ )
71
+
72
+ # Configure second series. Note alternative use of array ref to define
73
+ # ranges: [ sheetname, row_start, row_end, col_start, col_end ].
74
+ chart2.add_series(
75
+ :name => '=Sheet1!$C$1',
76
+ :categories => [ 'Sheet1', 1, 6, 0, 0 ],
77
+ :values => [ 'Sheet1', 1, 6, 2, 2 ]
78
+ )
79
+
80
+ # Add a chart title and some axis labels.
81
+ chart2.set_title(:name => 'Data Table with legend keys')
82
+ chart2.set_x_axis(:name => 'Test number')
83
+ chart2.set_y_axis(:name => 'Sample length (mm)')
84
+
85
+ # Set a default data table on the X-Axis with the legend keys shown.
86
+ chart2.set_table(:show_keys => true)
87
+
88
+ # Hide the chart legend since the keys are show on the data table.
89
+ chart2.set_legend(:position => 'none')
90
+
91
+ # Insert the chart into the worksheet (with an offset).
92
+ worksheet.insert_chart('D18', chart2, 25, 11)
93
+
94
+ workbook.close
@@ -3,6 +3,35 @@
3
3
  require 'write_xlsx/utility'
4
4
 
5
5
  module Writexlsx
6
+ class Table
7
+ include Writexlsx::Utility
8
+
9
+ attr_reader :horizontal, :vertical, :outline, :show_keys
10
+
11
+ def initialize(params = {})
12
+ @horizontal, @vertical, @outline, @show_keys = true, true, true, false
13
+ @horizontal = params[:horizontal] if params.has_key?(:horizontal)
14
+ @vertical = params[:vertical] if params.has_key?(:vertical)
15
+ @outline = params[:outline] if params.has_key?(:outline)
16
+ @show_keys = params[:show_keys] if params.has_key?(:show_keys)
17
+ end
18
+
19
+ def write_d_table(writer)
20
+ writer.tag_elements('c:dTable') do
21
+ writer.empty_tag('c:showHorzBorder', attributes) if ptrue?(horizontal)
22
+ writer.empty_tag('c:showVertBorder', attributes) if ptrue?(vertical)
23
+ writer.empty_tag('c:showOutline', attributes) if ptrue?(outline)
24
+ writer.empty_tag('c:showKeys', attributes) if ptrue?(show_keys)
25
+ end
26
+ end
27
+
28
+ private
29
+
30
+ def attributes
31
+ ['val', 1]
32
+ end
33
+ end
34
+
6
35
  # ==SYNOPSIS
7
36
  #
8
37
  # To create a simple Excel file with a chart using WriteXLSX:
@@ -512,6 +541,7 @@ def initialize(subtype) # :nodoc:
512
541
  @y_scale = 1
513
542
  @x_offset = 0
514
543
  @y_offset = 0
544
+ @table = nil
515
545
 
516
546
  set_default_properties
517
547
  end
@@ -975,7 +1005,7 @@ def show_hidden_data
975
1005
  #
976
1006
  # Set dimensions for scale for the chart.
977
1007
  #
978
- def size(params = {})
1008
+ def set_size(params = {})
979
1009
  @width = params[:width] if params[:width]
980
1010
  @height = params[:height] if params[:height]
981
1011
  @x_scale = params[:x_scale] if params[:x_scale]
@@ -984,6 +1014,28 @@ def size(params = {})
984
1014
  @y_offset = params[:y_offset] if params[:y_offset]
985
1015
  end
986
1016
 
1017
+ # Backward compatibility with poorly chosen method name.
1018
+ alias :size :set_size
1019
+
1020
+ #
1021
+ # The set_table method adds a data table below the horizontal axis with the
1022
+ # data used to plot the chart.
1023
+ #
1024
+ # chart.set_table
1025
+ #
1026
+ # The available options, with default values are:
1027
+ #
1028
+ # :vertical => true # Display vertical lines in the table.
1029
+ # :horizontal => true # Display horizontal lines in the table.
1030
+ # :outline => true # Display an outline in the table.
1031
+ # :show_keys => false # Show the legend keys with the table data.
1032
+ #
1033
+ # The data table can only be shown with Bar, Column, Line, Area and Stock charts.
1034
+ #
1035
+ def set_table(params = {})
1036
+ @table = Table.new(params)
1037
+ end
1038
+
987
1039
  #
988
1040
  # Setup the default configuration data for an embedded chart.
989
1041
  #
@@ -1753,6 +1805,9 @@ def write_plot_area_base(type = nil) # :nodoc:
1753
1805
  write_val_axis(params)
1754
1806
  write_cat_or_date_axis(params, type)
1755
1807
 
1808
+ # Write the c:dTable element.
1809
+ write_d_table
1810
+
1756
1811
  # Write the c:spPr element for the plotarea formatting.
1757
1812
  write_sp_pr(@plotarea)
1758
1813
  end
@@ -3436,6 +3491,13 @@ def write_a_latin(args) # :nodoc:
3436
3491
  @writer.empty_tag('a:latin', args)
3437
3492
  end
3438
3493
 
3494
+ #
3495
+ # Write the <c:dTable> element.
3496
+ #
3497
+ def write_d_table
3498
+ @table.write_d_table(@writer) if @table
3499
+ end
3500
+
3439
3501
  def nil_or_max?(val) # :nodoc:
3440
3502
  val.nil? || val == 'max'
3441
3503
  end
@@ -1,5 +1,5 @@
1
1
  require 'write_xlsx/workbook'
2
2
 
3
3
  class WriteXLSX < Writexlsx::Workbook
4
- VERSION = "0.61.0"
4
+ VERSION = "0.62.0"
5
5
  end
@@ -2802,7 +2802,7 @@ def insert_chart(*args)
2802
2802
  raise "Not a Chart object in insert_chart()" unless chart.is_a?(Chart) || chart.is_a?(Chartsheet)
2803
2803
  raise "Not a embedded style Chart object in insert_chart()" if chart.respond_to?(:embedded) && chart.embedded == 0
2804
2804
 
2805
- # Use the values set with chart.size, if any.
2805
+ # Use the values set with chart.set_size, if any.
2806
2806
  x_scale = chart.x_scale if chart.x_scale != 1
2807
2807
  y_scale = chart.y_scale if chart.y_scale != 1
2808
2808
  x_offset = chart.x_offset if ptrue?(chart.x_offset)
@@ -31,7 +31,7 @@ def test_chart_size01
31
31
  chart.add_series(:values => '=Sheet1!$B$1:$B$5')
32
32
  chart.add_series(:values => '=Sheet1!$C$1:$C$5')
33
33
 
34
- chart.size(:width => 512, :height => 320)
34
+ chart.set_size(:width => 512, :height => 320)
35
35
 
36
36
  worksheet.insert_chart('E9', chart)
37
37
 
@@ -31,7 +31,7 @@ def test_chart_size02
31
31
  chart.add_series(:values => '=Sheet1!$B$1:$B$5')
32
32
  chart.add_series(:values => '=Sheet1!$C$1:$C$5')
33
33
 
34
- chart.size(:x_scale => 1.066666666, :y_scale => 1.11111111)
34
+ chart.set_size(:x_scale => 1.066666666, :y_scale => 1.11111111)
35
35
 
36
36
  worksheet.insert_chart('E9', chart)
37
37
 
@@ -0,0 +1,45 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'helper'
3
+
4
+ class TestChartTable01 < Test::Unit::TestCase
5
+ def setup
6
+ setup_dir_var
7
+ end
8
+
9
+ def teardown
10
+ File.delete(@xlsx) if File.exist?(@xlsx)
11
+ end
12
+
13
+ def test_chart_table01
14
+ @xlsx = 'chart_table01.xlsx'
15
+ workbook = WriteXLSX.new(@xlsx)
16
+ worksheet = workbook.add_worksheet
17
+ chart = workbook.add_chart(:type => 'column', :embedded => 1)
18
+
19
+ # For testing, copy the randomly generated axis ids in the target xls file.
20
+ chart.instance_variable_set(:@axis_ids, [61355520, 61357056])
21
+
22
+ data = [
23
+ [ 1, 2, 3, 4, 5 ],
24
+ [ 2, 4, 6, 8, 10 ],
25
+ [ 3, 6, 9, 12, 15 ]
26
+ ]
27
+
28
+ worksheet.write('A1', data)
29
+
30
+ chart.add_series(:values => '=Sheet1!$A$1:$A$5')
31
+ chart.add_series(:values => '=Sheet1!$B$1:$B$5')
32
+ chart.add_series(:values => '=Sheet1!$C$1:$C$5')
33
+
34
+ # Test the default table.
35
+ chart.set_table
36
+
37
+ worksheet.insert_chart('E9', chart)
38
+
39
+ workbook.close
40
+ compare_xlsx_for_regression(
41
+ File.join(@regression_output, @xlsx),
42
+ @xlsx
43
+ )
44
+ end
45
+ end
@@ -0,0 +1,50 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'helper'
3
+
4
+ class TestChartTable02 < Test::Unit::TestCase
5
+ def setup
6
+ setup_dir_var
7
+ end
8
+
9
+ def teardown
10
+ File.delete(@xlsx) if File.exist?(@xlsx)
11
+ end
12
+
13
+ def test_chart_table02
14
+ @xlsx = 'chart_table02.xlsx'
15
+ workbook = WriteXLSX.new(@xlsx)
16
+ worksheet = workbook.add_worksheet
17
+ chart = workbook.add_chart(:type => 'column', :embedded => 1)
18
+
19
+ # For testing, copy the randomly generated axis ids in the target xls file.
20
+ chart.instance_variable_set(:@axis_ids, [61354368, 61355904])
21
+
22
+ data = [
23
+ [ 1, 2, 3, 4, 5 ],
24
+ [ 2, 4, 6, 8, 10 ],
25
+ [ 3, 6, 9, 12, 15 ]
26
+ ]
27
+
28
+ worksheet.write('A1', data)
29
+
30
+ chart.add_series(:values => '=Sheet1!$A$1:$A$5')
31
+ chart.add_series(:values => '=Sheet1!$B$1:$B$5')
32
+ chart.add_series(:values => '=Sheet1!$C$1:$C$5')
33
+
34
+ # Test the default table.
35
+ chart.set_table(
36
+ :vertical => false,
37
+ :horizontal => false,
38
+ :outline => false,
39
+ :show_keys => true
40
+ )
41
+
42
+ worksheet.insert_chart('E9', chart)
43
+
44
+ workbook.close
45
+ compare_xlsx_for_regression(
46
+ File.join(@regression_output, @xlsx),
47
+ @xlsx
48
+ )
49
+ end
50
+ end
@@ -4881,4 +4881,88 @@ def test_hide_row_col
4881
4881
  workbook.close
4882
4882
  compare_xlsx(File.join(@perl_output, @xlsx), @xlsx)
4883
4883
  end
4884
+
4885
+ def test_chart_data_table
4886
+ @xlsx = 'chart_data_table.xlsx'
4887
+ workbook = WriteXLSX.new(@xlsx)
4888
+ worksheet = workbook.add_worksheet
4889
+ bold = workbook.add_format(:bold => 1)
4890
+
4891
+ # Add the worksheet data that the charts will refer to.
4892
+ headings = [ 'Number', 'Batch 1', 'Batch 2' ]
4893
+ data = [
4894
+ [ 2, 3, 4, 5, 6, 7 ],
4895
+ [ 10, 40, 50, 20, 10, 50 ],
4896
+ [ 30, 60, 70, 50, 40, 30 ]
4897
+ ]
4898
+
4899
+ worksheet.write('A1', headings, bold)
4900
+ worksheet.write('A2', data)
4901
+
4902
+ # Create a new column chart with a data table.
4903
+ chart1 = workbook.add_chart(:type => 'column', :embedded => 1)
4904
+
4905
+ # Configure the first series.
4906
+ chart1.add_series(
4907
+ :name => '=Sheet1!$B$1',
4908
+ :categories => '=Sheet1!$A$2:$A$7',
4909
+ :values => '=Sheet1!$B$2:$B$7'
4910
+ )
4911
+
4912
+ # Configure second series. Note alternative use of array ref to define
4913
+ # ranges: [ sheetname, row_start, row_end, col_start, col_end ].
4914
+ chart1.add_series(
4915
+ :name => '=Sheet1!$C$1',
4916
+ :categories => [ 'Sheet1', 1, 6, 0, 0 ],
4917
+ :values => [ 'Sheet1', 1, 6, 2, 2 ]
4918
+ )
4919
+
4920
+ # Add a chart title and some axis labels.
4921
+ chart1.set_title(:name => 'Chart with Data Table')
4922
+ chart1.set_x_axis(:name => 'Test number')
4923
+ chart1.set_y_axis(:name => 'Sample length (mm)')
4924
+
4925
+ # Set a default data table on the X-Axis.
4926
+ chart1.set_table
4927
+
4928
+ # Insert the chart into the worksheet (with an offset).
4929
+ worksheet.insert_chart('D2', chart1, 25, 10)
4930
+
4931
+ #
4932
+ # Create a second charat.
4933
+ #
4934
+ chart2 = workbook.add_chart(:type => 'column', :embedded => 1)
4935
+
4936
+ # Configure the first series.
4937
+ chart2.add_series(
4938
+ :name => '=Sheet1!$B$1',
4939
+ :categories => '=Sheet1!$A$2:$A$7',
4940
+ :values => '=Sheet1!$B$2:$B$7'
4941
+ )
4942
+
4943
+ # Configure second series. Note alternative use of array ref to define
4944
+ # ranges: [ sheetname, row_start, row_end, col_start, col_end ].
4945
+ chart2.add_series(
4946
+ :name => '=Sheet1!$C$1',
4947
+ :categories => [ 'Sheet1', 1, 6, 0, 0 ],
4948
+ :values => [ 'Sheet1', 1, 6, 2, 2 ]
4949
+ )
4950
+
4951
+ # Add a chart title and some axis labels.
4952
+ chart2.set_title(:name => 'Data Table with legend keys')
4953
+ chart2.set_x_axis(:name => 'Test number')
4954
+ chart2.set_y_axis(:name => 'Sample length (mm)')
4955
+
4956
+ # Set a default data table on the X-Axis with the legend keys shown.
4957
+ chart2.set_table(:show_keys => true)
4958
+
4959
+ # Hide the chart legend since the keys are show on the data table.
4960
+ chart2.set_legend(:position => 'none')
4961
+
4962
+ # Insert the chart into the worksheet (with an offset).
4963
+ worksheet.insert_chart('D18', chart2, 25, 11)
4964
+
4965
+ workbook.close
4966
+ compare_xlsx(File.join(@perl_output, @xlsx), @xlsx)
4967
+ end
4884
4968
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: write_xlsx
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.61.0
4
+ version: 0.62.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -61,6 +61,7 @@ files:
61
61
  - examples/chart_area.rb
62
62
  - examples/chart_bar.rb
63
63
  - examples/chart_column.rb
64
+ - examples/chart_data_table.rb
64
65
  - examples/chart_line.rb
65
66
  - examples/chart_pie.rb
66
67
  - examples/chart_radar.rb
@@ -262,6 +263,7 @@ files:
262
263
  - test/perl_output/chart_area.xlsx
263
264
  - test/perl_output/chart_bar.xlsx
264
265
  - test/perl_output/chart_column.xlsx
266
+ - test/perl_output/chart_data_table.xlsx
265
267
  - test/perl_output/chart_line.xlsx
266
268
  - test/perl_output/chart_pie.xlsx
267
269
  - test/perl_output/chart_radar.xlsx
@@ -474,6 +476,8 @@ files:
474
476
  - test/regression/test_chart_stock02.rb
475
477
  - test/regression/test_chart_str01.rb
476
478
  - test/regression/test_chart_str02.rb
479
+ - test/regression/test_chart_table01.rb
480
+ - test/regression/test_chart_table02.rb
477
481
  - test/regression/test_chartsheet01.rb
478
482
  - test/regression/test_chartsheet02.rb
479
483
  - test/regression/test_chartsheet03.rb
@@ -789,6 +793,8 @@ files:
789
793
  - test/regression/xlsx_files/chart_stock02.xlsx
790
794
  - test/regression/xlsx_files/chart_str01.xlsx
791
795
  - test/regression/xlsx_files/chart_str02.xlsx
796
+ - test/regression/xlsx_files/chart_table01.xlsx
797
+ - test/regression/xlsx_files/chart_table02.xlsx
792
798
  - test/regression/xlsx_files/chartsheet01.xlsx
793
799
  - test/regression/xlsx_files/chartsheet02.xlsx
794
800
  - test/regression/xlsx_files/chartsheet03.xlsx
@@ -1205,6 +1211,7 @@ test_files:
1205
1211
  - test/perl_output/chart_area.xlsx
1206
1212
  - test/perl_output/chart_bar.xlsx
1207
1213
  - test/perl_output/chart_column.xlsx
1214
+ - test/perl_output/chart_data_table.xlsx
1208
1215
  - test/perl_output/chart_line.xlsx
1209
1216
  - test/perl_output/chart_pie.xlsx
1210
1217
  - test/perl_output/chart_radar.xlsx
@@ -1417,6 +1424,8 @@ test_files:
1417
1424
  - test/regression/test_chart_stock02.rb
1418
1425
  - test/regression/test_chart_str01.rb
1419
1426
  - test/regression/test_chart_str02.rb
1427
+ - test/regression/test_chart_table01.rb
1428
+ - test/regression/test_chart_table02.rb
1420
1429
  - test/regression/test_chartsheet01.rb
1421
1430
  - test/regression/test_chartsheet02.rb
1422
1431
  - test/regression/test_chartsheet03.rb
@@ -1732,6 +1741,8 @@ test_files:
1732
1741
  - test/regression/xlsx_files/chart_stock02.xlsx
1733
1742
  - test/regression/xlsx_files/chart_str01.xlsx
1734
1743
  - test/regression/xlsx_files/chart_str02.xlsx
1744
+ - test/regression/xlsx_files/chart_table01.xlsx
1745
+ - test/regression/xlsx_files/chart_table02.xlsx
1735
1746
  - test/regression/xlsx_files/chartsheet01.xlsx
1736
1747
  - test/regression/xlsx_files/chartsheet02.xlsx
1737
1748
  - test/regression/xlsx_files/chartsheet03.xlsx