rubyXL 1.2.10 → 2.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (101) hide show
  1. data/Gemfile +14 -10
  2. data/Gemfile.lock +80 -21
  3. data/LICENSE.txt +1 -1
  4. data/README.rdoc +88 -82
  5. data/Rakefile +7 -2
  6. data/VERSION +1 -1
  7. data/lib/rubyXL.rb +13 -7
  8. data/lib/rubyXL/cell.rb +108 -268
  9. data/lib/rubyXL/generic_storage.rb +40 -0
  10. data/lib/rubyXL/objects/border.rb +66 -0
  11. data/lib/rubyXL/objects/calculation_chain.rb +28 -0
  12. data/lib/rubyXL/objects/cell_style.rb +75 -0
  13. data/lib/rubyXL/objects/color.rb +25 -0
  14. data/lib/rubyXL/objects/column_range.rb +74 -0
  15. data/lib/rubyXL/objects/container_nodes.rb +122 -0
  16. data/lib/rubyXL/objects/data_validation.rb +43 -0
  17. data/lib/rubyXL/objects/document_properties.rb +76 -0
  18. data/lib/rubyXL/objects/extensions.rb +36 -0
  19. data/lib/rubyXL/objects/fill.rb +57 -0
  20. data/lib/rubyXL/objects/font.rb +111 -0
  21. data/lib/rubyXL/objects/formula.rb +24 -0
  22. data/lib/rubyXL/objects/ooxml_object.rb +295 -0
  23. data/lib/rubyXL/objects/reference.rb +110 -0
  24. data/lib/rubyXL/objects/relationships.rb +59 -0
  25. data/lib/rubyXL/objects/shared_strings.rb +57 -0
  26. data/lib/rubyXL/objects/sheet_data.rb +149 -0
  27. data/lib/rubyXL/objects/sheet_view.rb +71 -0
  28. data/lib/rubyXL/objects/stylesheet.rb +200 -0
  29. data/lib/rubyXL/objects/text.rb +87 -0
  30. data/lib/rubyXL/objects/theme.rb +64 -0
  31. data/lib/rubyXL/objects/workbook.rb +233 -0
  32. data/lib/rubyXL/objects/worksheet.rb +485 -0
  33. data/lib/rubyXL/parser.rb +78 -442
  34. data/lib/rubyXL/workbook.rb +216 -385
  35. data/lib/rubyXL/worksheet.rb +509 -1062
  36. data/lib/rubyXL/writer/content_types_writer.rb +104 -68
  37. data/lib/rubyXL/writer/core_writer.rb +26 -43
  38. data/lib/rubyXL/writer/generic_writer.rb +43 -0
  39. data/lib/rubyXL/writer/root_rels_writer.rb +11 -19
  40. data/lib/rubyXL/writer/styles_writer.rb +6 -398
  41. data/lib/rubyXL/writer/theme_writer.rb +321 -327
  42. data/lib/rubyXL/writer/workbook_writer.rb +63 -67
  43. data/lib/rubyXL/writer/worksheet_writer.rb +29 -218
  44. data/rdoc/created.rid +39 -0
  45. data/rdoc/fonts.css +167 -0
  46. data/rdoc/fonts/Lato-Light.ttf +0 -0
  47. data/rdoc/fonts/Lato-LightItalic.ttf +0 -0
  48. data/rdoc/fonts/Lato-Regular.ttf +0 -0
  49. data/rdoc/fonts/Lato-RegularItalic.ttf +0 -0
  50. data/rdoc/fonts/SourceCodePro-Bold.ttf +0 -0
  51. data/rdoc/fonts/SourceCodePro-Regular.ttf +0 -0
  52. data/rdoc/images/add.png +0 -0
  53. data/rdoc/images/arrow_up.png +0 -0
  54. data/rdoc/images/brick.png +0 -0
  55. data/rdoc/images/brick_link.png +0 -0
  56. data/rdoc/images/bug.png +0 -0
  57. data/rdoc/images/bullet_black.png +0 -0
  58. data/rdoc/images/bullet_toggle_minus.png +0 -0
  59. data/rdoc/images/bullet_toggle_plus.png +0 -0
  60. data/rdoc/images/date.png +0 -0
  61. data/rdoc/images/delete.png +0 -0
  62. data/rdoc/images/find.png +0 -0
  63. data/rdoc/images/loadingAnimation.gif +0 -0
  64. data/rdoc/images/macFFBgHack.png +0 -0
  65. data/rdoc/images/package.png +0 -0
  66. data/rdoc/images/page_green.png +0 -0
  67. data/rdoc/images/page_white_text.png +0 -0
  68. data/rdoc/images/page_white_width.png +0 -0
  69. data/rdoc/images/plugin.png +0 -0
  70. data/rdoc/images/ruby.png +0 -0
  71. data/rdoc/images/tag_blue.png +0 -0
  72. data/rdoc/images/tag_green.png +0 -0
  73. data/rdoc/images/transparent.png +0 -0
  74. data/rdoc/images/wrench.png +0 -0
  75. data/rdoc/images/wrench_orange.png +0 -0
  76. data/rdoc/images/zoom.png +0 -0
  77. data/rdoc/js/darkfish.js +140 -0
  78. data/rdoc/js/jquery.js +18 -0
  79. data/rdoc/js/navigation.js +142 -0
  80. data/rdoc/js/search.js +109 -0
  81. data/rdoc/js/search_index.js +1 -0
  82. data/rdoc/js/searcher.js +228 -0
  83. data/rdoc/rdoc.css +580 -0
  84. data/rubyXL.gemspec +90 -34
  85. data/spec/lib/cell_spec.rb +29 -59
  86. data/spec/lib/parser_spec.rb +35 -19
  87. data/spec/lib/reference_spec.rb +29 -0
  88. data/spec/lib/stylesheet_spec.rb +29 -0
  89. data/spec/lib/workbook_spec.rb +22 -17
  90. data/spec/lib/worksheet_spec.rb +47 -202
  91. metadata +185 -148
  92. data/lib/.DS_Store +0 -0
  93. data/lib/rubyXL/Hash.rb +0 -60
  94. data/lib/rubyXL/color.rb +0 -14
  95. data/lib/rubyXL/private_class.rb +0 -265
  96. data/lib/rubyXL/writer/app_writer.rb +0 -62
  97. data/lib/rubyXL/writer/calc_chain_writer.rb +0 -33
  98. data/lib/rubyXL/writer/shared_strings_writer.rb +0 -30
  99. data/lib/rubyXL/writer/workbook_rels_writer.rb +0 -59
  100. data/lib/rubyXL/zip.rb +0 -20
  101. data/spec/lib/hash_spec.rb +0 -28
@@ -1,77 +1,113 @@
1
- # require File.expand_path(File.join(File.dirname(__FILE__),'workbook'))
2
- # require File.expand_path(File.join(File.dirname(__FILE__),'worksheet'))
3
- # require File.expand_path(File.join(File.dirname(__FILE__),'cell'))
4
- # require File.expand_path(File.join(File.dirname(__FILE__),'color'))
5
- require 'rubygems'
6
- require 'nokogiri'
1
+ module RubyXL
2
+ module Writer
3
+ class ContentTypesWriter < GenericWriter
7
4
 
5
+ def filepath
6
+ '[Content_Types].xml'
7
+ end
8
8
 
9
- module RubyXL
10
- module Writer
11
- class ContentTypesWriter
12
- attr_accessor :dirpath, :filepath, :workbook
13
- def initialize(dirpath, wb)
14
- @dirpath = dirpath
15
- @workbook = wb
16
- @filepath = dirpath + '/[Content_Types].xml'
17
- end
9
+ def write()
10
+
11
+ render_xml do |xml|
12
+ xml << (xml.create_element('Types',
13
+ :xmlns => 'http://schemas.openxmlformats.org/package/2006/content-types') { |root|
18
14
 
19
- def write()
20
- builder = Nokogiri::XML::Builder.new do |xml|
21
- xml.Types('xmlns'=>"http://schemas.openxmlformats.org/package/2006/content-types") {
22
- xml.Default('Extension'=>'xml', 'ContentType'=>'application/xml')
23
- unless @workbook.shared_strings.nil?
24
- xml.Override('PartName'=>'/xl/sharedStrings.xml',
25
- 'ContentType'=>"application/vnd.openxmlformats-officedocument.spreadsheetml.sharedStrings+xml")
26
- end
27
- if @workbook.macros.nil? && @workbook.drawings.nil?
28
- xml.Override('PartName'=>'/xl/workbook.xml',
29
- 'ContentType'=>"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml")
30
- else
31
- xml.Override('PartName'=>'/xl/workbook.xml',
32
- 'ContentType'=>"application/vnd.ms-excel.sheet.macroEnabled.main+xml")
33
- end
34
- xml.Override('PartName'=>"/xl/styles.xml",
35
- 'ContentType'=>"application/vnd.openxmlformats-officedocument.spreadsheetml.styles+xml")
36
- xml.Default('Extension'=>'rels','ContentType'=>'application/vnd.openxmlformats-package.relationships+xml')
37
- unless @workbook.external_links.nil?
38
- 1.upto(@workbook.external_links.size-1) do |i|
39
- xml.Override('PartName'=>"/xl/externalLinks/externalLink#{i}.xml",
40
- 'ContentType'=>"application/vnd.openxmlformats-officedocument.spreadsheetml.externalLink+xml")
15
+ unless @workbook.printer_settings.empty?
16
+ root << xml.create_element('Default', {
17
+ :Extension => 'bin',
18
+ :ContentType => 'application/vnd.openxmlformats-officedocument.spreadsheetml.printerSettings' })
41
19
  end
42
- end
43
- unless @workbook.macros.nil?
44
- xml.Override('PartName'=>'/xl/vbaProject.bin',
45
- 'ContentType'=>'application/vnd.ms-office.vbaProject')
46
- end
47
- unless @workbook.printer_settings.nil?
48
- xml.Default('Extension'=>'bin',
49
- 'ContentType'=>'application/vnd.openxmlformats-officedocument.spreadsheetml.printerSettings')
50
- end
51
- unless @workbook.drawings.nil?
52
- xml.Default('Extension'=>'vml',
53
- 'ContentType'=>'application/vnd.openxmlformats-officedocument.vmlDrawing')
54
- end
55
- xml.Override('PartName'=>'/xl/theme/theme1.xml',
56
- 'ContentType'=>"application/vnd.openxmlformats-officedocument.theme+xml")
57
- @workbook.worksheets.each_with_index do |sheet,i|
58
- xml.Override('PartName'=>"/xl/worksheets/sheet#{i+1}.xml",
59
- 'ContentType'=>"application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml")
60
- end
61
- xml.Override('PartName'=>'/docProps/core.xml',
62
- 'ContentType'=>"application/vnd.openxmlformats-package.core-properties+xml")
63
- xml.Override('PartName'=>'/docProps/app.xml',
64
- 'ContentType'=>"application/vnd.openxmlformats-officedocument.extended-properties+xml")
65
- }
66
- end
20
+ root << xml.create_element('Default', {
21
+ :Extension => 'rels',
22
+ :ContentType => 'application/vnd.openxmlformats-package.relationships+xml' })
67
23
 
68
- contents = builder.to_xml
69
- contents = contents.gsub(/\n/,'')
70
- contents = contents.gsub(/>(\s)+</,'><')
71
- contents = contents.sub(/<\?xml version=\"1.0\"\?>/,'<?xml version="1.0" encoding="UTF-8" standalone="yes"?>'+"\n")
24
+ root << xml.create_element('Default', {
25
+ :Extension => 'xml',
26
+ :ContentType => 'application/xml' })
27
+
28
+ root << xml.create_element('Default', {
29
+ :Extension => 'jpeg',
30
+ :ContentType => 'image/jpeg' })
31
+
32
+ # if @workbook.macros.nil? && @workbook.drawings.empty?
33
+ root << xml.create_element('Override', {
34
+ :PartName => '/xl/workbook.xml',
35
+ :ContentType => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml' })
36
+ # else
37
+ # xml.Override('PartName'=>'/xl/workbook.xml',
38
+ # 'ContentType'=>"application/vnd.ms-excel.sheet.macroEnabled.main+xml")
39
+ # end
40
+
41
+ @workbook.worksheets.each_with_index { |sheet,i|
42
+ root << xml.create_element('Override', {
43
+ :PartName => "/xl/worksheets/sheet#{i+1}.xml",
44
+ :ContentType => 'application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml' })
45
+ }
46
+
47
+ root << xml.create_element('Override', {
48
+ :PartName => '/xl/theme/theme1.xml',
49
+ :ContentType => 'application/vnd.openxmlformats-officedocument.theme+xml' })
50
+
51
+ root << xml.create_element('Override', {
52
+ :PartName => '/xl/styles.xml',
53
+ :ContentType => 'application/vnd.openxmlformats-officedocument.spreadsheetml.styles+xml' })
54
+
55
+ unless @workbook.shared_strings_container.empty?
56
+ root << xml.create_element('Override', {
57
+ :PartName => '/xl/sharedStrings.xml',
58
+ :ContentType => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sharedStrings+xml' })
59
+ end
60
+
61
+ @workbook.drawings.each_pair { |k, v|
62
+ root << xml.create_element('Override', {
63
+ :PartName => "/#{@workbook.drawings.local_dir_path}/#{k}",
64
+ :ContentType => 'application/vnd.openxmlformats-officedocument.drawing+xml' })
65
+ # xml.Default('Extension'=>'vml',
66
+ # 'ContentType'=>'application/vnd.openxmlformats-officedocument.vmlDrawing')
67
+ }
68
+
69
+ @workbook.charts.each_pair { |k, v|
70
+ case k
71
+ when /^chart\d*.xml$/ then
72
+ root << xml.create_element('Override', {
73
+ :PartName => "/#{@workbook.charts.local_dir_path}/#{k}",
74
+ :ContentType => 'application/vnd.openxmlformats-officedocument.drawingml.chart+xml' })
75
+ when /^style\d*.xml$/ then
76
+ root << xml.create_element('Override', {
77
+ :PartName => "/#{@workbook.charts.local_dir_path}/#{k}",
78
+ :ContentType => 'application/vnd.ms-office.chartstyle+xml' })
79
+ when /^colors\d*.xml$/ then
80
+ root << xml.create_element('Override', {
81
+ :PartName => "/#{@workbook.charts.local_dir_path}/#{k}",
82
+ :ContentType => 'application/vnd.ms-office.chartcolorstyle+xml' })
83
+ end
84
+ }
85
+
86
+ root << xml.create_element('Override', {
87
+ :PartName => '/docProps/core.xml',
88
+ :ContentType => 'application/vnd.openxmlformats-package.core-properties+xml' })
89
+
90
+ root << xml.create_element('Override', {
91
+ :PartName => '/docProps/app.xml',
92
+ :ContentType => 'application/vnd.openxmlformats-officedocument.extended-properties+xml' })
93
+
94
+ unless @workbook.external_links.nil?
95
+ 1.upto(@workbook.external_links.size-1) do |i|
96
+ root << xml.create_element('Override', {
97
+ :PartName => "/xl/externalLinks/externalLink#{i}.xml",
98
+ :ContentType => 'application/vnd.openxmlformats-officedocument.spreadsheetml.externalLink+xml' })
99
+ end
100
+ end
101
+
102
+ unless @workbook.macros.nil?
103
+ root << xml.create_element('Override', {
104
+ :PartName => '/xl/vbaProject.bin',
105
+ :ContentType => 'application/vnd.ms-office.vbaProject' })
106
+ end
107
+ })
108
+ end
109
+ end
72
110
 
73
- contents
74
111
  end
75
112
  end
76
113
  end
77
- end
@@ -1,51 +1,34 @@
1
- # require File.expand_path(File.join(File.dirname(__FILE__),'workbook'))
2
- # require File.expand_path(File.join(File.dirname(__FILE__),'worksheet'))
3
- # require File.expand_path(File.join(File.dirname(__FILE__),'cell'))
4
- # require File.expand_path(File.join(File.dirname(__FILE__),'color'))
5
- require 'rubygems'
6
- require 'nokogiri'
7
-
8
1
  module RubyXL
9
- module Writer
10
- class CoreWriter
11
- attr_accessor :dirpath, :filepath, :workbook
12
-
13
- def initialize(dirpath, wb)
14
- @dirpath = dirpath
15
- @workbook = wb
16
- @filepath = @dirpath + '/docProps/core.xml'
17
- end
18
-
19
- def write()
20
- builder = Nokogiri::XML::Builder.new do |xml|
21
- xml.coreProperties('xmlns:cp'=>"http://schemas.openxmlformats.org/package/2006/metadata/core-properties",
22
- 'xmlns:dc'=>"http://purl.org/dc/elements/1.1/", 'xmlns:dcterms'=>"http://purl.org/dc/terms/",
23
- 'xmlns:dcmitype'=>"http://purl.org/dc/dcmitype/", 'xmlns:xsi'=>"http://www.w3.org/2001/XMLSchema-instance") {
24
- xml['dc'].creator @workbook.creator.to_s
25
- xml['cp'].lastModifiedBy @workbook.modifier.to_s
26
- xml['dcterms'].created('xsi:type' => 'dcterms:W3CDTF') do
27
- @workbook.created_at
28
- end
2
+ module Writer
3
+ class CoreWriter < GenericWriter
29
4
 
30
- xml['dcterms'].modified('xsi:type' => 'dcterms:W3CDTF')
31
- }
5
+ def filepath
6
+ File.join('docProps', 'core.xml')
32
7
  end
33
8
 
34
- contents = builder.to_xml
35
- contents = contents.gsub(/coreProperties/,'cp:coreProperties')
36
- contents = contents.gsub(/\n/,'')
37
- contents = contents.gsub(/>(\s)+</,'><')
38
-
39
- #seems hack-y..
40
- contents = contents.gsub(/<dcterms:created xsi:type=\"dcterms:W3CDTF\"\/>/,
41
- '<dcterms:created xsi:type="dcterms:W3CDTF">'+@workbook.created_at+'</dcterms:created>')
42
- contents = contents.gsub(/<dcterms:modified xsi:type=\"dcterms:W3CDTF\"\/>/,
43
- '<dcterms:modified xsi:type="dcterms:W3CDTF">'+@workbook.modified_at+'</dcterms:modified>')
44
-
45
- contents = contents.sub(/<\?xml version=\"1.0\"\?>/,'<?xml version="1.0" encoding="UTF-8" standalone="yes"?>'+"\n")
9
+ def write()
10
+ render_xml do |xml|
11
+ xml << (xml.create_element('cp:coreProperties',
12
+ 'xmlns:cp' => 'http://schemas.openxmlformats.org/package/2006/metadata/core-properties',
13
+ 'xmlns:dc' => 'http://purl.org/dc/elements/1.1/',
14
+ 'xmlns:dcterms' => 'http://purl.org/dc/terms/',
15
+ 'xmlns:dcmitype' => 'http://purl.org/dc/dcmitype/',
16
+ 'xmlns:xsi' => 'http://www.w3.org/2001/XMLSchema-instance') { |root|
17
+
18
+ root << xml.create_element('dc:creator', @workbook.creator)
19
+ root << xml.create_element('cp:lastModifiedBy', @workbook.modifier)
20
+
21
+ unless @workbook.created_at.to_s.empty?
22
+ root << xml.create_element('dcterms:created', { 'xsi:type' => 'dcterms:W3CDTF' }, @workbook.created_at)
23
+ end
24
+
25
+ unless @workbook.modified_at.to_s.empty?
26
+ root << xml.create_element('dcterms:modified', { 'xsi:type' => 'dcterms:W3CDTF' }, @workbook.modified_at)
27
+ end
28
+ })
29
+ end
30
+ end
46
31
 
47
- return contents
48
32
  end
49
33
  end
50
34
  end
51
- end
@@ -0,0 +1,43 @@
1
+ require 'rubygems'
2
+ require 'nokogiri'
3
+
4
+ module RubyXL
5
+ module Writer
6
+ class GenericWriter
7
+
8
+ def initialize(workbook)
9
+ @workbook = workbook
10
+ # +self.class+ makes sure constant is pulled from descendant class, not from this one.
11
+ # self.class::FILEPATH
12
+ end
13
+
14
+ def filepath
15
+ raise 'Subclass responsebility'
16
+ end
17
+
18
+ def render_xml
19
+ seed_xml = Nokogiri::XML('<?xml version = "1.0" standalone ="yes"?>')
20
+ seed_xml.encoding = 'UTF-8'
21
+
22
+ yield(seed_xml)
23
+
24
+ seed_xml.to_xml({ :indent => 0, :save_with => Nokogiri::XML::Node::SaveOptions::AS_XML })
25
+ end
26
+
27
+ def add_to_zip(zipfile)
28
+ output = write
29
+ return if output.nil?
30
+ zipfile.get_output_stream(filepath) { |f| f << output }
31
+ end
32
+
33
+ def ooxml_object
34
+ raise 'Subclass responsebility'
35
+ end
36
+
37
+ def write
38
+ ooxml_object && ooxml_object.write_xml
39
+ end
40
+
41
+ end
42
+ end
43
+ end
@@ -1,25 +1,17 @@
1
- # require File.expand_path(File.join(File.dirname(__FILE__),'workbook'))
2
- # require File.expand_path(File.join(File.dirname(__FILE__),'worksheet'))
3
- # require File.expand_path(File.join(File.dirname(__FILE__),'cell'))
4
- # require File.expand_path(File.join(File.dirname(__FILE__),'color'))
5
- require 'rubygems'
6
- require 'nokogiri'
7
-
8
1
  module RubyXL
9
- module Writer
10
- class RootRelsWriter
11
- attr_accessor :dirpath, :workbook
2
+ module Writer
3
+ class RootRelsWriter < GenericWriter
12
4
 
13
- def initialize(dirpath, wb)
14
- @dirpath = dirpath
15
- @wb = wb
16
- end
5
+ def filepath
6
+ File.join('_rels', '.rels')
7
+ end
8
+
9
+ def write()
10
+ contents = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
11
+ <Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"><Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" Target="xl/workbook.xml"/><Relationship Id="rId2" Type="http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties" Target="docProps/core.xml"/><Relationship Id="rId3" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties" Target="docProps/app.xml"/></Relationships>'
12
+ contents
13
+ end
17
14
 
18
- def write()
19
- contents = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
20
- <Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"><Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" Target="xl/workbook.xml"/><Relationship Id="rId2" Type="http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties" Target="docProps/core.xml"/><Relationship Id="rId3" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties" Target="docProps/app.xml"/></Relationships>'
21
- contents
22
15
  end
23
16
  end
24
17
  end
25
- end
@@ -1,407 +1,15 @@
1
- require 'rubygems'
2
- require 'nokogiri'
3
-
4
1
  module RubyXL
5
- module Writer
6
- class StylesWriter
7
- attr_accessor :dirpath, :filepath, :workbook
8
-
9
- def initialize(dirpath, wb)
10
- @dirpath = dirpath
11
- @workbook = wb
12
- @filepath = @dirpath + '/xl/styles.xml'
13
- end
14
-
15
- def write()
16
- font_id_corrector = {}
17
- fill_id_corrector = {}
18
- border_id_corrector = {}
19
- style_id_corrector = {}
20
-
21
- builder = Nokogiri::XML::Builder.new do |xml|
22
- xml.styleSheet('xmlns'=>"http://schemas.openxmlformats.org/spreadsheetml/2006/main") {
23
- unless @workbook.num_fmts.nil? || @workbook.num_fmts[:attributes].nil?
24
- xml.numFmts('count'=>@workbook.num_fmts[:attributes][:count].to_s) {
25
- @workbook.num_fmts[:numFmt].each do |fmt|
26
- attributes = fmt[:attributes]
27
- xml.numFmt('numFmtId'=>attributes[:numFmtId].to_s,
28
- 'formatCode'=>attributes[:formatCode].to_s)
29
- end
30
- }
31
- end
32
-
33
- offset = 0
34
- #default font should stay the same
35
- font_id_corrector['0']=0
36
- 1.upto(@workbook.fonts.size-1) do |i|
37
- font_id_corrector[i.to_s] = i-offset
38
- if @workbook.fonts[i.to_s][:count] == 0
39
- @workbook.fonts[i.to_s] = nil
40
- font_id_corrector[i.to_s] = nil
41
- offset += 1
42
- end
43
- end
44
-
45
-
46
- offset = 0
47
- #STARTS AT 2 because excel is stupid
48
- #and it seems to hard code access the first
49
- #2 styles.............
50
- fill_id_corrector['0']=0
51
- fill_id_corrector['1']=1
52
- 2.upto(@workbook.fills.size-1) do |i|
53
- fill_id_corrector[i.to_s] = i-offset
54
- if @workbook.fills[i.to_s][:count] == 0
55
- @workbook.fills[i.to_s] = nil
56
- fill_id_corrector[i.to_s] = nil
57
- offset += 1
58
- end
59
- end
60
-
61
- #sets index to itself as init correction
62
- #if items deleted, indexes adjusted
63
- #that id 'corrects' to nil
64
- offset = 0
65
-
66
- #default border should stay the same
67
- border_id_corrector['0'] = 0
68
- 1.upto(@workbook.borders.size-1) do |i|
69
- border_id_corrector[i.to_s] = i-offset
70
- if @workbook.borders[i.to_s][:count] == 0
71
- @workbook.borders[i.to_s] = nil
72
- border_id_corrector[i.to_s] = nil
73
- offset += 1
74
- end
75
- end
76
-
77
- if !@workbook.cell_xfs[:xf].is_a?(Array)
78
- @workbook.cell_xfs[:xf] = [@workbook.cell_xfs[:xf]]
79
- end
80
-
81
-
82
-
83
- style_id_corrector['0']=0
84
- delete_list = []
85
- i = 1
86
- while(i < @workbook.cell_xfs[:xf].size) do
87
- if style_id_corrector[i.to_s].nil?
88
- style_id_corrector[i.to_s]= i
89
- end
90
- # style correction commented out until bug is fixed
91
- j = i+1
92
- while(j < @workbook.cell_xfs[:xf].size) do
93
- if hash_equal(@workbook.cell_xfs[:xf][i],@workbook.cell_xfs[:xf][j]) #check if this is working
94
- style_id_corrector[j.to_s] = i
95
- delete_list << j
96
- end
97
- j += 1
98
- end
99
- i += 1
100
- end
101
-
102
- #go through delete list, if before delete_list index 0, offset 0, if before delete_list index 1, offset 1, etc.
103
- delete_list.sort!
104
-
105
- i = 1
106
- offset = 0
107
- offset_corrector = 0
108
- delete_list << @workbook.cell_xfs[:xf].size
109
- while offset < delete_list.size do
110
- delete_index = delete_list[offset] - offset
111
-
112
- while i <= delete_list[offset] do #if <= instead of <, fixes odd border but adds random cells with fill
113
- if style_id_corrector[i.to_s] == i
114
- style_id_corrector[i.to_s] -= offset# unless style_id_corrector[i.to_s].nil? #173 should equal 53, not 52?
115
- end
116
-
117
- i += 1
118
- end
119
- @workbook.cell_xfs[:xf].delete_at(delete_index)
120
- offset += 1
121
- end
122
-
123
- @workbook.style_corrector = style_id_corrector
124
-
2
+ module Writer
3
+ class StylesWriter < GenericWriter
125
4
 
126
- xml.fonts('count'=>@workbook.fonts.size) {
127
- 0.upto(@workbook.fonts.size-1) do |i|
128
- font = @workbook.fonts[i.to_s]
129
- unless font.nil?
130
- font = font[:font]
131
- xml.font {
132
- xml.sz('val'=>font[:sz][:attributes][:val].to_s)
133
- unless font[:b].nil?
134
- xml.b
135
- end
136
- unless font[:i].nil?
137
- xml.i
138
- end
139
- unless font[:u].nil?
140
- xml.u
141
- end
142
- unless font[:strike].nil?
143
- xml.strike
144
- end
145
- unless font[:color].nil?
146
- unless font[:color][:attributes][:indexed].nil?
147
- xml.color('indexed'=>font[:color][:attributes][:indexed])
148
- else
149
- unless font[:color][:attributes][:rgb].nil?
150
- xml.color('rgb'=>font[:color][:attributes][:rgb])
151
- else
152
- unless font[:color][:attributes][:theme].nil?
153
- xml.color('theme'=>font[:color][:attributes][:theme])
154
- end
155
- end
156
- end
157
- end
158
- unless font[:family].nil?
159
- xml.family('val'=>font[:family][:attributes][:val].to_s)
160
- end
161
- unless font[:scheme].nil?
162
- xml.scheme('val'=>font[:scheme][:attributes][:val].to_s)
163
- end
164
-
165
- xml.name('val'=>font[:name][:attributes][:val].to_s)
166
- }
167
- end
168
- end
169
- }
170
-
171
- xml.fills('count'=>@workbook.fills.size) {
172
- 0.upto(@workbook.fills.size-1) do |i|
173
- fill = @workbook.fills[i.to_s]
174
- unless fill.nil?
175
- fill = fill[:fill]
176
- xml.fill {
177
- xml.patternFill('patternType'=>fill[:patternFill][:attributes][:patternType].to_s) {
178
- unless fill[:patternFill][:fgColor].nil?
179
- fgColor = fill[:patternFill][:fgColor][:attributes]
180
- unless fgColor[:indexed].nil?
181
- xml.fgColor('indexed'=>fgColor[:indexed].to_s)
182
- else
183
- unless fgColor[:rgb].nil?
184
- xml.fgColor('rgb'=>fgColor[:rgb])
185
- end
186
- end
187
- end
188
- unless fill[:patternFill][:bgColor].nil?
189
- bgColor = fill[:patternFill][:bgColor][:attributes]
190
- unless bgColor[:indexed].nil?
191
- xml.bgColor('indexed'=>bgColor[:indexed].to_s)
192
- else
193
- unless bgColor[:rgb].nil?
194
- xml.bgColor('rgb'=>bgColor[:rgb])
195
- end
196
- end
197
- end
198
- }
199
- }
200
- end
201
- end
202
- }
203
-
204
- xml.borders('count'=>@workbook.borders.size) {
205
- 0.upto(@workbook.borders.size-1) do |i|
206
- border = @workbook.borders[i.to_s]
207
- unless border.nil?
208
- border = border[:border]
209
- xml.border {
210
- if border[:left][:attributes].nil?
211
- xml.left
212
- else
213
- xml.left('style'=>border[:left][:attributes][:style]) {
214
- unless border[:left][:color].nil?
215
- color = border[:left][:color][:attributes]
216
- unless color[:indexed].nil?
217
- xml.color('indexed'=>color[:indexed])
218
- else
219
- unless color[:rgb].nil?
220
- xml.color('rgb'=>color[:rgb])
221
- end
222
- end
223
- end
224
- }
225
- end
226
- if border[:right][:attributes].nil?
227
- xml.right
228
- else
229
- xml.right('style'=>border[:right][:attributes][:style]) {
230
- unless border[:right][:color].nil?
231
- color = border[:right][:color][:attributes]
232
- unless color[:indexed].nil?
233
- xml.color('indexed'=>color[:indexed])
234
- else
235
- unless color[:rgb].nil?
236
- xml.color('rgb'=>color[:rgb])
237
- end
238
- end
239
- end
240
- }
241
- end
242
- if border[:top][:attributes].nil?
243
- xml.top
244
- else
245
- xml.top('style'=>border[:top][:attributes][:style]) {
246
- unless border[:top][:color].nil?
247
- color = border[:top][:color][:attributes]
248
- unless color[:indexed].nil?
249
- xml.color('indexed'=>color[:indexed])
250
- else
251
- unless color[:rgb].nil?
252
- xml.color('rgb'=>color[:rgb])
253
- end
254
- end
255
- end
256
- }
257
- end
258
- if border[:bottom][:attributes].nil?
259
- xml.bottom
260
- else
261
- xml.bottom('style'=>border[:bottom][:attributes][:style]) {
262
- unless border[:bottom][:color].nil?
263
- color = border[:bottom][:color][:attributes]
264
- unless color[:indexed].nil?
265
- xml.color('indexed'=>color[:indexed])
266
- else
267
- unless color[:rgb].nil?
268
- xml.color('rgb'=>color[:rgb])
269
- end
270
- end
271
- end
272
- }
273
- end
274
- if border[:diagonal][:attributes].nil?
275
- xml.diagonal
276
- else
277
- xml.diagonal('style'=>border[:diagonal][:attributes][:style]) {
278
- unless border[:diagonal][:color].nil?
279
- color = border[:diagonal][:color][:attributes]
280
- unless color[:indexed].nil?
281
- xml.color('indexed'=>color[:indexed])
282
- else
283
- unless color[:rgb].nil?
284
- xml.color('rgb'=>color[:rgb])
285
- end
286
- end
287
- end
288
- }
289
- end
290
- }
291
- end #unless border.nil?
292
- end #0.upto(size)
293
- }
294
-
295
- xml.cellStyleXfs('count'=>@workbook.cell_style_xfs[:attributes][:count].to_s) {
296
- @workbook.cell_style_xfs[:xf].each do |style|
297
- style = @workbook.get_style_attributes(style)
298
- xml.xf('numFmtId'=>style[:numFmtId].to_s,
299
- 'fontId'=>font_id_corrector[style[:fontId].to_s].to_s,
300
- 'fillId'=>fill_id_corrector[style[:fillId].to_s].to_s,
301
- 'borderId'=>border_id_corrector[style[:borderId].to_s].to_s)
302
- end
303
- }
304
-
305
- xml.cellXfs('count'=>@workbook.cell_xfs[:xf].size) {
306
- @workbook.cell_xfs[:xf].each do |xf_obj|
307
- xf = @workbook.get_style_attributes(xf_obj)
308
-
309
- xml.xf('numFmtId'=>xf[:numFmtId].to_s,
310
- 'fontId'=>font_id_corrector[xf[:fontId].to_s].to_s,
311
- 'fillId'=>fill_id_corrector[xf[:fillId].to_s].to_s,
312
- 'borderId'=>border_id_corrector[xf[:borderId].to_s].to_s,
313
- 'xfId'=>xf[:xfId].to_s,
314
- 'applyFont'=>xf[:applyFont].to_i.to_s, #0 if nil
315
- 'applyFill'=>xf[:applyFill].to_i.to_s,
316
- 'applyAlignment'=>xf[:applyAlignment].to_i.to_s,
317
- 'applyNumberFormat'=>xf[:applyNumberFormat].to_i.to_s) {
318
- unless xf_obj.is_a?Array
319
- unless xf_obj[:alignment].nil?
320
- xml.alignment('horizontal'=>xf_obj[:alignment][:attributes][:horizontal].to_s,
321
- 'vertical'=>xf_obj[:alignment][:attributes][:vertical].to_s,
322
- 'wrapText'=>xf_obj[:alignment][:attributes][:wrapText].to_s)
323
- end
324
- end
325
- }
326
- end
327
- }
328
- xml.cellStyles('count'=>@workbook.cell_styles[:attributes][:count].to_s) {
329
-
330
- @workbook.cell_styles[:cellStyle].each do |style|
331
- style = @workbook.get_style_attributes(style)
332
- xml.cellStyle('name'=>style[:name].to_s,
333
- 'xfId'=>style[:xfId].to_s,
334
- 'builtinId'=>style[:builtinId].to_s)
335
- end
336
- }
337
- xml.dxfs('count'=>'0')
338
- xml.tableStyles('count'=>'0', 'defaultTableStyle'=>'TableStyleMedium9')
339
-
340
- unless @colors.nil?
341
- xml.colors {
342
- unless @colors[:indexedColors].nil?
343
- xml.indexedColors {
344
- @colors[:indexedColors].each do |rgb_color|
345
- xml.rgbColor rgb_color[:attributes][:rgb]
346
- end
347
- }
348
- end
349
-
350
- unless @colors[:mruColors].nil?
351
- xml.mruColors {
352
- @colors[:mruColors].each do |color|
353
- xml.color color[:attributes][:rgb]
354
- end
355
- }
356
- end
357
- }
358
- end
359
- }
360
- end
361
- contents = builder.to_xml
362
- contents = contents.gsub(/\n/,'')
363
- contents = contents.gsub(/>(\s)+</,'><')
364
- contents = contents.sub(/<\?xml version=\"1.0\"\?>/,'<?xml version="1.0" encoding="UTF-8" standalone="yes"?>'+"\n")
365
- contents
366
- end
367
-
368
- private
369
-
370
- def hash_equal(h1,h2)
371
- if h1.nil?
372
- if h2.nil?
373
- return true
374
- else
375
- return false
376
- end
377
- elsif h2.nil?
378
- return false
379
- end
380
- if h1.size != h2.size
381
- return false
5
+ def filepath
6
+ File.join('xl', 'styles.xml')
382
7
  end
383
8
 
384
- h1.each do |k,v|
385
- if (h1[k].is_a?String) || (h2[k].is_a?String)
386
- if (h1.is_a?Hash) && (h2.is_a?Hash)
387
- unless hash_equal(h1[k].to_s,h2[k].to_s)
388
- return false
389
- end
390
- else
391
- unless h1[k].to_s == h2[k].to_s
392
- return false
393
- end
394
- end
395
- else
396
- unless h1[k] == h2[k]
397
- return false
398
- end
399
- end
9
+ def ooxml_object
10
+ @workbook.stylesheet
400
11
  end
401
12
 
402
- true
403
13
  end
404
-
405
14
  end
406
15
  end
407
- end