thunderboltlabs-rubyXL 1.2.10.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,265 @@
1
+ module RubyXL
2
+ class PrivateClass
3
+ private
4
+
5
+ #validate and modify methods
6
+ def validate_horizontal_alignment(alignment)
7
+ if alignment.to_s == '' || alignment == 'center' || alignment == 'distributed' || alignment == 'justify' || alignment == 'left' || alignment == 'right'
8
+ return true
9
+ end
10
+ raise 'Only center, distributed, justify, left, and right are valid horizontal alignments'
11
+ end
12
+
13
+ def validate_vertical_alignment(alignment)
14
+ if alignment.to_s == '' || alignment == 'center' || alignment == 'distributed' || alignment == 'justify' || alignment == 'top' || alignment == 'bottom'
15
+ return true
16
+ end
17
+ raise 'Only center, distributed, justify, top, and bottom are valid vertical alignments'
18
+ end
19
+
20
+ def validate_border(weight)
21
+ if weight.to_s == '' || weight == 'thin' || weight == 'thick' || weight == 'hairline' || weight == 'medium'
22
+ return true
23
+ end
24
+ raise 'Border weights must only be "hairline", "thin", "medium", or "thick"'
25
+ end
26
+
27
+ def validate_nonnegative(row_or_col)
28
+ if row_or_col < 0
29
+ raise 'Row and Column arguments must be nonnegative'
30
+ end
31
+ end
32
+
33
+ # This method checks to see if there is an equivalent font that exists
34
+ def find_font(workbook, font)
35
+ workbook.fonts.each {|font_id, f|
36
+ if f[:font][:i] == font[:i] &&
37
+ f[:font][:b] == font[:b] &&
38
+ f[:font][:u] == font[:u] &&
39
+ f[:font][:strike] == font[:strike] &&
40
+ f[:font][:name][:attributes][:val] == font[:name][:attributes][:val] &&
41
+ f[:font][:sz][:attributes][:val] == font[:sz][:attributes][:val] &&
42
+ (f[:font][:color] && f[:font][:color][:attributes][:rgb]) == (font[:color] && font[:color][:attributes][:rgb])
43
+ return font_id
44
+ end
45
+ }
46
+ return nil
47
+ end
48
+
49
+ # Helper method to modify the font color
50
+ def modify_font_color(font, font_color)
51
+ if font[:color].nil?
52
+ font[:color] = {:attributes => {:rgb => ''}}
53
+ end
54
+ font[:color][:attributes][:rgb] = font_color.to_s
55
+ return font
56
+ end
57
+
58
+ # Helper method to modify the font's italics settings
59
+ def modify_font_italics(font, italicized)
60
+ if italicized
61
+ font[:i] = {}
62
+ else
63
+ font[:i] = nil
64
+ end
65
+ return font
66
+ end
67
+
68
+ # Helper method to modify the font's bold settings
69
+ def modify_font_bold(font, bolded)
70
+ if bolded
71
+ font[:b] = {}
72
+ else
73
+ font[:b] = nil
74
+ end
75
+ return font
76
+ end
77
+
78
+ # Helper method to modify the font's underline settings
79
+ def modify_font_underline(font, underlined)
80
+ if underlined
81
+ font[:u] = {}
82
+ else
83
+ font[:u] = nil
84
+ end
85
+ return font
86
+ end
87
+
88
+ # Helper method to modify the font's strikethrough settings
89
+ def modify_font_strikethrough(font, struckthrough)
90
+ if struckthrough
91
+ font[:strike] = {}
92
+ else
93
+ font[:strike] = nil
94
+ end
95
+ return font
96
+ end
97
+
98
+ # Determines if font exists
99
+ # If yes, return id of existing font
100
+ # If no, appends font to font array
101
+ def modify_font(workbook, font, old_font_id)
102
+ font_id = old_font_id
103
+ existing_font_id = find_font(workbook, font)
104
+ if !existing_font_id.nil?
105
+ font_id = existing_font_id
106
+ workbook.fonts[font_id][:count] += 1
107
+ workbook.fonts[old_font_id][:count] -= 1
108
+ elsif workbook.fonts[old_font_id.to_s][:count] > 1 || old_font_id == '0'
109
+ font_id = workbook.fonts.size.to_s
110
+ workbook.fonts[font_id] = {}
111
+ workbook.fonts[font_id][:font] = font
112
+ workbook.fonts[font_id][:count] = 1
113
+ workbook.fonts[old_font_id][:count] -= 1
114
+ else
115
+ workbook.fonts[font_id][:font] = font
116
+ end
117
+ return font_id
118
+ end
119
+
120
+ # This method checks to see if there is an equivalent xf that exists
121
+ def find_xf(workbook, xf)
122
+ workbook.cell_xfs[:xf].each_with_index {|xfs, index|
123
+ if xfs[:attributes][:borderId] == xf[:borderId] &&
124
+ xfs[:attributes][:xfId] == xf[:xfId] &&
125
+ xfs[:attributes][:fillId] == xf[:fillId] &&
126
+ xfs[:attributes][:numFmtId] == xf[:numFmtId] &&
127
+ xfs[:attributes][:fontId] == xf[:fontId]
128
+ return index
129
+ end
130
+ }
131
+ return nil
132
+ end
133
+
134
+ # Determines if xf exists
135
+ # If yes, return id of existing xf
136
+ # If no, appends xf to xf array
137
+ def modify_xf(workbook, xf)
138
+ existing_xf_id = find_xf(workbook, xf)
139
+ if !existing_xf_id.nil?
140
+ xf_id = existing_xf_id
141
+ else
142
+ if workbook.cell_xfs[:xf].is_a?Array
143
+ workbook.cell_xfs[:xf] << {:attributes=>xf}
144
+ else
145
+ workbook.cell_xfs[:xf] = [workbook.cell_xfs[:xf], {:attributes=>xf}]
146
+ end
147
+ xf[:applyFont] = '1'
148
+ workbook.cell_xfs[:attributes][:count] += 1
149
+ xf_id = workbook.cell_xfs[:xf].size - 1
150
+ end
151
+ return xf_id
152
+ end
153
+
154
+ #modifies fill array (copies, appends, adds color and solid attribute)
155
+ #then styles array (copies, appends)
156
+ def modify_fill(workbook, style_index, rgb)
157
+ xf_obj = workbook.get_style(style_index)
158
+ xf = workbook.get_style_attributes(xf_obj)
159
+ #modify fill array
160
+ fill_id = xf[:fillId]
161
+
162
+ fill = workbook.fills[fill_id.to_s][:fill]
163
+ if workbook.fills[fill_id.to_s][:count] > 1 || fill_id == 0 || fill_id == 1
164
+ old_size = workbook.fills.size.to_s
165
+ workbook.fills[old_size] = {}
166
+ workbook.fills[old_size][:fill] = deep_copy(fill)
167
+ workbook.fills[old_size][:count] = 1
168
+ workbook.fills[fill_id.to_s][:count] -= 1
169
+
170
+ change_wb_fill(workbook, old_size,rgb)
171
+
172
+ #modify styles array
173
+ fill_id = old_size
174
+ if workbook.cell_xfs[:xf].is_a?Array
175
+ workbook.cell_xfs[:xf] << deep_copy({:attributes=>xf})
176
+ else
177
+ workbook.cell_xfs[:xf] = [workbook.cell_xfs[:xf], deep_copy({:attributes=>xf})]
178
+ end
179
+ xf = workbook.get_style_attributes(workbook.cell_xfs[:xf].last)
180
+ xf[:fillId] = fill_id
181
+ xf[:applyFill] = '1'
182
+ workbook.cell_xfs[:attributes][:count] += 1
183
+ return workbook.cell_xfs[:xf].size-1
184
+ else
185
+ change_wb_fill(workbook, fill_id.to_s,rgb)
186
+ return style_index
187
+ end
188
+ end
189
+
190
+ def modify_border(workbook, style_index)
191
+ xf_obj = workbook.get_style(style_index)
192
+ xf = workbook.get_style_attributes(xf_obj)
193
+
194
+ border_id = Integer(xf[:borderId])
195
+ border = workbook.borders[border_id.to_s][:border]
196
+ if workbook.borders[border_id.to_s][:count] > 1 || border_id == 0 || border_id == 1
197
+ old_size = workbook.borders.size.to_s
198
+ workbook.borders[old_size] = {}
199
+ workbook.borders[old_size][:border] = deep_copy(border)
200
+ workbook.borders[old_size][:count] = 1
201
+
202
+ border_id = old_size
203
+
204
+ if workbook.cell_xfs[:xf].is_a?Array
205
+ workbook.cell_xfs[:xf] << deep_copy(xf_obj)
206
+ else
207
+ workbook.cell_xfs[:xf] = [workbook.cell_xfs[:xf], deep_copy(xf_obj)]
208
+ end
209
+
210
+ xf = workbook.get_style_attributes(workbook.cell_xfs[:xf].last)
211
+ xf[:borderId] = border_id
212
+ xf[:applyBorder] = '1'
213
+ workbook.cell_xfs[:attributes][:count] += 1
214
+ return workbook.cell_xfs[:xf].size-1
215
+ else
216
+ return style_index
217
+ end
218
+ end
219
+
220
+ #is_horizontal is true when doing horizontal alignment,
221
+ #false when doing vertical alignment
222
+ def modify_alignment(workbook, style_index, is_horizontal, alignment)
223
+ old_xf_obj = workbook.get_style(style_index)
224
+
225
+ xf_obj = deep_copy(old_xf_obj)
226
+
227
+ if xf_obj[:alignment].nil? || xf_obj[:alignment][:attributes].nil?
228
+ xf_obj[:alignment] = {:attributes=>{:horizontal=>nil, :vertical=>nil}}
229
+ end
230
+
231
+ if is_horizontal
232
+ xf_obj[:alignment][:attributes][:horizontal] = alignment.to_s
233
+ else
234
+ xf_obj[:alignment][:attributes][:vertical] = alignment.to_s
235
+ end
236
+
237
+ if workbook.cell_xfs[:xf].is_a?Array
238
+ workbook.cell_xfs[:xf] << deep_copy(xf_obj)
239
+ else
240
+ workbook.cell_xfs[:xf] = [workbook.cell_xfs[:xf], deep_copy(xf_obj)]
241
+ end
242
+
243
+ xf = workbook.get_style_attributes(workbook.cell_xfs[:xf].last)
244
+ xf[:applyAlignment] = '1'
245
+ workbook.cell_xfs[:attributes][:count] += 1
246
+ workbook.cell_xfs[:xf].size-1
247
+ end
248
+
249
+ #returns non-shallow copy of hash
250
+ def deep_copy(hash)
251
+ Marshal.load(Marshal.dump(hash))
252
+ end
253
+
254
+ def change_wb_fill(workbook, fill_index, rgb)
255
+ if workbook.fills[fill_index][:fill][:patternFill][:fgColor].nil?
256
+ workbook.fills[fill_index][:fill][:patternFill][:fgColor] = {:attributes => {:rgb => ''}}
257
+ end
258
+ workbook.fills[fill_index][:fill][:patternFill][:fgColor][:attributes][:rgb] = rgb
259
+
260
+ #previously none, doesn't show fill
261
+ workbook.fills[fill_index][:fill][:patternFill][:attributes][:patternType] = 'solid'
262
+ end
263
+
264
+ end
265
+ end