Class: RubyXL::Writer::WorksheetWriter

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dirpath, wb, sheet_num = 1) ⇒ WorksheetWriter

Returns a new instance of WorksheetWriter.



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

def initialize(dirpath, wb, sheet_num=1)
  @dirpath = dirpath
  @workbook = wb
  @sheet_num = sheet_num
  @worksheet = @workbook.worksheets[@sheet_num]
  @filepath = dirpath + '/xl/worksheets/sheet'+(sheet_num+1).to_s()+'.xml'
end

Instance Attribute Details

#dirpathObject

Returns the value of attribute dirpath.



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

def dirpath
  @dirpath
end

#filepathObject

Returns the value of attribute filepath.



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

def filepath
  @filepath
end

#sheet_numObject

Returns the value of attribute sheet_num.



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

def sheet_num
  @sheet_num
end

#workbookObject

Returns the value of attribute workbook.



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

def workbook
  @workbook
end

#worksheetObject

Returns the value of attribute worksheet.



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

def worksheet
  @worksheet
end

Instance Method Details

#writeObject



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
# File 'lib/rubyXL/writer/worksheet_writer.rb', line 17

def write()
  builder = Nokogiri::XML::Builder.new do |xml|
    xml.worksheet('xmlns'=>"http://schemas.openxmlformats.org/spreadsheetml/2006/main",
    'xmlns:r'=>"http://schemas.openxmlformats.org/officeDocument/2006/relationships",
    'xmlns:mc'=>"http://schemas.openxmlformats.org/markup-compatibility/2006",
    'xmlns:mv'=>"urn:schemas-microsoft-com:mac:vml",
    'mc:Ignorable'=>'mv',
    'mc:PreserveAttributes'=>'mv:*') {
      col = 0
      @worksheet.sheet_data.each do |row|
        if row.size > col
          col = row.size
        end
      end
      row = Integer(@worksheet.sheet_data.size)
      dimension = 'A1:'
      dimension += Cell.convert_to_cell(row-1,col-1)
      xml.dimension('ref'=>dimension)
      xml.sheetViews {
        view = @worksheet.sheet_view
        if view.nil? || view[:attributes].nil?
          xml.sheetView('tabSelected'=>1,'view'=>'normalLayout','workbookViewId'=>0)
          raise 'end test' #TODO remove
        else
          view = view[:attributes]
          if view[:view].nil?
            view[:view] = 'normalLayout'
          end

          if view[:workbookViewId].nil?
            view[:workbookViewId] = 0
          end

          if view[:zoomScale].nil? || view[:zoomScaleNormal].nil?
            view[:zoomScale] = 100
            view[:zoomScaleNormal] = 100
          end

          if view[:tabSelected].nil?
            view[:tabSelected] = 1
          end

          xml.sheetView('tabSelected'=>view[:tabSelected],
                        'view'=>view[:view],
                        'workbookViewId'=>view[:workbookViewId],
                        'zoomScale'=>view[:zoomScale],
                        'zoomScaleNormal'=>view[:zoomScaleNormal]) {
            #TODO
            #can't be done unless I figure out a way to programmatically add attributes.
            #(can't put xSplit with an invalid value)
            # unless @worksheet.pane.nil?
              # xml.pane('state'=>@worksheet.pane[:state])
            # end
              # unless view[:selection].nil?
                # xml.
              # end
          }
        end
      }
      xml.sheetFormatPr('baseColWidth'=>'10','defaultRowHeight'=>'13')

      unless @worksheet.cols.nil? || @worksheet.cols.size==0
        xml.cols {
          if !@worksheet.cols.is_a?(Array)
            @worksheet.cols = [@worksheet.cols]
          end
          @worksheet.cols.each do |col|
            if col[:attributes][:customWidth].nil?
              col[:attributes][:customWidth] = '0'
            end
            if col[:attributes][:width].nil?
              col[:attributes][:width] = '10'
              col[:attributes][:customWidth] = '0'
            end
            # unless col[:attributes] == {}
              xml.col('style'=>@workbook.style_corrector[col[:attributes][:style].to_s].to_s,
                'min'=>col[:attributes][:min].to_s,
                'max'=>col[:attributes][:max].to_s,
                'width'=>col[:attributes][:width].to_s,
                'customWidth'=>col[:attributes][:customWidth].to_s)
            # end
          end
        }
      end

      xml.sheetData {
        i=0
        @worksheet.sheet_data.each_with_index do |row,i|
          #TODO fix this spans thing. could be 2:3 (not necessary)
          if @worksheet.row_styles[(i+1).to_s].nil?
            @worksheet.row_styles[(i+1).to_s] = {}
            @worksheet.row_styles[(i+1).to_s][:style] = '0'
          end
          custom_format = '1'
          if @worksheet.row_styles[(i+1).to_s][:style] == '0'
            custom_format = '0'
          end

          @worksheet.row_styles[(i+1).to_s][:style] = @workbook.style_corrector[@worksheet.row_styles[(i+1).to_s][:style].to_s]
          row_opts = {
            'r'=>(i+1).to_s,
            'spans'=>'1:'+row.size.to_s,
            'customFormat'=>custom_format
          }
          unless @worksheet.row_styles[(i+1).to_s][:style].to_s == ''
            row_opts['s'] = @worksheet.row_styles[(i+1).to_s][:style].to_s
          end
          unless @worksheet.row_styles[(i+1).to_s][:height].to_s == ''
            row_opts['ht'] = @worksheet.row_styles[(i+1).to_s][:height].to_s
          end
          unless @worksheet.row_styles[(i+1).to_s][:customheight].to_s == ''
            row_opts['customHeight'] = @worksheet.row_styles[(i+1).to_s][:customHeight].to_s
          end
          xml.row(row_opts) {
            row.each_with_index do |dat, j|
              unless dat.nil?
                  #TODO do xml.c for all cases, inside specific.
                  # if dat.formula.nil?
                  dat.style_index = @workbook.style_corrector[dat.style_index.to_s]
                  c_opts = {'r'=>Cell.convert_to_cell(i,j), 's'=>dat.style_index.to_s}
                  unless dat.datatype.nil? || dat.datatype == ''
                    c_opts['t'] = dat.datatype
                  end
                  xml.c(c_opts) {
                    unless dat.formula.nil?
                      if dat.formula_attributes.nil? || dat.formula_attributes.empty?
                        xml.f dat.formula.to_s
                      else
                        xml.f('t'=>dat.formula_attributes['t'].to_s, 'ref'=>dat.formula_attributes['ref'], 'si'=>dat.formula_attributes['si']).nokogiri dat.formula
                      end
                    end
                    if(dat.datatype == 's')
                      unless dat.value.nil? #empty cell, but has a style
                        xml.v @workbook.shared_strings[dat.value].to_s
                      end
                    elsif(dat.datatype == 'str')
                      xml.v dat.value.to_s
                    elsif(dat.datatype == '') #number
                      xml.v dat.value.to_s
                    end
                  }
                  #
                  # else
                  #   xml.c('r'=>Cell.convert_to_cell(i,j)) {
                  #     xml.v dat.value.to_s
                  #   }
                  # end #data.formula.nil?
              end #unless dat.nil?
            end #row.each_with_index
          }
        end
      }


      xml.sheetCalcPr('fullCalcOnLoad'=>'1')

      unless @worksheet.merged_cells.nil? || @worksheet.merged_cells.size==0
        xml.mergeCells {
          @worksheet.merged_cells.each do |merged_cell|
            xml.mergeCell('ref'=>merged_cell[:attributes][:ref].to_s)
          end
        }
      end

      xml.phoneticPr('fontId'=>'1','type'=>'noConversion')

      unless @worksheet.validations.nil?
        xml.dataValidations('count'=>@worksheet.validations.size.to_s) {
          @worksheet.validations.each do |validation|
            xml.dataValidation('type'=>validation[:attributes][:type],
              'sqref'=>validation[:attributes][:sqref],
              'allowBlank'=>Integer(validation[:attributes][:allowBlank]).to_s,
              'showInputMessage'=>Integer(validation[:attributes][:showInputMessage]).to_s,
              'showErrorMessage'=>Integer(validation[:attributes][:showErrorMessage]).to_s) {
                unless validation[:formula1].nil?
                  xml.formula1 validation[:formula1]
                end
              }
          end
        }
      end

      xml.pageMargins('left'=>'0.75','right'=>'0.75','top'=>'1',
        'bottom'=>'1','header'=>'0.5','footer'=>'0.5')

      xml.pageSetup('orientation'=>'portrait',
        'horizontalDpi'=>'4294967292', 'verticalDpi'=>'4294967292')

      unless @worksheet.legacy_drawing.nil?
        xml.legacyDrawing('r:id'=>@worksheet.legacy_drawing[:attributes][:id])
      end

      unless @worksheet.extLst.nil?
        xml.extLst {
          xml.ext('xmlns:mx'=>"http://schemas.microsoft.com/office/mac/excel/2008/main",
          'uri'=>"http://schemas.microsoft.com/office/mac/excel/2008/main") {
            xml['mx'].PLV('Mode'=>'1', 'OnePage'=>'0','WScale'=>'0')
          }
        }
      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