Class: RubyXL::Worksheet
- Inherits:
-
PrivateClass
- Object
- PrivateClass
- RubyXL::Worksheet
- Defined in:
- lib/rubyXL/worksheet.rb
Instance Attribute Summary collapse
-
#cols ⇒ Object
Returns the value of attribute cols.
-
#extLst ⇒ Object
Returns the value of attribute extLst.
-
#legacy_drawing ⇒ Object
Returns the value of attribute legacy_drawing.
-
#merged_cells ⇒ Object
Returns the value of attribute merged_cells.
-
#pane ⇒ Object
Returns the value of attribute pane.
-
#row_styles ⇒ Object
Returns the value of attribute row_styles.
-
#sheet_data ⇒ Object
Returns the value of attribute sheet_data.
-
#sheet_name ⇒ Object
Returns the value of attribute sheet_name.
-
#sheet_view ⇒ Object
Returns the value of attribute sheet_view.
-
#validations ⇒ Object
Returns the value of attribute validations.
-
#workbook ⇒ Object
Returns the value of attribute workbook.
Instance Method Summary collapse
-
#[](row = 0) ⇒ Object
allows for easier access to sheet_data.
- #add_cell(row = 0, column = 0, data = '', formula = nil, overwrite = true) ⇒ Object
- #add_cell_obj(cell, overwrite = true) ⇒ Object
- #change_column_bold(col = 0, bolded = false) ⇒ Object
- #change_column_border_bottom(col = 0, weight = 'thin') ⇒ Object
- #change_column_border_diagonal(col = 0, weight = 'thin') ⇒ Object
- #change_column_border_left(col = 0, weight = 'thin') ⇒ Object
- #change_column_border_right(col = 0, weight = 'thin') ⇒ Object
- #change_column_border_top(col = 0, weight = 'thin') ⇒ Object
- #change_column_fill(col = 0, color_index = 'ffffff') ⇒ Object
- #change_column_font_color(col = 0, font_color = '000000') ⇒ Object
- #change_column_font_name(col = 0, font_name = 'Verdana') ⇒ Object
- #change_column_font_size(col = 0, font_size = 10) ⇒ Object
- #change_column_horizontal_alignment(col = 0, alignment = 'center') ⇒ Object
- #change_column_italics(col = 0, italicized = false) ⇒ Object
- #change_column_strikethrough(col = 0, struckthrough = false) ⇒ Object
- #change_column_underline(col = 0, underlined = false) ⇒ Object
- #change_column_vertical_alignment(col = 0, alignment = 'center') ⇒ Object
- #change_column_width(col = 0, width = 13) ⇒ Object
- #change_row_bold(row = 0, bolded = false) ⇒ Object
- #change_row_border_bottom(row = 0, weight = 'thin') ⇒ Object
- #change_row_border_diagonal(row = 0, weight = 'thin') ⇒ Object
- #change_row_border_left(row = 0, weight = 'thin') ⇒ Object
- #change_row_border_right(row = 0, weight = 'thin') ⇒ Object
- #change_row_border_top(row = 0, weight = 'thin') ⇒ Object
-
#change_row_fill(row = 0, rgb = 'ffffff') ⇒ Object
changes color of fill in (zer0 indexed) row.
- #change_row_font_color(row = 0, font_color = '000000') ⇒ Object
- #change_row_font_name(row = 0, font_name = 'Verdana') ⇒ Object
- #change_row_font_size(row = 0, font_size = 10) ⇒ Object
- #change_row_height(row = 0, height = 10) ⇒ Object
- #change_row_horizontal_alignment(row = 0, alignment = 'center') ⇒ Object
- #change_row_italics(row = 0, italicized = false) ⇒ Object
- #change_row_strikethrough(row = 0, struckthrough = false) ⇒ Object
- #change_row_underline(row = 0, underlined = false) ⇒ Object
- #change_row_vertical_alignment(row = 0, alignment = 'center') ⇒ Object
-
#delete_cell(row = 0, col = 0, shift = nil) ⇒ Object
by default, only sets cell to nil if :left is specified, method will shift row contents to the right of the deleted cell to the left if :up is specified, method will shift column contents below the deleted cell upward.
- #delete_column(col_index = 0) ⇒ Object
- #delete_row(row_index = 0) ⇒ Object
-
#extract_data ⇒ Object
returns 2d array of just the cell values (without style or formula information).
- #get_column_border_bottom(col = 0) ⇒ Object
- #get_column_border_diagonal(col = 0) ⇒ Object
- #get_column_border_left(col = 0) ⇒ Object
- #get_column_border_right(col = 0) ⇒ Object
- #get_column_border_top(col = 0) ⇒ Object
- #get_column_fill(col = 0) ⇒ Object
- #get_column_font_color(col = 0) ⇒ Object
- #get_column_font_name(col = 0) ⇒ Object
- #get_column_font_size(col = 0) ⇒ Object
- #get_column_horizontal_alignment(col = 0) ⇒ Object
- #get_column_vertical_alignment(col = 0) ⇒ Object
- #get_column_width(col = 0) ⇒ Object
- #get_row_border_bottom(row = 0) ⇒ Object
- #get_row_border_diagonal(row = 0) ⇒ Object
- #get_row_border_left(row = 0) ⇒ Object
- #get_row_border_right(row = 0) ⇒ Object
- #get_row_border_top(row = 0) ⇒ Object
- #get_row_fill(row = 0) ⇒ Object
- #get_row_font_color(row = 0) ⇒ Object
- #get_row_font_name(row = 0) ⇒ Object
- #get_row_font_size(row = 0) ⇒ Object
- #get_row_height(row = 0) ⇒ Object
- #get_row_horizontal_alignment(row = 0) ⇒ Object
- #get_row_vertical_alignment(row = 0) ⇒ Object
- #get_table(headers = []) ⇒ Object
-
#initialize(workbook, sheet_name = 'Sheet1', sheet_data = [[nil]], cols = [], merged_cells = []) ⇒ Worksheet
constructor
A new instance of Worksheet.
- #insert_cell(row = 0, col = 0, data = nil, formula = nil, shift = nil) ⇒ Object
-
#insert_column(col_index = 0) ⇒ Object
inserts column at col_index, pushes everything right, takes styles from column to left USE OF THIS METHOD will break formulas which reference cells which are being “pushed down”.
-
#insert_row(row_index = 0) ⇒ Object
inserts row at row_index, pushes down, copies style from below (row previously at that index) USE OF THIS METHOD will break formulas which reference cells which are being “pushed down”.
- #is_column_bolded(col = 0) ⇒ Object
- #is_column_italicized(col = 0) ⇒ Object
- #is_column_struckthrough(col = 0) ⇒ Object
- #is_column_underlined(col = 0) ⇒ Object
- #is_row_bolded(row = 0) ⇒ Object
- #is_row_italicized(row = 0) ⇒ Object
- #is_row_struckthrough(row = 0) ⇒ Object
- #is_row_underlined(row = 0) ⇒ Object
-
#merge_cells(row1 = 0, col1 = 0, row2 = 0, col2 = 0) ⇒ Object
merges cells within a rectangular range.
Constructor Details
#initialize(workbook, sheet_name = 'Sheet1', sheet_data = [[nil]], cols = [], merged_cells = []) ⇒ Worksheet
Returns a new instance of Worksheet.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/rubyXL/worksheet.rb', line 7 def initialize(workbook, sheet_name='Sheet1',sheet_data=[[nil]],cols=[], merged_cells=[]) @workbook = workbook @sheet_name = sheet_name @sheet_data = sheet_data @cols = cols @merged_cells = merged_cells @row_styles={} @sheet_view = { :attributes => { :workbookViewId => 0, :zoomScale => 100, :tabSelected => 1, :view=>'normalLayout', :zoomScaleNormal => 100 } } @extLst = nil @legacy_drawing=nil end |
Instance Attribute Details
#cols ⇒ Object
Returns the value of attribute cols.
4 5 6 |
# File 'lib/rubyXL/worksheet.rb', line 4 def cols @cols end |
#extLst ⇒ Object
Returns the value of attribute extLst.
4 5 6 |
# File 'lib/rubyXL/worksheet.rb', line 4 def extLst @extLst end |
#legacy_drawing ⇒ Object
Returns the value of attribute legacy_drawing.
4 5 6 |
# File 'lib/rubyXL/worksheet.rb', line 4 def legacy_drawing @legacy_drawing end |
#merged_cells ⇒ Object
Returns the value of attribute merged_cells.
4 5 6 |
# File 'lib/rubyXL/worksheet.rb', line 4 def merged_cells @merged_cells end |
#pane ⇒ Object
Returns the value of attribute pane.
4 5 6 |
# File 'lib/rubyXL/worksheet.rb', line 4 def pane @pane end |
#row_styles ⇒ Object
Returns the value of attribute row_styles.
4 5 6 |
# File 'lib/rubyXL/worksheet.rb', line 4 def row_styles @row_styles end |
#sheet_data ⇒ Object
Returns the value of attribute sheet_data.
4 5 6 |
# File 'lib/rubyXL/worksheet.rb', line 4 def sheet_data @sheet_data end |
#sheet_name ⇒ Object
Returns the value of attribute sheet_name.
4 5 6 |
# File 'lib/rubyXL/worksheet.rb', line 4 def sheet_name @sheet_name end |
#sheet_view ⇒ Object
Returns the value of attribute sheet_view.
4 5 6 |
# File 'lib/rubyXL/worksheet.rb', line 4 def sheet_view @sheet_view end |
#validations ⇒ Object
Returns the value of attribute validations.
4 5 6 |
# File 'lib/rubyXL/worksheet.rb', line 4 def validations @validations end |
#workbook ⇒ Object
Returns the value of attribute workbook.
4 5 6 |
# File 'lib/rubyXL/worksheet.rb', line 4 def workbook @workbook end |
Instance Method Details
#[](row = 0) ⇒ Object
allows for easier access to sheet_data
25 26 27 |
# File 'lib/rubyXL/worksheet.rb', line 25 def [](row=0) return @sheet_data[row] end |
#add_cell(row = 0, column = 0, data = '', formula = nil, overwrite = true) ⇒ Object
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 367 368 369 370 371 372 373 374 |
# File 'lib/rubyXL/worksheet.rb', line 342 def add_cell(row=0, column=0, data='', formula=nil,overwrite=true) validate_workbook validate_nonnegative(row) validate_nonnegative(column) unless @sheet_data.size > row && @sheet_data[row].size > column increase_columns(column) increase_rows(row) end datatype = 'str' unless formula.nil? datatype = '' end if overwrite || @sheet_data[row][column].nil? @sheet_data[row][column] = Cell.new(self,row,column,data,formula,datatype) if (data.is_a?Integer) || (data.is_a?Float) @sheet_data[row][column].datatype = '' end col = @cols[get_cols_index(column)] if @row_styles[(row+1).to_s] != nil @sheet_data[row][column].style_index = @row_styles[(row+1).to_s][:style] elsif col != nil @sheet_data[row][column].style_index = col[:attributes][:style] end end add_cell_style(row,column) return @sheet_data[row][column] end |
#add_cell_obj(cell, overwrite = true) ⇒ Object
376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 |
# File 'lib/rubyXL/worksheet.rb', line 376 def add_cell_obj(cell, overwrite=true) validate_workbook if cell.nil? return cell end row = cell.row column = cell.column validate_nonnegative(row) validate_nonnegative(column) unless @sheet_data.size > row && @sheet_data[row].size > column increase_columns(column) increase_rows(row) end if overwrite || @sheet_data[row][column].nil? @sheet_data[row][column] = cell end add_cell_style(row,column) return @sheet_data[row][column] end |
#change_column_bold(col = 0, bolded = false) ⇒ Object
231 232 233 234 |
# File 'lib/rubyXL/worksheet.rb', line 231 def change_column_bold(col=0,bolded=false) s = change_column_font(col,Worksheet::BOLD,bolded) @workbook.fonts[font_id(s)][:font][:b] = bolded ? {} : nil end |
#change_column_border_bottom(col = 0, weight = 'thin') ⇒ Object
321 322 323 |
# File 'lib/rubyXL/worksheet.rb', line 321 def change_column_border_bottom(col=0,weight='thin') change_column_border(col,:bottom,weight) end |
#change_column_border_diagonal(col = 0, weight = 'thin') ⇒ Object
325 326 327 |
# File 'lib/rubyXL/worksheet.rb', line 325 def change_column_border_diagonal(col=0,weight='thin') change_column_border(col,:diagonal,weight) end |
#change_column_border_left(col = 0, weight = 'thin') ⇒ Object
313 314 315 |
# File 'lib/rubyXL/worksheet.rb', line 313 def change_column_border_left(col=0,weight='thin') change_column_border(col,:left,weight) end |
#change_column_border_right(col = 0, weight = 'thin') ⇒ Object
317 318 319 |
# File 'lib/rubyXL/worksheet.rb', line 317 def change_column_border_right(col=0,weight='thin') change_column_border(col,:right,weight) end |
#change_column_border_top(col = 0, weight = 'thin') ⇒ Object
309 310 311 |
# File 'lib/rubyXL/worksheet.rb', line 309 def change_column_border_top(col=0,weight='thin') change_column_border(col,:top,weight) end |
#change_column_fill(col = 0, color_index = 'ffffff') ⇒ Object
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 |
# File 'lib/rubyXL/worksheet.rb', line 266 def change_column_fill(col=0, color_index='ffffff') validate_workbook validate_nonnegative(col) Color.validate_color(color_index) increase_columns(col) i = get_cols_index(col) if cols[i].nil? style_index = 0 else #just copies any style if there is none which already exists for this col #while it changes style/min/max, width *might* be preserved style_index = Integer(@cols[i][:attributes][:style]) end modify_fill(@workbook,style_index,color_index) change_cols(i,col) @sheet_data.each_with_index do |row,i| c = row[Integer(col)] unless c.nil? c.change_fill(color_index) end end end |
#change_column_font_color(col = 0, font_color = '000000') ⇒ Object
214 215 216 217 218 219 220 221 222 223 224 |
# File 'lib/rubyXL/worksheet.rb', line 214 def change_column_font_color(col=0,font_color='000000') Color.validate_color(font_color) s = change_column_font(col,Worksheet::COLOR,font_color) font = @workbook.fonts[font_id(s)][:font] if font[:color].nil? || font[:color][:attributes].nil? font[:color] = { :attributes => { :rgb => nil } } end font[:color][:attributes][:rgb] = font_color.to_s end |
#change_column_font_name(col = 0, font_name = 'Verdana') ⇒ Object
204 205 206 207 |
# File 'lib/rubyXL/worksheet.rb', line 204 def change_column_font_name(col=0,font_name='Verdana') s = change_column_font(col,Worksheet::NAME,font_name) @workbook.fonts[font_id(s)][:font][:name][:attributes][:val] = font_name.to_s end |
#change_column_font_size(col = 0, font_size = 10) ⇒ Object
209 210 211 212 |
# File 'lib/rubyXL/worksheet.rb', line 209 def change_column_font_size(col=0,font_size=10) s = change_column_font(col,Worksheet::SIZE,font_size) @workbook.fonts[font_id(s)][:font][:sz][:attributes][:val] = font_size end |
#change_column_horizontal_alignment(col = 0, alignment = 'center') ⇒ Object
295 296 297 298 299 300 |
# File 'lib/rubyXL/worksheet.rb', line 295 def change_column_horizontal_alignment(col=0,alignment='center') validate_workbook validate_nonnegative(col) validate_horizontal_alignment(alignment) change_column_alignment(col,alignment,true) end |
#change_column_italics(col = 0, italicized = false) ⇒ Object
226 227 228 229 |
# File 'lib/rubyXL/worksheet.rb', line 226 def change_column_italics(col=0,italicized=false) s = change_column_font(col,Worksheet::ITALICS,italicized) @workbook.fonts[font_id(s)][:font][:i] = italicized ? {} : nil end |
#change_column_strikethrough(col = 0, struckthrough = false) ⇒ Object
241 242 243 244 |
# File 'lib/rubyXL/worksheet.rb', line 241 def change_column_strikethrough(col=0,struckthrough=false) s = change_column_font(col,Worksheet::STRIKETHROUGH,struckthrough) @workbook.fonts[font_id(s)][:font][:strike] = struckthrough ? {} : nil end |
#change_column_underline(col = 0, underlined = false) ⇒ Object
236 237 238 239 |
# File 'lib/rubyXL/worksheet.rb', line 236 def change_column_underline(col=0,underlined=false) s = change_column_font(col,Worksheet::UNDERLINE,underlined) @workbook.fonts[font_id(s)][:font][:u] = underlined ? {} : nil end |
#change_column_vertical_alignment(col = 0, alignment = 'center') ⇒ Object
302 303 304 305 306 307 |
# File 'lib/rubyXL/worksheet.rb', line 302 def change_column_vertical_alignment(col=0,alignment='center') validate_workbook validate_nonnegative(col) validate_vertical_alignment(alignment) change_column_alignment(col,alignment,false) end |
#change_column_width(col = 0, width = 13) ⇒ Object
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 |
# File 'lib/rubyXL/worksheet.rb', line 246 def change_column_width(col=0,width=13) validate_workbook validate_nonnegative(col) increase_columns(col) i = get_cols_index(col) if width.to_i.to_s == width.to_s width = Integer(width) elsif width.to_f.to_s == width.to_s width = Float(width) else raise 'You must enter a number for the width' end change_cols(i,col) @cols.last[:attributes][:width] = width @cols.last[:attributes][:customWidth] = '1' end |
#change_row_bold(row = 0, bolded = false) ⇒ Object
133 134 135 136 |
# File 'lib/rubyXL/worksheet.rb', line 133 def change_row_bold(row=0,bolded=false) change_row_font(row,Worksheet::BOLD,bolded) @workbook.fonts[font_id(@row_styles[(row+1).to_s][:style])][:font][:b] = bolded ? {} : nil end |
#change_row_border_bottom(row = 0, weight = 'thin') ⇒ Object
196 197 198 |
# File 'lib/rubyXL/worksheet.rb', line 196 def change_row_border_bottom(row=0,weight='thin') change_row_border(row, :bottom, weight) end |
#change_row_border_diagonal(row = 0, weight = 'thin') ⇒ Object
200 201 202 |
# File 'lib/rubyXL/worksheet.rb', line 200 def change_row_border_diagonal(row=0,weight='thin') change_row_border(row, :diagonal, weight) end |
#change_row_border_left(row = 0, weight = 'thin') ⇒ Object
188 189 190 |
# File 'lib/rubyXL/worksheet.rb', line 188 def change_row_border_left(row=0,weight='thin') change_row_border(row, :left, weight) end |
#change_row_border_right(row = 0, weight = 'thin') ⇒ Object
192 193 194 |
# File 'lib/rubyXL/worksheet.rb', line 192 def change_row_border_right(row=0,weight='thin') change_row_border(row, :right, weight) end |
#change_row_border_top(row = 0, weight = 'thin') ⇒ Object
184 185 186 |
# File 'lib/rubyXL/worksheet.rb', line 184 def change_row_border_top(row=0,weight='thin') change_row_border(row, :top, weight) end |
#change_row_fill(row = 0, rgb = 'ffffff') ⇒ Object
changes color of fill in (zer0 indexed) row
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/rubyXL/worksheet.rb', line 87 def change_row_fill(row=0,rgb='ffffff') validate_workbook validate_nonnegative(row) increase_rows(row) Color.validate_color(rgb) if @row_styles[(Integer(row)+1).to_s].nil? @row_styles[(Integer(row)+1).to_s] = {} @row_styles[(Integer(row)+1).to_s][:style] = '0' end @row_styles[(Integer(row)+1).to_s][:style] = modify_fill(@workbook,Integer(@row_styles[(Integer(row)+1).to_s][:style]),rgb) @sheet_data[Integer(row)].each do |c| unless c.nil? c.change_fill(rgb) end end end |
#change_row_font_color(row = 0, font_color = '000000') ⇒ Object
116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/rubyXL/worksheet.rb', line 116 def change_row_font_color(row=0,font_color='000000') Color.validate_color(font_color) change_row_font(row,Worksheet::COLOR,font_color) font = @workbook.fonts[font_id(@row_styles[(row+1).to_s][:style])][:font] if font[:color].nil? || font[:color][:attributes].nil? font[:color] = { :attributes => { :rgb => nil } } end font[:color][:attributes][:rgb] = font_color.to_s end |
#change_row_font_name(row = 0, font_name = 'Verdana') ⇒ Object
106 107 108 109 |
# File 'lib/rubyXL/worksheet.rb', line 106 def change_row_font_name(row=0,font_name='Verdana') change_row_font(row,Worksheet::NAME,font_name) @workbook.fonts[font_id(@row_styles[(row+1).to_s][:style])][:font][:name][:attributes][:val] = font_name.to_s end |
#change_row_font_size(row = 0, font_size = 10) ⇒ Object
111 112 113 114 |
# File 'lib/rubyXL/worksheet.rb', line 111 def change_row_font_size(row=0,font_size=10) change_row_font(row,Worksheet::SIZE,font_size) @workbook.fonts[font_id(@row_styles[(row+1).to_s][:style])][:font][:sz][:attributes][:val] = font_size end |
#change_row_height(row = 0, height = 10) ⇒ Object
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/rubyXL/worksheet.rb', line 148 def change_row_height(row=0,height=10) validate_workbook validate_nonnegative(row) increase_rows(row) if height.to_i.to_s == height.to_s height = Integer(height) elsif height.to_f.to_s == height.to_s height = Float(height) else raise 'You must enter a number for the height' end if @row_styles[(row+1).to_s].nil? @row_styles[(row+1).to_s] = {} @row_styles[(row+1).to_s][:style] = '0' end @row_styles[(row+1).to_s][:height] = height @row_styles[(row+1).to_s][:customHeight] = '1' end |
#change_row_horizontal_alignment(row = 0, alignment = 'center') ⇒ Object
170 171 172 173 174 175 |
# File 'lib/rubyXL/worksheet.rb', line 170 def change_row_horizontal_alignment(row=0,alignment='center') validate_workbook validate_nonnegative(row) validate_horizontal_alignment(alignment) change_row_alignment(row,alignment,true) end |
#change_row_italics(row = 0, italicized = false) ⇒ Object
128 129 130 131 |
# File 'lib/rubyXL/worksheet.rb', line 128 def change_row_italics(row=0,italicized=false) change_row_font(row,Worksheet::ITALICS,italicized) @workbook.fonts[font_id(@row_styles[(row+1).to_s][:style])][:font][:i] = italicized ? {} : nil end |
#change_row_strikethrough(row = 0, struckthrough = false) ⇒ Object
143 144 145 146 |
# File 'lib/rubyXL/worksheet.rb', line 143 def change_row_strikethrough(row=0,struckthrough=false) change_row_font(row,Worksheet::STRIKETHROUGH,struckthrough) @workbook.fonts[font_id(@row_styles[(row+1).to_s][:style])][:font][:strike] = struckthrough ? {} : nil end |
#change_row_underline(row = 0, underlined = false) ⇒ Object
138 139 140 141 |
# File 'lib/rubyXL/worksheet.rb', line 138 def change_row_underline(row=0,underlined=false) change_row_font(row,Worksheet::UNDERLINE,underlined) @workbook.fonts[font_id(@row_styles[(row+1).to_s][:style])][:font][:u] = underlined ? {} : nil end |
#change_row_vertical_alignment(row = 0, alignment = 'center') ⇒ Object
177 178 179 180 181 182 |
# File 'lib/rubyXL/worksheet.rb', line 177 def change_row_vertical_alignment(row=0,alignment='center') validate_workbook validate_nonnegative(row) validate_vertical_alignment(alignment) change_row_alignment(row,alignment,false) end |
#delete_cell(row = 0, col = 0, shift = nil) ⇒ Object
by default, only sets cell to nil if :left is specified, method will shift row contents to the right of the deleted cell to the left if :up is specified, method will shift column contents below the deleted cell upward
615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 |
# File 'lib/rubyXL/worksheet.rb', line 615 def delete_cell(row=0,col=0,shift=nil) validate_workbook validate_nonnegative(row) validate_nonnegative(col) if @sheet_data.size <= row || @sheet_data[row].size <= col return nil end cell = @sheet_data[row][col] @sheet_data[row][col]=nil if shift && shift != :left && shift != :up raise 'invalid shift option' end if shift == :left @sheet_data[row].delete_at(col) @sheet_data[row] << nil (col...(@sheet_data[row].size)).each do |index| if @sheet_data[row][index].is_a?(Cell) @sheet_data[row][index].column -= 1 end end elsif shift == :up (row...(@sheet_data.size-1)).each do |index| @sheet_data[index][col] = @sheet_data[index+1][col] if @sheet_data[index][col].is_a?(Cell) @sheet_data[index][col].row -= 1 end end if @sheet_data.last[col].is_a?(Cell) @sheet_data.last[col].row -= 1 end end return cell end |
#delete_column(col_index = 0) ⇒ Object
486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 |
# File 'lib/rubyXL/worksheet.rb', line 486 def delete_column(col_index=0) validate_workbook validate_nonnegative(col_index) if col_index >= @sheet_data[0].size return nil end #delete column @sheet_data.map {|r| r.delete_at(col_index)} #change column numbers for cells to right of deleted column @sheet_data.each_with_index do |row,row_index| (col_index...(row.size)).each do |index| if @sheet_data[row_index][index].is_a?(Cell) @sheet_data[row_index][index].column -= 1 end end end #shift column styles #shift col styles 'left' @cols.each do |col| if Integer(col[:attributes][:min]) >= col_index col[:attributes][:min] = (Integer(col[:attributes][:min]) - 1).to_s end if Integer(col[:attributes][:max]) >= col_index col[:attributes][:max] = (Integer(col[:attributes][:max]) - 1).to_s end end end |
#delete_row(row_index = 0) ⇒ Object
402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 |
# File 'lib/rubyXL/worksheet.rb', line 402 def delete_row(row_index=0) validate_workbook validate_nonnegative(row_index) if row_index >= @sheet_data.size return nil end deleted = @sheet_data.delete_at(row_index) row_num = row_index+1 row_num.upto(@sheet_data.size) do |index| @row_styles[(index-1).to_s] = deep_copy(@row_styles[index.to_s]) end @row_styles.delete(@sheet_data.size.to_s) #change row styles # raise row_styles.inspect #change cell row numbers (row_index...(@sheet_data.size-1)).each do |index| @sheet_data[index].map {|c| c.row -= 1 if c} end return deleted end |
#extract_data ⇒ Object
returns 2d array of just the cell values (without style or formula information)
30 31 32 |
# File 'lib/rubyXL/worksheet.rb', line 30 def extract_data return @sheet_data.map {|row| row.map {|c| if c.is_a?(Cell) then c.value else nil end}} end |
#get_column_border_bottom(col = 0) ⇒ Object
903 904 905 |
# File 'lib/rubyXL/worksheet.rb', line 903 def get_column_border_bottom(col=0) return get_column_border(col,:bottom) end |
#get_column_border_diagonal(col = 0) ⇒ Object
907 908 909 |
# File 'lib/rubyXL/worksheet.rb', line 907 def get_column_border_diagonal(col=0) return get_column_border(col,:diagonal) end |
#get_column_border_left(col = 0) ⇒ Object
895 896 897 |
# File 'lib/rubyXL/worksheet.rb', line 895 def get_column_border_left(col=0) return get_column_border(col,:left) end |
#get_column_border_right(col = 0) ⇒ Object
899 900 901 |
# File 'lib/rubyXL/worksheet.rb', line 899 def get_column_border_right(col=0) return get_column_border(col,:right) end |
#get_column_border_top(col = 0) ⇒ Object
891 892 893 |
# File 'lib/rubyXL/worksheet.rb', line 891 def get_column_border_top(col=0) return get_column_border(col,:top) end |
#get_column_fill(col = 0) ⇒ Object
866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 |
# File 'lib/rubyXL/worksheet.rb', line 866 def get_column_fill(col=0) validate_workbook validate_nonnegative(col) if @sheet_data[0].size <= col return nil end style_index = get_cols_style_index(col) if style_index == 0 return "ffffff" #default, white end return @workbook.get_fill_color(@workbook.cell_xfs[:xf][style_index][:attributes]) end |
#get_column_font_color(col = 0) ⇒ Object
813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 |
# File 'lib/rubyXL/worksheet.rb', line 813 def get_column_font_color(col=0) validate_workbook validate_nonnegative(col) if @sheet_data[0].size <= col return nil end style_index = get_cols_style_index(col) font = @workbook.fonts[font_id( style_index ).to_s][:font] if font[:color].nil? || font[:color][:attributes].nil? || font[:color][:attributes][:rgb].nil? return '000000' end return font[:color][:attributes][:rgb] end |
#get_column_font_name(col = 0) ⇒ Object
787 788 789 790 791 792 793 794 795 796 797 798 |
# File 'lib/rubyXL/worksheet.rb', line 787 def get_column_font_name(col=0) validate_workbook validate_nonnegative(col) if @sheet_data[0].size <= col return nil end style_index = get_cols_style_index(col) return @workbook.fonts[font_id( style_index ).to_s][:font][:name][:attributes][:val] end |
#get_column_font_size(col = 0) ⇒ Object
800 801 802 803 804 805 806 807 808 809 810 811 |
# File 'lib/rubyXL/worksheet.rb', line 800 def get_column_font_size(col=0) validate_workbook validate_nonnegative(col) if @sheet_data[0].size <= col return nil end style_index = get_cols_style_index(col) return @workbook.fonts[font_id( style_index ).to_s][:font][:sz][:attributes][:val] end |
#get_column_horizontal_alignment(col = 0) ⇒ Object
883 884 885 |
# File 'lib/rubyXL/worksheet.rb', line 883 def get_column_horizontal_alignment(col=0) get_column_alignment(col, :horizontal) end |
#get_column_vertical_alignment(col = 0) ⇒ Object
887 888 889 |
# File 'lib/rubyXL/worksheet.rb', line 887 def get_column_vertical_alignment(col=0) get_column_alignment(col, :vertical) end |
#get_column_width(col = 0) ⇒ Object
849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 |
# File 'lib/rubyXL/worksheet.rb', line 849 def get_column_width(col=0) validate_workbook validate_nonnegative(col) if @sheet_data[0].size <= col return nil end cols_index = get_cols_index(col) if @cols[cols_index].nil? || @cols[cols_index][:attributes].nil? || @cols[cols_index][:attributes][:width].to_s == '' return 10 end return @cols[cols_index][:attributes][:width] end |
#get_row_border_bottom(row = 0) ⇒ Object
779 780 781 |
# File 'lib/rubyXL/worksheet.rb', line 779 def get_row_border_bottom(row=0) return get_row_border(row,:bottom) end |
#get_row_border_diagonal(row = 0) ⇒ Object
783 784 785 |
# File 'lib/rubyXL/worksheet.rb', line 783 def get_row_border_diagonal(row=0) return get_row_border(row,:diagonal) end |
#get_row_border_left(row = 0) ⇒ Object
771 772 773 |
# File 'lib/rubyXL/worksheet.rb', line 771 def get_row_border_left(row=0) return get_row_border(row,:left) end |
#get_row_border_right(row = 0) ⇒ Object
775 776 777 |
# File 'lib/rubyXL/worksheet.rb', line 775 def get_row_border_right(row=0) return get_row_border(row,:right) end |
#get_row_border_top(row = 0) ⇒ Object
767 768 769 |
# File 'lib/rubyXL/worksheet.rb', line 767 def get_row_border_top(row=0) return get_row_border(row,:top) end |
#get_row_fill(row = 0) ⇒ Object
653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 |
# File 'lib/rubyXL/worksheet.rb', line 653 def get_row_fill(row=0) validate_workbook validate_nonnegative(row) if @sheet_data.size <= row return nil end if @row_styles[(row+1).to_s].nil? return "ffffff" #default, white end xf = xf_attr_row(row) return @workbook.get_fill_color(xf) end |
#get_row_font_color(row = 0) ⇒ Object
704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 |
# File 'lib/rubyXL/worksheet.rb', line 704 def get_row_font_color(row=0) validate_workbook validate_nonnegative(row) if @sheet_data.size <= row return nil end if @row_styles[(row+1).to_s].nil? return '000000' end xf = xf_attr_row(row) color = @workbook.fonts[xf[:fontId].to_s][:font][:color] if color.nil? || color[:attributes].nil? || color[:attributes][:rgb].nil? return '000000' end return color[:attributes][:rgb] end |
#get_row_font_name(row = 0) ⇒ Object
670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 |
# File 'lib/rubyXL/worksheet.rb', line 670 def get_row_font_name(row=0) validate_workbook validate_nonnegative(row) if @sheet_data.size <= row return nil end if @row_styles[(row+1).to_s].nil? return 'Verdana' end xf = xf_attr_row(row) return @workbook.fonts[xf[:fontId].to_s][:font][:name][:attributes][:val] end |
#get_row_font_size(row = 0) ⇒ Object
687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 |
# File 'lib/rubyXL/worksheet.rb', line 687 def get_row_font_size(row=0) validate_workbook validate_nonnegative(row) if @sheet_data.size <= row return nil end if @row_styles[(row+1).to_s].nil? return '10' end xf = xf_attr_row(row) return @workbook.fonts[xf[:fontId].to_s][:font][:sz][:attributes][:val] end |
#get_row_height(row = 0) ⇒ Object
744 745 746 747 748 749 750 751 752 753 754 755 756 757 |
# File 'lib/rubyXL/worksheet.rb', line 744 def get_row_height(row=0) validate_workbook validate_nonnegative(row) if @sheet_data.size <= row return nil end if @row_styles[(row+1).to_s].nil? return 13 else @row_styles[(row+1).to_s][:height] end end |
#get_row_horizontal_alignment(row = 0) ⇒ Object
759 760 761 |
# File 'lib/rubyXL/worksheet.rb', line 759 def get_row_horizontal_alignment(row=0) return get_row_alignment(row,true) end |
#get_row_vertical_alignment(row = 0) ⇒ Object
763 764 765 |
# File 'lib/rubyXL/worksheet.rb', line 763 def get_row_vertical_alignment(row=0) return get_row_alignment(row,false) end |
#get_table(headers = []) ⇒ Object
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 |
# File 'lib/rubyXL/worksheet.rb', line 34 def get_table(headers=[]) validate_workbook if !headers.is_a?(Array) headers = [headers] end row_num = find_first_row_with_content(headers) if row_num.nil? return nil end table_hash = {} table_hash[:table] = [] header_row = @sheet_data[row_num] header_row.each_with_index do |header_cell, index| next if header_cell.nil? || header_cell.value.nil? header = header_cell.value.to_s table_hash[header] = [] original_row = row_num + 1 current_row = original_row cell = @sheet_data[current_row][index] # makes array of hashes in table_hash[:table] # as well as hash of arrays in table_hash[header] while !cell.nil? && !cell.value.nil? table_hash[header] << cell.value table_index = current_row - original_row if table_hash[:table][table_index].nil? table_hash[:table][table_index] = {} end table_hash[:table][table_index][header] = cell.value current_row += 1 if @sheet_data[current_row].nil? cell = nil else cell = @sheet_data[current_row][index] end end end return table_hash end |
#insert_cell(row = 0, col = 0, data = nil, formula = nil, shift = nil) ⇒ Object
583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 |
# File 'lib/rubyXL/worksheet.rb', line 583 def insert_cell(row=0,col=0,data=nil,formula=nil,shift=nil) validate_workbook validate_nonnegative(row) validate_nonnegative(col) increase_rows(row) increase_columns(col) if shift && shift != :right && shift != :down raise 'invalid shift option' end if shift == :right @sheet_data[row].insert(col,nil) (row...(@sheet_data[row].size)).each do |index| if @sheet_data[row][index].is_a?(Cell) @sheet_data[row][index].column += 1 end end elsif shift == :down @sheet_data << Array.new(@sheet_data[row].size) (@sheet_data.size-1).downto(row+1) do |index| @sheet_data[index][col] = @sheet_data[index-1][col] end end return add_cell(row,col,data,formula) end |
#insert_column(col_index = 0) ⇒ Object
inserts column at col_index, pushes everything right, takes styles from column to left USE OF THIS METHOD will break formulas which reference cells which are being “pushed down”
520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 |
# File 'lib/rubyXL/worksheet.rb', line 520 def insert_column(col_index=0) validate_workbook validate_nonnegative(col_index) increase_columns(col_index) old_index = col_index > 0 ? col_index-1 : col_index+1 old_col = @cols[get_cols_index(old_index)] if old_index == 1 old_col = nil end #go through each cell in column @sheet_data.each_with_index do |r,i| #insert "column" in each row r.insert(col_index, nil) #copy styles over to each cell old_cell = r[old_index] unless old_cell.nil? #only add cell if style exists, not copying content if old_cell.style_index != 0 if !old_col.nil? && old_cell.style_index.to_s != old_col[:attributes][:style].to_s c = Cell.new(self,i,col_index) c.style_index = old_cell.style_index @sheet_data[i][col_index] = c end end end end #copy over column-level styles new_col = change_cols(get_cols_index(old_index),old_index) @cols[-1] = deep_copy(old_col)#-1 = last new_col = @cols.last if @cols.last.nil? @cols.pop end #shift col styles 'right' @cols.each do |col| if Integer(col[:attributes][:min]) > col_index col[:attributes][:min] = (1 + Integer(col[:attributes][:min])).to_s end if Integer(col[:attributes][:max]) > col_index col[:attributes][:max] = (1 + Integer(col[:attributes][:max])).to_s end end unless new_col.nil? new_col[:attributes][:min] = (1 + Integer(new_col[:attributes][:min])).to_s new_col[:attributes][:max] = (1 + Integer(new_col[:attributes][:max])).to_s end #update column numbers @sheet_data.each do |row| (col_index+1).upto(row.size) do |j| unless row[j].nil? row[j].column += 1 end end end end |
#insert_row(row_index = 0) ⇒ Object
inserts row at row_index, pushes down, copies style from below (row previously at that index) USE OF THIS METHOD will break formulas which reference cells which are being “pushed down”
431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 |
# File 'lib/rubyXL/worksheet.rb', line 431 def insert_row(row_index=0) validate_workbook validate_nonnegative(row_index) increase_rows(row_index) @sheet_data.insert(row_index,Array.new(@sheet_data[row_index].size)) row_num = row_index+1 #copy cell styles from row above, (or below if first row) @sheet_data[row_index].each_index do |i| if row_index > 0 old_cell = @sheet_data[row_index-1][i] else old_cell = @sheet_data[row_index+1][i] end unless old_cell.nil? #only add cell if style exists, not copying content if @row_styles[(row_num+1).to_s].nil? @row_styles[(row_num+1).to_s] = {:style=>0} end if old_cell.style_index != 0 && old_cell.style_index.to_s != @row_styles[(row_num+1).to_s][:style].to_s c = Cell.new(self,row_index,i) c.style_index = old_cell.style_index @sheet_data[row_index][i] = c end end end #copy row styles from row above, (or below if first row) (@row_styles.size+1).downto(row_num+1) do |i| @row_styles[i.to_s] = @row_styles[(i-1).to_s] end if row_index > 0 @row_styles[row_num.to_s] = @row_styles[(row_num-1).to_s] else @row_styles[row_num.to_s] = nil#@row_styles[(row_num+1).to_s] end #update row value for all rows below (row_index+1).upto(@sheet_data.size-1) do |i| row = @sheet_data[i] row.each do |c| unless c.nil? c.row += 1 end end end return @sheet_data[row_index] end |
#is_column_bolded(col = 0) ⇒ Object
837 838 839 |
# File 'lib/rubyXL/worksheet.rb', line 837 def is_column_bolded(col=0) get_column_bool(col,:b) end |
#is_column_italicized(col = 0) ⇒ Object
833 834 835 |
# File 'lib/rubyXL/worksheet.rb', line 833 def is_column_italicized(col=0) get_column_bool(col,:i) end |
#is_column_struckthrough(col = 0) ⇒ Object
845 846 847 |
# File 'lib/rubyXL/worksheet.rb', line 845 def is_column_struckthrough(col=0) get_column_bool(col,:strike) end |
#is_column_underlined(col = 0) ⇒ Object
841 842 843 |
# File 'lib/rubyXL/worksheet.rb', line 841 def is_column_underlined(col=0) get_column_bool(col,:u) end |
#is_row_bolded(row = 0) ⇒ Object
731 732 733 |
# File 'lib/rubyXL/worksheet.rb', line 731 def is_row_bolded(row=0) return get_row_bool(row,:b) end |
#is_row_italicized(row = 0) ⇒ Object
727 728 729 |
# File 'lib/rubyXL/worksheet.rb', line 727 def is_row_italicized(row=0) return get_row_bool(row,:i) end |
#is_row_struckthrough(row = 0) ⇒ Object
739 740 741 |
# File 'lib/rubyXL/worksheet.rb', line 739 def is_row_struckthrough(row=0) return get_row_bool(row,:strike) end |
#is_row_underlined(row = 0) ⇒ Object
735 736 737 |
# File 'lib/rubyXL/worksheet.rb', line 735 def is_row_underlined(row=0) return get_row_bool(row,:u) end |
#merge_cells(row1 = 0, col1 = 0, row2 = 0, col2 = 0) ⇒ Object
merges cells within a rectangular range
330 331 332 333 334 335 336 337 338 339 340 |
# File 'lib/rubyXL/worksheet.rb', line 330 def merge_cells(row1=0,col1=0,row2=0,col2=0) validate_workbook @merged_cells << { :attributes => { :ref => '' } } cell1 = Cell.convert_to_cell(row1,col1) cell2 = Cell.convert_to_cell(row2,col2) @merged_cells.last[:attributes][:ref] = cell1+':'+cell2 end |