Class: RubyXL::Writer::StylesWriter

Inherits:
Object
  • Object
show all
Defined in:
lib/rubyXL/writer/styles_writer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dirpath, wb) ⇒ StylesWriter

Returns a new instance of StylesWriter.



9
10
11
12
13
# File 'lib/rubyXL/writer/styles_writer.rb', line 9

def initialize(dirpath, wb)
  @dirpath = dirpath
  @workbook = wb
  @filepath = @dirpath + '/xl/styles.xml'
end

Instance Attribute Details

#dirpathObject

Returns the value of attribute dirpath.



7
8
9
# File 'lib/rubyXL/writer/styles_writer.rb', line 7

def dirpath
  @dirpath
end

#filepathObject

Returns the value of attribute filepath.



7
8
9
# File 'lib/rubyXL/writer/styles_writer.rb', line 7

def filepath
  @filepath
end

#workbookObject

Returns the value of attribute workbook.



7
8
9
# File 'lib/rubyXL/writer/styles_writer.rb', line 7

def workbook
  @workbook
end

Instance Method Details

#writeObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
# File 'lib/rubyXL/writer/styles_writer.rb', line 15

def write()
  font_id_corrector = {}
  fill_id_corrector = {}
  border_id_corrector = {}
  style_id_corrector = {}

  builder = Nokogiri::XML::Builder.new do |xml|
    xml.styleSheet('xmlns'=>"http://schemas.openxmlformats.org/spreadsheetml/2006/main") {
      unless @workbook.num_fmts.nil? || @workbook.num_fmts[:attributes].nil?
        xml.numFmts('count'=>@workbook.num_fmts[:attributes][:count].to_s) {
          @workbook.num_fmts[:numFmt].each do |fmt|
            attributes = fmt[:attributes]
            xml.numFmt('numFmtId'=>attributes[:numFmtId].to_s,
              'formatCode'=>attributes[:formatCode].to_s)
          end
        }
      end

      offset = 0
      #default font should stay the same
      font_id_corrector['0']=0
      1.upto(@workbook.fonts.size-1) do |i|
        font_id_corrector[i.to_s] = i-offset
        if @workbook.fonts[i.to_s][:count] == 0
          @workbook.fonts[i.to_s] = nil
          font_id_corrector[i.to_s] = nil
          offset += 1
        end
      end


      offset = 0
      #STARTS AT 2 because excel is stupid
      #and it seems to hard code access the first
      #2 styles.............
      fill_id_corrector['0']=0
      fill_id_corrector['1']=1
      2.upto(@workbook.fills.size-1) do |i|
        fill_id_corrector[i.to_s] = i-offset
        if @workbook.fills[i.to_s][:count] == 0
          @workbook.fills[i.to_s] = nil
          fill_id_corrector[i.to_s] = nil
          offset += 1
        end
      end

      #sets index to itself as init correction
      #if items deleted, indexes adjusted
      #that id 'corrects' to nil
      offset = 0

      #default border should stay the same
      border_id_corrector['0'] = 0
      1.upto(@workbook.borders.size-1) do |i|
        border_id_corrector[i.to_s] = i-offset
        if @workbook.borders[i.to_s][:count] == 0
          @workbook.borders[i.to_s] = nil
          border_id_corrector[i.to_s] = nil
          offset += 1
        end
      end

      if !@workbook.cell_xfs[:xf].is_a?(Array)
        @workbook.cell_xfs[:xf] = [@workbook.cell_xfs[:xf]]
      end
      

      
      style_id_corrector['0']=0
      delete_list = []
      i = 1
      while(i < @workbook.cell_xfs[:xf].size) do
        if style_id_corrector[i.to_s].nil?
          style_id_corrector[i.to_s]= i
        end
        # style correction commented out until bug is fixed
        j = i+1
        while(j < @workbook.cell_xfs[:xf].size) do
          if hash_equal(@workbook.cell_xfs[:xf][i],@workbook.cell_xfs[:xf][j]) #check if this is working
            style_id_corrector[j.to_s] = i
            delete_list << j
          end
          j += 1
        end
        i += 1
      end
      
      #go through delete list, if before delete_list index 0, offset 0, if before delete_list index 1, offset 1, etc.
      delete_list.sort!

      i = 1
      offset = 0
      offset_corrector = 0
      delete_list << @workbook.cell_xfs[:xf].size
      while offset < delete_list.size do
        delete_index = delete_list[offset] - offset

        while i <= delete_list[offset] do #if <= instead of <, fixes odd border but adds random cells with fill              
          if style_id_corrector[i.to_s] == i
            style_id_corrector[i.to_s] -= offset# unless style_id_corrector[i.to_s].nil? #173 should equal 53, not 52?
          end

          i += 1
        end
        @workbook.cell_xfs[:xf].delete_at(delete_index)
        offset += 1
      end
      
      @workbook.style_corrector = style_id_corrector


      xml.fonts('count'=>@workbook.fonts.size) {
        0.upto(@workbook.fonts.size-1) do |i|
          font = @workbook.fonts[i.to_s]
          unless font.nil?
            font = font[:font]
            xml.font {
              xml.sz('val'=>font[:sz][:attributes][:val].to_s)
              unless font[:b].nil?
                xml.b
              end
              unless font[:i].nil?
                xml.i
              end
              unless font[:u].nil?
                xml.u
              end
              unless font[:strike].nil?
                xml.strike
              end
              unless font[:color].nil?
                unless font[:color][:attributes][:indexed].nil?
                  xml.color('indexed'=>font[:color][:attributes][:indexed])
                else
                  unless font[:color][:attributes][:rgb].nil?
                    xml.color('rgb'=>font[:color][:attributes][:rgb])
                  else
                    unless font[:color][:attributes][:theme].nil?
                      xml.color('theme'=>font[:color][:attributes][:theme])
                    end
                  end
                end
              end
              unless font[:family].nil?
                xml.family('val'=>font[:family][:attributes][:val].to_s)
              end
              unless font[:scheme].nil?
                xml.scheme('val'=>font[:scheme][:attributes][:val].to_s)
              end

              xml.name('val'=>font[:name][:attributes][:val].to_s)
            }
          end
        end
      }

      xml.fills('count'=>@workbook.fills.size) {
        0.upto(@workbook.fills.size-1) do |i|
          fill = @workbook.fills[i.to_s]
          unless fill.nil?
            fill = fill[:fill]
            xml.fill {
              xml.patternFill('patternType'=>fill[:patternFill][:attributes][:patternType].to_s) {
                unless fill[:patternFill][:fgColor].nil?
                  fgColor = fill[:patternFill][:fgColor][:attributes]
                  unless fgColor[:indexed].nil?
                    xml.fgColor('indexed'=>fgColor[:indexed].to_s)
                  else
                    unless fgColor[:rgb].nil?
                      xml.fgColor('rgb'=>fgColor[:rgb])
                    end
                  end
                end
                unless fill[:patternFill][:bgColor].nil?
                  bgColor = fill[:patternFill][:bgColor][:attributes]
                  unless bgColor[:indexed].nil?
                    xml.bgColor('indexed'=>bgColor[:indexed].to_s)
                  else
                    unless bgColor[:rgb].nil?
                      xml.bgColor('rgb'=>bgColor[:rgb])
                    end
                  end
                end
              }
            }
          end
        end
      }

      xml.borders('count'=>@workbook.borders.size) {
        0.upto(@workbook.borders.size-1) do |i|
          border = @workbook.borders[i.to_s]
          unless border.nil?
            border = border[:border]
            xml.border {
              if border[:left][:attributes].nil?
                xml.left
              else
                xml.left('style'=>border[:left][:attributes][:style]) {
                  unless border[:left][:color].nil?
                    color = border[:left][:color][:attributes]
                    unless color[:indexed].nil?
                      xml.color('indexed'=>color[:indexed])
                    else
                      unless color[:rgb].nil?
                        xml.color('rgb'=>color[:rgb])
                      end
                    end
                  end
                }
              end
              if border[:right][:attributes].nil?
                xml.right
              else
                xml.right('style'=>border[:right][:attributes][:style]) {
                  unless border[:right][:color].nil?
                    color = border[:right][:color][:attributes]
                    unless color[:indexed].nil?
                      xml.color('indexed'=>color[:indexed])
                    else
                      unless color[:rgb].nil?
                        xml.color('rgb'=>color[:rgb])
                      end
                    end
                  end
                }
              end
              if border[:top][:attributes].nil?
                xml.top
              else
                xml.top('style'=>border[:top][:attributes][:style]) {
                  unless border[:top][:color].nil?
                    color = border[:top][:color][:attributes]
                    unless color[:indexed].nil?
                      xml.color('indexed'=>color[:indexed])
                    else
                      unless color[:rgb].nil?
                        xml.color('rgb'=>color[:rgb])
                      end
                    end
                  end
                }
              end
              if border[:bottom][:attributes].nil?
                xml.bottom
              else
                xml.bottom('style'=>border[:bottom][:attributes][:style]) {
                  unless border[:bottom][:color].nil?
                    color = border[:bottom][:color][:attributes]
                    unless color[:indexed].nil?
                      xml.color('indexed'=>color[:indexed])
                    else
                      unless color[:rgb].nil?
                        xml.color('rgb'=>color[:rgb])
                      end
                    end
                  end
                }
              end
              if border[:diagonal][:attributes].nil?
                xml.diagonal
              else
                xml.diagonal('style'=>border[:diagonal][:attributes][:style]) {
                  unless border[:diagonal][:color].nil?
                    color = border[:diagonal][:color][:attributes]
                    unless color[:indexed].nil?
                      xml.color('indexed'=>color[:indexed])
                    else
                      unless color[:rgb].nil?
                        xml.color('rgb'=>color[:rgb])
                      end
                    end
                  end
                }
              end
            }
          end #unless border.nil?
        end #0.upto(size)
      }

      xml.cellStyleXfs('count'=>@workbook.cell_style_xfs[:attributes][:count].to_s) {
        @workbook.cell_style_xfs[:xf].each do |style|
          style = @workbook.get_style_attributes(style)
          xml.xf('numFmtId'=>style[:numFmtId].to_s,
          'fontId'=>font_id_corrector[style[:fontId].to_s].to_s,
          'fillId'=>fill_id_corrector[style[:fillId].to_s].to_s,
          'borderId'=>border_id_corrector[style[:borderId].to_s].to_s)
        end
      }

      xml.cellXfs('count'=>@workbook.cell_xfs[:xf].size) {
        @workbook.cell_xfs[:xf].each do |xf_obj|
          xf = @workbook.get_style_attributes(xf_obj)

          xml.xf('numFmtId'=>xf[:numFmtId].to_s,
          'fontId'=>font_id_corrector[xf[:fontId].to_s].to_s,
          'fillId'=>fill_id_corrector[xf[:fillId].to_s].to_s,
          'borderId'=>border_id_corrector[xf[:borderId].to_s].to_s,
          'xfId'=>xf[:xfId].to_s,
          'applyFont'=>xf[:applyFont].to_i.to_s, #0 if nil
          'applyFill'=>xf[:applyFill].to_i.to_s,
          'applyAlignment'=>xf[:applyAlignment].to_i.to_s,
          'applyNumberFormat'=>xf[:applyNumberFormat].to_i.to_s) {
            unless xf_obj.is_a?Array
              unless xf_obj[:alignment].nil?
                xml.alignment('horizontal'=>xf_obj[:alignment][:attributes][:horizontal].to_s,
                              'vertical'=>xf_obj[:alignment][:attributes][:vertical].to_s,
                              'wrapText'=>xf_obj[:alignment][:attributes][:wrapText].to_s)
              end
            end
          }
        end
      }
      xml.cellStyles('count'=>@workbook.cell_styles[:attributes][:count].to_s) {

        @workbook.cell_styles[:cellStyle].each do |style|
          style = @workbook.get_style_attributes(style)
          xml.cellStyle('name'=>style[:name].to_s,
          'xfId'=>style[:xfId].to_s,
          'builtinId'=>style[:builtinId].to_s)
        end
      }
      xml.dxfs('count'=>'0')
      xml.tableStyles('count'=>'0', 'defaultTableStyle'=>'TableStyleMedium9')

      unless @colors.nil?
        xml.colors {
          unless @colors[:indexedColors].nil?
            xml.indexedColors {
              @colors[:indexedColors].each do |rgb_color|
                xml.rgbColor rgb_color[:attributes][:rgb]
              end
            }
          end

          unless @colors[:mruColors].nil?
            xml.mruColors {
              @colors[:mruColors].each do |color|
                xml.color color[:attributes][:rgb]
              end
            }
          end
        }
      end
    }
  end
  contents = builder.to_xml
  contents = contents.gsub(/\n/,'')
  contents = contents.gsub(/>(\s)+</,'><')
  contents = contents.sub(/<\?xml version=\"1.0\"\?>/,'<?xml version="1.0" encoding="UTF-8" standalone="yes"?>'+"\n")
  contents
end