write_xlsx 0.77.2 → 0.78.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.
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
@@ -352,7 +352,10 @@ def initialize(workbook, index, name) #:nodoc:
352
352
  @outline_row_level = 0
353
353
  @outline_col_level = 0
354
354
 
355
+ @original_row_height = 15
355
356
  @default_row_height = 15
357
+ @default_row_pixels = 20
358
+ @default_col_pixels = 64
356
359
  @default_row_rezoed = 0
357
360
 
358
361
  @merge = []
@@ -365,6 +368,17 @@ def initialize(workbook, index, name) #:nodoc:
365
368
 
366
369
  @cond_formats = {}
367
370
  @dxf_priority = 1
371
+
372
+ if excel2003_style?
373
+ @original_row_height = 12.75
374
+ @default_row_height = 12.75
375
+ @default_row_pixels = 17
376
+ self::margins_left_right = 0.75
377
+ self::margins_top_bottom = 1
378
+ @page_setup.margin_header = 0.5
379
+ @page_setup.margin_footer = 0.5
380
+ @page_setup.header_footer_aligns = 1
381
+ end
368
382
  end
369
383
 
370
384
  def set_xml_writer(filename) #:nodoc:
@@ -381,6 +395,8 @@ def assemble_xml_file #:nodoc:
381
395
  write_cols
382
396
  write_sheet_data
383
397
  write_sheet_protection
398
+ # write_sheet_calc_pr
399
+ write_phonetic_pr if excel2003_style?
384
400
  write_auto_filter
385
401
  write_merge_cells
386
402
  write_conditional_formats
@@ -3087,10 +3103,10 @@ def set_row(*args)
3087
3103
  # Set the default row properties
3088
3104
  #
3089
3105
  def set_default_row(height = nil, zero_height = nil)
3090
- height ||= 15
3106
+ height ||= @original_row_height
3091
3107
  zero_height ||= 0
3092
3108
 
3093
- if height != 15
3109
+ if height != @original_row_height
3094
3110
  @default_row_height = height
3095
3111
 
3096
3112
  # Store the row change to allow optimisations.
@@ -5601,7 +5617,7 @@ def position_object_pixels(col_start, row_start, x1, y1, width, height) #:nodoc:
5601
5617
  x_abs = (0 .. col_start-1).inject(0) {|sum, col| sum += size_col(col)}
5602
5618
  else
5603
5619
  # Optimisation for when the column widths haven't changed.
5604
- x_abs = 64 * col_start
5620
+ x_abs = @default_col_pixels * col_start
5605
5621
  end
5606
5622
  x_abs += x1
5607
5623
 
@@ -5611,7 +5627,7 @@ def position_object_pixels(col_start, row_start, x1, y1, width, height) #:nodoc:
5611
5627
  y_abs = (0 .. row_start-1).inject(0) {|sum, row| sum += size_row(row)}
5612
5628
  else
5613
5629
  # Optimisation for when the row heights haven't changed.
5614
- y_abs = 20 * row_start
5630
+ y_abs = @default_row_pixels * row_start
5615
5631
  end
5616
5632
  y_abs += y1
5617
5633
 
@@ -5681,6 +5697,10 @@ def date_1904? #:nodoc:
5681
5697
  @workbook.date_1904?
5682
5698
  end
5683
5699
 
5700
+ def excel2003_style? # :nodoc:
5701
+ @workbook.excel2003_style
5702
+ end
5703
+
5684
5704
  #
5685
5705
  # Convert from an Excel internal colour index to a XML style #RRGGBB index
5686
5706
  # based on the default or user defined values in the Workbook palette.
@@ -6087,7 +6107,7 @@ def size_col(col) #:nodoc:
6087
6107
  pixels = (width * MAX_DIGIT_WIDTH + 0.5).to_i + PADDING
6088
6108
  end
6089
6109
  else
6090
- pixels = 64
6110
+ pixels = @default_col_pixels
6091
6111
  end
6092
6112
  pixels
6093
6113
  end
@@ -6279,8 +6299,8 @@ def button_params(row, col, params)
6279
6299
  end
6280
6300
 
6281
6301
  # Ensure that a width and height have been set.
6282
- default_width = 64
6283
- default_height = 20
6302
+ default_width = @default_col_pixels
6303
+ default_height = @default_row_pixels
6284
6304
  params[:width] = default_width if !params[:width]
6285
6305
  params[:height] = default_height if !params[:height]
6286
6306
 
@@ -6498,7 +6518,7 @@ def write_sheet_format_pr #:nodoc:
6498
6518
  attributes = [
6499
6519
  ['defaultRowHeight', @default_row_height]
6500
6520
  ]
6501
- if @default_row_height != 15
6521
+ if @default_row_height != @original_row_height
6502
6522
  attributes << ['customHeight', 1]
6503
6523
  end
6504
6524
 
@@ -6651,9 +6671,9 @@ def row_attributes(args)
6651
6671
  attributes << ['spans', spans] if spans
6652
6672
  attributes << ['s', xf_index] if ptrue?(xf_index)
6653
6673
  attributes << ['customFormat', 1] if ptrue?(format)
6654
- attributes << ['ht', height] if height != 15
6674
+ attributes << ['ht', height] if height != @original_row_height
6655
6675
  attributes << ['hidden', 1] if ptrue?(hidden)
6656
- attributes << ['customHeight', 1] if height != 15
6676
+ attributes << ['customHeight', 1] if height != @original_row_height
6657
6677
  attributes << ['outlineLevel', level] if ptrue?(level)
6658
6678
  attributes << ['collapsed', 1] if ptrue?(collapsed)
6659
6679
 
@@ -6794,7 +6814,7 @@ def write_sheet_calc_pr #:nodoc:
6794
6814
  #
6795
6815
  def write_phonetic_pr #:nodoc:
6796
6816
  attributes = [
6797
- ['fontId', 1],
6817
+ ['fontId', 0],
6798
6818
  ['type', 'noConversion']
6799
6819
  ]
6800
6820
 
@@ -6856,7 +6876,7 @@ def write_print_options #:nodoc:
6856
6876
  # Write the <headerFooter> element.
6857
6877
  #
6858
6878
  def write_header_footer #:nodoc:
6859
- @page_setup.write_header_footer(@writer)
6879
+ @page_setup.write_header_footer(@writer, excel2003_style?)
6860
6880
  end
6861
6881
 
6862
6882
  #
@@ -12,7 +12,7 @@ class PageSetup # :nodoc:
12
12
  attr_accessor :fit_page, :fit_width, :fit_height, :page_setup_changed # :nodoc:
13
13
  attr_writer :across # :nodoc:
14
14
  attr_accessor :orientation, :print_options_changed # :nodoc:
15
- attr_accessor :header, :footer, :header_footer_changed
15
+ attr_accessor :header, :footer, :header_footer_changed, :header_footer_aligns
16
16
  attr_writer :page_start
17
17
 
18
18
  def initialize # :nodoc:
@@ -136,13 +136,20 @@ def write_print_options(writer) #:nodoc:
136
136
  #
137
137
  # Write the <headerFooter> element.
138
138
  #
139
- def write_header_footer(writer) #:nodoc:
140
- return unless @header_footer_changed
141
-
142
- writer.tag_elements('headerFooter') do
143
- write_odd_header(writer) if @header && @header != ''
144
- write_odd_footer(writer) if @footer && @footer != ''
139
+ def write_header_footer(writer, excel2003_style) #:nodoc:
140
+ tag = 'headerFooter'
141
+ attributes = []
142
+ attributes << ['alignWithMargins', 0] if @header_footer_aligns
143
+
144
+ if @header_footer_changed
145
+ writer.tag_elements(tag, attributes) do
146
+ write_odd_header(writer) if @header && @header != ''
147
+ write_odd_footer(writer) if @footer && @footer != ''
148
+ end
149
+ elsif excel2003_style
150
+ writer.empty_tag(tag, attributes)
145
151
  end
152
+
146
153
  end
147
154
 
148
155
  private
@@ -0,0 +1,43 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'helper'
3
+
4
+ class TestRegressionChartColumn09 < 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_column09
14
+ @xlsx = 'chart_column09.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 xlsx file.
20
+ chart.instance_variable_set(:@axis_ids, [47400832, 61387136 ])
21
+
22
+ data = [
23
+ [ 1, 2, 3, 4, 5 ],
24
+ [ 1, 2, 3, 2, 1 ]
25
+ ]
26
+
27
+ worksheet.write('A1', data)
28
+
29
+ chart.add_series(
30
+ :categories => 'Sheet1!$A$1:$A$5',
31
+ :values => 'Sheet1!$B$1:$B$5'
32
+ )
33
+
34
+ worksheet.insert_chart('E9', chart)
35
+
36
+ workbook.close
37
+ compare_xlsx_for_regression(File.join(@regression_output, @xlsx),
38
+ @xlsx,
39
+ nil,
40
+ nil
41
+ )
42
+ end
43
+ end
@@ -0,0 +1,43 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'helper'
3
+
4
+ class TestRegressionChartColumn10 < 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_column10
14
+ @xlsx = 'chart_column10.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 xlsx file.
20
+ chart.instance_variable_set(:@axis_ids, [45686144, 45722240 ])
21
+
22
+ data = [
23
+ [ 'A', 'B', 'C', 'D', 'E' ],
24
+ [ 1, 2, 3, 2, 1 ]
25
+ ]
26
+
27
+ worksheet.write('A1', data)
28
+
29
+ chart.add_series(
30
+ :categories => 'Sheet1!$A$1:$A$5',
31
+ :values => 'Sheet1!$B$1:$B$5'
32
+ )
33
+
34
+ worksheet.insert_chart('E9', chart)
35
+
36
+ workbook.close
37
+ compare_xlsx_for_regression(File.join(@regression_output, @xlsx),
38
+ @xlsx,
39
+ nil,
40
+ nil
41
+ )
42
+ end
43
+ end
@@ -0,0 +1,43 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'helper'
3
+
4
+ class TestRegressionChartDoughnut01 < 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_doughnut01
14
+ @xlsx = 'chart_doughnut01.xlsx'
15
+ workbook = WriteXLSX.new(@xlsx)
16
+ worksheet = workbook.add_worksheet
17
+ chart = workbook.add_chart(:type => 'doughnut', :embedded => 1)
18
+
19
+ # For testing, copy the randomly generated axis ids in the target xlsx file.
20
+ chart.instance_variable_set(:@axis_ids, [45686144, 45722240 ])
21
+
22
+ data = [
23
+ [ 2, 4, 6 ],
24
+ [ 60, 30, 10 ]
25
+ ]
26
+
27
+ worksheet.write('A1', data)
28
+
29
+ chart.add_series(
30
+ :categories => 'Sheet1!$A$1:$A$3',
31
+ :values => 'Sheet1!$B$1:$B$3'
32
+ )
33
+
34
+ worksheet.insert_chart('E9', chart)
35
+
36
+ workbook.close
37
+ compare_xlsx_for_regression(File.join(@regression_output, @xlsx),
38
+ @xlsx,
39
+ nil,
40
+ nil
41
+ )
42
+ end
43
+ end
@@ -0,0 +1,42 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'helper'
3
+
4
+ class TestRegressionChartDoughnut02 < 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_doughnut02
14
+ @xlsx = 'chart_doughnut02.xlsx'
15
+ workbook = WriteXLSX.new(@xlsx)
16
+ worksheet = workbook.add_worksheet
17
+ chart = workbook.add_chart(:type => 'doughnut', :embedded => 1)
18
+
19
+ data = [
20
+ [ 2, 4, 6 ],
21
+ [ 60, 30, 10 ]
22
+ ]
23
+
24
+ worksheet.write('A1', data)
25
+
26
+ chart.add_series(
27
+ :categories => 'Sheet1!$A$1:$A$3',
28
+ :values => 'Sheet1!$B$1:$B$3'
29
+ )
30
+
31
+ chart.set_hole_size(10)
32
+
33
+ worksheet.insert_chart('E9', chart)
34
+
35
+ workbook.close
36
+ compare_xlsx_for_regression(File.join(@regression_output, @xlsx),
37
+ @xlsx,
38
+ nil,
39
+ nil
40
+ )
41
+ end
42
+ end
@@ -0,0 +1,42 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'helper'
3
+
4
+ class TestRegressionChartDoughnut03 < 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_doughnut03
14
+ @xlsx = 'chart_doughnut03.xlsx'
15
+ workbook = WriteXLSX.new(@xlsx)
16
+ worksheet = workbook.add_worksheet
17
+ chart = workbook.add_chart(:type => 'doughnut', :embedded => 1)
18
+
19
+ data = [
20
+ [ 2, 4, 6 ],
21
+ [ 60, 30, 10 ]
22
+ ]
23
+
24
+ worksheet.write('A1', data)
25
+
26
+ chart.add_series(
27
+ :categories => 'Sheet1!$A$1:$A$3',
28
+ :values => 'Sheet1!$B$1:$B$3'
29
+ )
30
+
31
+ chart.set_hole_size(90)
32
+
33
+ worksheet.insert_chart('E9', chart)
34
+
35
+ workbook.close
36
+ compare_xlsx_for_regression(File.join(@regression_output, @xlsx),
37
+ @xlsx,
38
+ nil,
39
+ nil
40
+ )
41
+ end
42
+ end
@@ -0,0 +1,42 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'helper'
3
+
4
+ class TestRegressionChartDoughnut04 < 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_doughnut04
14
+ @xlsx = 'chart_doughnut04.xlsx'
15
+ workbook = WriteXLSX.new(@xlsx)
16
+ worksheet = workbook.add_worksheet
17
+ chart = workbook.add_chart(:type => 'doughnut', :embedded => 1)
18
+
19
+ data = [
20
+ [ 2, 4, 6 ],
21
+ [ 60, 30, 10 ]
22
+ ]
23
+
24
+ worksheet.write('A1', data)
25
+
26
+ chart.add_series(
27
+ :categories => 'Sheet1!$A$1:$A$3',
28
+ :values => 'Sheet1!$B$1:$B$3'
29
+ )
30
+
31
+ chart.set_rotation(30)
32
+
33
+ worksheet.insert_chart('E9', chart)
34
+
35
+ workbook.close
36
+ compare_xlsx_for_regression(File.join(@regression_output, @xlsx),
37
+ @xlsx,
38
+ nil,
39
+ nil
40
+ )
41
+ end
42
+ end
@@ -0,0 +1,42 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'helper'
3
+
4
+ class TestRegressionChartDoughnut05 < 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_doughnut05
14
+ @xlsx = 'chart_doughnut05.xlsx'
15
+ workbook = WriteXLSX.new(@xlsx)
16
+ worksheet = workbook.add_worksheet
17
+ chart = workbook.add_chart(:type => 'doughnut', :embedded => 1)
18
+
19
+ data = [
20
+ [ 2, 4, 6 ],
21
+ [ 60, 30, 10 ]
22
+ ]
23
+
24
+ worksheet.write('A1', data)
25
+
26
+ chart.add_series(
27
+ :categories => 'Sheet1!$A$1:$A$3',
28
+ :values => 'Sheet1!$B$1:$B$3'
29
+ )
30
+
31
+ chart.set_rotation(360)
32
+
33
+ worksheet.insert_chart('E9', chart)
34
+
35
+ workbook.close
36
+ compare_xlsx_for_regression(File.join(@regression_output, @xlsx),
37
+ @xlsx,
38
+ nil,
39
+ nil
40
+ )
41
+ end
42
+ end