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
|
# File 'lib/rubyXL/writer/worksheet_writer.rb', line 15
def write()
render_xml do |xml|
xml << (xml.create_element('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:*') { |root|
col = @worksheet.sheet_data.max_by{ |row| row.size }.size
row = @worksheet.sheet_data.size
root << xml.create_element('dimension', { :ref => RubyXL::Reference.new(0, row - 1, 0, col - 1) })
unless @worksheet.sheet_views.empty?
root << xml.create_element('sheetViews') { |sheet_views|
@worksheet.sheet_views.each { |sheet_view| sheet_views << sheet_view.write_xml(xml) }
}
end
root << xml.create_element('sheetFormatPr', { :baseColWidth => 10, :defaultRowHeight => 13 })
ranges = @worksheet.column_ranges
unless ranges.nil? || ranges.empty?
root << (xml.create_element('cols') { |cols|
ranges.each do |range|
col_attrs = { :min => range.min + 1,
:max => range.max + 1,
:width => range.width || 10,
:customWidth => range.custom_width || 0 }
col_attrs[:style] = range.style_index if range.style_index
cols << (xml.create_element('col', col_attrs))
end
})
end
root << (xml.create_element('sheetData') { |data|
@worksheet.sheet_data.each_with_index { |row, i|
if @worksheet.row_styles[(i+1)].nil?
@worksheet.row_styles[(i+1)] = {}
@worksheet.row_styles[(i+1)][:style] = 0
end
custom_format = '1'
if @worksheet.row_styles[(i+1)][:style].to_s == '0'
custom_format = '0'
end
row_opts = {
:r => i + 1,
:spans => "1:#{row.size}",
:customFormat => custom_format
}
unless @worksheet.row_styles[(i+1)][:style].to_s == ''
row_opts[:s] = @worksheet.row_styles[(i+1)][:style]
end
unless @worksheet.row_styles[(i+1)][:height].to_s == ''
row_opts[:ht] = @worksheet.row_styles[(i+1)][:height]
end
unless @worksheet.row_styles[(i+1)][:customheight].to_s == ''
row_opts[:customHeight] = @worksheet.row_styles[(i+1)][:customHeight]
end
data << (xml.create_element('row', row_opts) { |row_xml|
row.each_with_index { |cell, j|
unless cell.nil?
c_opts = { :r => RubyXL::Reference.ind2ref(i, j), :s => cell.style_index }
unless cell.datatype.nil? || cell.datatype == ''
c_opts[:t] = cell.datatype
end
row_xml << (xml.create_element('c', c_opts) { |cell_xml|
unless cell.formula.nil?
attrs = {}
attrs[:t] = cell.formula_attributes['t'] unless cell.formula_attributes['t'].nil?
attrs[:ref] = cell.formula_attributes['ref'] unless cell.formula_attributes['ref'].nil?
attrs[:si] = cell.formula_attributes['si'] unless cell.formula_attributes['si'].nil?
cell_xml << xml.create_element('f', attrs, cell.formula)
end
cell_value = if (cell.datatype == RubyXL::Cell::SHARED_STRING) then
@workbook.shared_strings.get_index(cell.value).to_s
else cell.value
end
cell_xml << xml.create_element('v', cell_value) unless cell_value.nil?
})
end
}
})
}
})
root << xml.create_element('sheetCalcPr', { :fullCalcOnLoad => 1 })
merged_cells = @worksheet.merged_cells
unless merged_cells.empty?
root << xml.create_element('mergeCells', { :count => merged_cells.size }) { |mc|
@worksheet.merged_cells.each { |ref| mc << xml.create_element('mergeCell', { 'ref' => ref }) }
}
end
root << xml.create_element('phoneticPr', { :fontId => 1, :type => 'noConversion' })
unless @worksheet.validations.empty?
root << (xml.create_element('dataValidations', { :count => @worksheet.validations.size }) { |validations|
@worksheet.validations.each { |validation| validations << validation.write_xml(xml) }
})
end
root << xml.create_element('pageMargins', { :left => 0.75, :right => 0.75, :top => 1, :bottom => 1,
:header => 0.5, :footer => 0.5 })
root << xml.create_element('pageSetup', { :orientation => 'portrait',
:horizontalDpi => 4294967292, :verticalDpi => 4294967292 })
@worksheet.legacy_drawings.each { |drawing| root << drawing.write_xml(xml) }
unless @worksheet.extLst.nil?
root << (xml.create_element('extLst') { |extlst|
extlst << (xml.create_element('ext', {
'xmlns:mx' => 'http://schemas.microsoft.com/office/mac/excel/2008/main',
'uri' => 'http://schemas.microsoft.com/office/mac/excel/2008/main' }) { |ext|
ext << xml.create_element('mx:PLV', { :Mode => 1, :OnePage => 0, :WScale => 0 })
})
})
end
@worksheet.drawings.each { |d| root << xml.create_element('drawing', { 'r:id' => d }) }
})
end
end
|