write_xlsx 0.58.0 → 0.59.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (37) hide show
  1. data/README.rdoc +7 -1
  2. data/bin/extract_vba.rb +29 -0
  3. data/examples/add_vba_project.rb +36 -0
  4. data/examples/vbaProject.bin +0 -0
  5. data/lib/write_xlsx/chart.rb +22 -37
  6. data/lib/write_xlsx/package/conditional_format.rb +593 -0
  7. data/lib/write_xlsx/package/content_types.rb +17 -0
  8. data/lib/write_xlsx/package/packager.rb +26 -6
  9. data/lib/write_xlsx/package/relationships.rb +11 -1
  10. data/lib/write_xlsx/package/table.rb +284 -62
  11. data/lib/write_xlsx/utility.rb +179 -0
  12. data/lib/write_xlsx/version.rb +1 -1
  13. data/lib/write_xlsx/workbook.rb +14 -1
  14. data/lib/write_xlsx/worksheet.rb +667 -875
  15. data/test/package/table/test_table01.rb +1 -2
  16. data/test/package/table/test_table02.rb +1 -2
  17. data/test/package/table/test_table03.rb +1 -2
  18. data/test/package/table/test_table04.rb +1 -2
  19. data/test/package/table/test_table05.rb +1 -2
  20. data/test/package/table/test_table06.rb +1 -2
  21. data/test/package/table/test_table07.rb +1 -2
  22. data/test/package/table/test_table08.rb +1 -2
  23. data/test/package/table/test_table09.rb +1 -2
  24. data/test/package/table/test_table10.rb +1 -2
  25. data/test/package/table/test_table11.rb +1 -2
  26. data/test/package/table/test_table12.rb +1 -2
  27. data/test/package/table/test_write_auto_filter.rb +10 -3
  28. data/test/package/table/test_write_table_column.rb +9 -2
  29. data/test/package/table/test_write_table_style_info.rb +12 -11
  30. data/test/package/table/test_write_xml_declaration.rb +6 -1
  31. data/test/perl_output/add_vba_project.xlsm +0 -0
  32. data/test/regression/test_macro01.rb +29 -0
  33. data/test/regression/xlsx_files/macro01.xlsm +0 -0
  34. data/test/regression/xlsx_files/vbaProject01.bin +0 -0
  35. data/test/test_example_match.rb +22 -0
  36. data/test/vbaProject.bin +0 -0
  37. metadata +18 -3
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.51, 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.58, https://github.com/jmcnamara/excel-writer-xlsx .
4
4
 
5
5
  == Description
6
6
 
@@ -74,6 +74,12 @@ the first worksheet in an Excel XML spreadsheet called ruby.xlsx:
74
74
  workbook.close
75
75
 
76
76
  == Recent change
77
+ 2013-02-17 v0.59.0
78
+ Added macro support via VBA projects extracted from existing Excel
79
+ xlsm files. User defined functions can be called from worksheets
80
+ and macros can be called by the user but they cannot, currently,
81
+ be linked to form elements such as buttons.
82
+
77
83
  2013-02-10 v0.58.0
78
84
  Added chart area and plot area formatting.
79
85
 
@@ -0,0 +1,29 @@
1
+ #!/usr/bin/env ruby
2
+ # -*- encoding: utf-8 -*-
3
+
4
+ require 'zip/zipfilesystem'
5
+ require 'fileutils'
6
+
7
+ # src zip filename
8
+ # dest destination directory
9
+ # options :fs_encoding=[UTF-8,Shift_JIS,EUC-JP]
10
+ def extract_vba_project(src, dest, options = {})
11
+ FileUtils.makedirs(dest)
12
+ Zip::ZipInputStream.open(src) do |is|
13
+ loop do
14
+ entry = is.get_next_entry()
15
+ break if entry.nil?()
16
+ if entry.name == 'xl/vbaProject.bin'
17
+ path = File.join(dest, 'vbaProject.bin')
18
+ File.open(path, File::CREAT|File::WRONLY|File::BINARY) do |w|
19
+ w.puts(is.read())
20
+ end
21
+ break
22
+ end
23
+ end
24
+ end
25
+ end
26
+
27
+ # main
28
+
29
+ extract_vba_project('add_vba_project.xlsm', './')
@@ -0,0 +1,36 @@
1
+ #!/usr/bin/env ruby
2
+ # -*- coding: utf-8 -*-
3
+
4
+ #######################################################################
5
+ #
6
+ # An example of adding macros to an Excel::Writer::XLSX file using
7
+ # a VBA project file extracted from an existing Excel xlsm file.
8
+ #
9
+ # The C<extract_vba> utility supplied with Excel::Writer::XLSX can be
10
+ # used to extract the vbaProject.bin file.
11
+ #
12
+ # reverse('(c)'), November 2012, John McNamara, jmcnamara@cpan.org
13
+ # convert to ruby by Hideo NAKAMURA, cxn03651@msj.biglobe.ne.jp
14
+ #
15
+
16
+ require 'rubygems'
17
+ require 'write_xlsx'
18
+
19
+ # Note the file extension should be .xlsm.
20
+ workbook = WriteXLSX.new('add_vba_project.xlsm')
21
+ worksheet = workbook.add_worksheet
22
+
23
+ worksheet.set_column('A:A', 50)
24
+
25
+ # Add the VBA project binary.
26
+ workbook.add_vba_project(File.join(File.dirname(__FILE__), 'vbaProject.bin'))
27
+
28
+ # Show text for the end user.
29
+ worksheet.write('A1', 'Run the SampleMacro embedded in this file.')
30
+ worksheet.write('A2', 'You may have to turn on the Excel Developer option first.')
31
+
32
+ # Call a user defined function from the VBA project.
33
+ worksheet.write('A6', 'Result from a user defined function:')
34
+ worksheet.write('B6', '=MyFunction(7)')
35
+
36
+ workbook.close
Binary file
@@ -2111,7 +2111,8 @@ module Writexlsx
2111
2111
  end
2112
2112
 
2113
2113
  #
2114
- # Write the <c:valAx> element. This is for the second valAx in scatter plots.
2114
+ # Write the <c:valAx> element.
2115
+ # This is for the second valAx in scatter plots.
2115
2116
  #
2116
2117
  # Usually the X axis.
2117
2118
  #
@@ -2528,33 +2529,27 @@ module Writexlsx
2528
2529
  # Write the <c:majorGridlines> element.
2529
2530
  #
2530
2531
  def write_major_gridlines(gridlines) # :nodoc:
2531
- return unless gridlines
2532
- return unless ptrue?(gridlines[:_visible])
2533
-
2534
- if gridlines[:_line] && ptrue?(gridlines[:_line][:_defined])
2535
- @writer.tag_elements('c:majorGridlines') do
2536
- # Write the c:spPr element.
2537
- write_sp_pr(gridlines)
2538
- end
2539
- else
2540
- @writer.empty_tag('c:majorGridlines')
2541
- end
2532
+ write_gridlines_common(gridlines, 'c:majorGridlines')
2542
2533
  end
2543
2534
 
2544
2535
  #
2545
2536
  # Write the <c:minorGridlines> element.
2546
2537
  #
2547
2538
  def write_minor_gridlines(gridlines) # :nodoc:
2539
+ write_gridlines_common(gridlines, 'c:minorGridlines')
2540
+ end
2541
+
2542
+ def write_gridlines_common(gridlines, tag) # :nodoc:
2548
2543
  return unless gridlines
2549
2544
  return unless ptrue?(gridlines[:_visible])
2550
2545
 
2551
2546
  if gridlines[:_line] && ptrue?(gridlines[:_line][:_defined])
2552
- @writer.tag_elements('c:minorGridlines') do
2547
+ @writer.tag_elements(tag) do
2553
2548
  # Write the c:spPr element.
2554
2549
  write_sp_pr(gridlines)
2555
2550
  end
2556
2551
  else
2557
- @writer.empty_tag('c:minorGridlines')
2552
+ @writer.empty_tag(tag)
2558
2553
  end
2559
2554
  end
2560
2555
 
@@ -2880,22 +2875,10 @@ module Writexlsx
2880
2875
  # Write the <a:defRPr> element.
2881
2876
  #
2882
2877
  def write_a_def_rpr(font = nil) # :nodoc:
2883
- style_attributes = get_font_style_attributes(font)
2884
- latin_attributes = get_font_latin_attributes(font)
2885
- has_color = ptrue?(font) && ptrue?(font[:_color])
2886
-
2887
- if !latin_attributes.empty? || has_color
2888
- @writer.tag_elements('a:defRPr', style_attributes) do
2889
- if has_color
2890
- write_a_solid_fill(:color => font[:_color])
2891
- end
2892
- if !latin_attributes.empty?
2893
- write_a_latin(latin_attributes)
2894
- end
2895
- end
2896
- else
2897
- @writer.empty_tag('a:defRPr', style_attributes)
2898
- end
2878
+ write_def_rpr_r_pr_common(
2879
+ font,
2880
+ get_font_style_attributes(font),
2881
+ 'a:defRPr')
2899
2882
  end
2900
2883
 
2901
2884
  #
@@ -2925,17 +2908,19 @@ module Writexlsx
2925
2908
  # Write the <a:rPr> element.
2926
2909
  #
2927
2910
  def write_a_r_pr(font) # :nodoc:
2928
- lang = 'en-US'
2911
+ write_def_rpr_r_pr_common(
2912
+ font,
2913
+ get_font_style_attributes(font).unshift('en-US').unshift('lang'),
2914
+ 'a:rPr'
2915
+ )
2916
+ end
2929
2917
 
2930
- style_attributes = get_font_style_attributes(font)
2918
+ def write_def_rpr_r_pr_common(font, style_attributes, tag) # :nodoc:
2931
2919
  latin_attributes = get_font_latin_attributes(font)
2932
2920
  has_color = ptrue?(font) && ptrue?(font[:_color])
2933
2921
 
2934
- # Add the lang type to the attributes.
2935
- style_attributes.unshift(lang).unshift('lang')
2936
-
2937
2922
  if !latin_attributes.empty? || has_color
2938
- @writer.tag_elements('a:rPr', style_attributes) do
2923
+ @writer.tag_elements(tag, style_attributes) do
2939
2924
  if has_color
2940
2925
  write_a_solid_fill(:color => font[:_color])
2941
2926
  end
@@ -2944,7 +2929,7 @@ module Writexlsx
2944
2929
  end
2945
2930
  end
2946
2931
  else
2947
- @writer.empty_tag('a:rPr', style_attributes)
2932
+ @writer.empty_tag(tag, style_attributes)
2948
2933
  end
2949
2934
  end
2950
2935