Class: RubyXL::Worksheet
- Inherits:
-
PrivateClass
- Object
- PrivateClass
- RubyXL::Worksheet
- Includes:
- Enumerable
- Defined in:
- lib/rubyXL/worksheet.rb
Constant Summary collapse
- SHEET_NAME_TEMPLATE =
'Sheet%d'
Instance Attribute Summary collapse
-
#column_ranges ⇒ Object
Returns the value of attribute column_ranges.
-
#drawings ⇒ Object
Returns the value of attribute drawings.
-
#extLst ⇒ Object
Returns the value of attribute extLst.
-
#legacy_drawings ⇒ Object
Returns the value of attribute legacy_drawings.
-
#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_id ⇒ Object
Returns the value of attribute sheet_id.
-
#sheet_name ⇒ Object
Returns the value of attribute sheet_name.
-
#sheet_views ⇒ Object
Returns the value of attribute sheet_views.
-
#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
Changes font color of column.
-
#change_column_font_name(col = 0, font_name = 'Verdana') ⇒ Object
Changes font name of column.
-
#change_column_font_size(col = 0, font_size = 10) ⇒ Object
Changes font size of column.
- #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
- #each ⇒ Object
-
#extract_data(args = {}) ⇒ 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_style_index(col) ⇒ 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 = [], opts = {}) ⇒ Object
-
#initialize(workbook, sheet_name = nil, 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 = nil, sheet_data = [[nil]], cols = [], merged_cells = []) ⇒ Worksheet
Returns a new instance of Worksheet.
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/rubyXL/worksheet.rb', line 11 def initialize(workbook, sheet_name = nil, sheet_data= [[nil]], cols=[], merged_cells=[]) @workbook = workbook @sheet_name = sheet_name || get_default_name @sheet_id = nil @sheet_data = sheet_data @column_ranges = cols @merged_cells = merged_cells || [] @row_styles = [] @sheet_views = [ RubyXL::SheetView.new ] @extLst = nil @legacy_drawings = [] @drawings = [] @validations = [] end |
Instance Attribute Details
#column_ranges ⇒ Object
Returns the value of attribute column_ranges.
5 6 7 |
# File 'lib/rubyXL/worksheet.rb', line 5 def column_ranges @column_ranges end |
#drawings ⇒ Object
Returns the value of attribute drawings.
5 6 7 |
# File 'lib/rubyXL/worksheet.rb', line 5 def drawings @drawings end |
#extLst ⇒ Object
Returns the value of attribute extLst.
5 6 7 |
# File 'lib/rubyXL/worksheet.rb', line 5 def extLst @extLst end |
#legacy_drawings ⇒ Object
Returns the value of attribute legacy_drawings.
5 6 7 |
# File 'lib/rubyXL/worksheet.rb', line 5 def legacy_drawings @legacy_drawings end |
#merged_cells ⇒ Object
Returns the value of attribute merged_cells.
5 6 7 |
# File 'lib/rubyXL/worksheet.rb', line 5 def merged_cells @merged_cells end |
#pane ⇒ Object
Returns the value of attribute pane.
5 6 7 |
# File 'lib/rubyXL/worksheet.rb', line 5 def pane @pane end |
#row_styles ⇒ Object
Returns the value of attribute row_styles.
5 6 7 |
# File 'lib/rubyXL/worksheet.rb', line 5 def row_styles @row_styles end |
#sheet_data ⇒ Object
Returns the value of attribute sheet_data.
5 6 7 |
# File 'lib/rubyXL/worksheet.rb', line 5 def sheet_data @sheet_data end |
#sheet_id ⇒ Object
Returns the value of attribute sheet_id.
5 6 7 |
# File 'lib/rubyXL/worksheet.rb', line 5 def sheet_id @sheet_id end |
#sheet_name ⇒ Object
Returns the value of attribute sheet_name.
5 6 7 |
# File 'lib/rubyXL/worksheet.rb', line 5 def sheet_name @sheet_name end |
#sheet_views ⇒ Object
Returns the value of attribute sheet_views.
5 6 7 |
# File 'lib/rubyXL/worksheet.rb', line 5 def sheet_views @sheet_views end |
#validations ⇒ Object
Returns the value of attribute validations.
5 6 7 |
# File 'lib/rubyXL/worksheet.rb', line 5 def validations @validations end |
#workbook ⇒ Object
Returns the value of attribute workbook.
5 6 7 |
# File 'lib/rubyXL/worksheet.rb', line 5 def workbook @workbook end |
Instance Method Details
#[](row = 0) ⇒ Object
allows for easier access to sheet_data
39 40 41 |
# File 'lib/rubyXL/worksheet.rb', line 39 def [](row = 0) @sheet_data[row] end |
#add_cell(row = 0, column = 0, data = '', formula = nil, overwrite = true) ⇒ Object
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 375 376 377 378 |
# File 'lib/rubyXL/worksheet.rb', line 350 def add_cell(row=0, column=0, data='', formula=nil,overwrite=true) validate_workbook validate_nonnegative(row) validate_nonnegative(column) ensure_cell_exists(row, column) datatype = (formula.nil?) ? RubyXL::Cell::RAW_STRING : '' 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 = RubyXL::ColumnRange.find(column, @column_ranges) if @row_styles[row+1] != nil @sheet_data[row][column].style_index = @row_styles[row+1][:style] elsif col != nil @sheet_data[row][column].style_index = col.style_index end end @sheet_data[row][column].style_index ||= 0 add_cell_style(row,column) return @sheet_data[row][column] end |
#add_cell_obj(cell, overwrite = true) ⇒ Object
380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 |
# File 'lib/rubyXL/worksheet.rb', line 380 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) ensure_cell_exists(row, column) 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
265 266 267 268 269 270 |
# File 'lib/rubyXL/worksheet.rb', line 265 def change_column_bold(col = 0, bolded = false) xf = get_col_xf(col) font = @workbook.fonts[xf.font_id].dup font.set_bold(bolded) change_column_font(col, Worksheet::BOLD, bolded, font, xf) end |
#change_column_border_bottom(col = 0, weight = 'thin') ⇒ Object
336 337 338 |
# File 'lib/rubyXL/worksheet.rb', line 336 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
340 341 342 |
# File 'lib/rubyXL/worksheet.rb', line 340 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
328 329 330 |
# File 'lib/rubyXL/worksheet.rb', line 328 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
332 333 334 |
# File 'lib/rubyXL/worksheet.rb', line 332 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
324 325 326 |
# File 'lib/rubyXL/worksheet.rb', line 324 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
299 300 301 302 303 304 305 306 307 308 309 310 311 312 |
# File 'lib/rubyXL/worksheet.rb', line 299 def change_column_fill(col=0, color_index='ffffff') validate_workbook validate_nonnegative(col) Color.validate_color(color_index) ensure_cell_exists(0, col) new_style_index = modify_fill(@workbook, get_column_style_index(col), color_index) RubyXL::ColumnRange.update(col, @column_ranges, { 'style' => new_style_index }) @sheet_data.each { |row| c = row[col] c.change_fill(color_index) if c } end |
#change_column_font_color(col = 0, font_color = '000000') ⇒ Object
Changes font color of column
249 250 251 252 253 254 255 256 |
# File 'lib/rubyXL/worksheet.rb', line 249 def change_column_font_color(col=0, font_color='000000') Color.validate_color(font_color) xf = get_col_xf(col) font = @workbook.fonts[xf.font_id].dup font.set_rgb_color(font_color) change_column_font(col, Worksheet::COLOR, font_color, font, xf) end |
#change_column_font_name(col = 0, font_name = 'Verdana') ⇒ Object
Changes font name of column
233 234 235 236 237 238 |
# File 'lib/rubyXL/worksheet.rb', line 233 def change_column_font_name(col = 0, font_name = 'Verdana') xf = get_col_xf(col) font = @workbook.fonts[xf.font_id].dup font.set_name(font_name) change_column_font(col, Worksheet::NAME, font_name, font, xf) end |
#change_column_font_size(col = 0, font_size = 10) ⇒ Object
Changes font size of column
241 242 243 244 245 246 |
# File 'lib/rubyXL/worksheet.rb', line 241 def change_column_font_size(col=0, font_size=10) xf = get_col_xf(col) font = @workbook.fonts[xf.font_id].dup font.set_size(font_size) change_column_font(col, Worksheet::SIZE, font_size, font, xf) end |
#change_column_horizontal_alignment(col = 0, alignment = 'center') ⇒ Object
314 315 316 317 |
# File 'lib/rubyXL/worksheet.rb', line 314 def change_column_horizontal_alignment(col=0,alignment='center') validate_horizontal_alignment(alignment) change_column_alignment(col,alignment,true) end |
#change_column_italics(col = 0, italicized = false) ⇒ Object
258 259 260 261 262 263 |
# File 'lib/rubyXL/worksheet.rb', line 258 def change_column_italics(col = 0, italicized = false) xf = get_col_xf(col) font = @workbook.fonts[xf.font_id].dup font.set_italic(italicized) change_column_font(col, Worksheet::ITALICS, italicized, font, xf) end |
#change_column_strikethrough(col = 0, struckthrough = false) ⇒ Object
279 280 281 282 283 284 |
# File 'lib/rubyXL/worksheet.rb', line 279 def change_column_strikethrough(col=0, struckthrough=false) xf = get_col_xf(col) font = @workbook.fonts[xf.font_id].dup font.set_strikethrough(struckthrough) change_column_font(col, Worksheet::STRIKETHROUGH, struckthrough, font, xf) end |
#change_column_underline(col = 0, underlined = false) ⇒ Object
272 273 274 275 276 277 |
# File 'lib/rubyXL/worksheet.rb', line 272 def change_column_underline(col = 0, underlined = false) xf = get_col_xf(col) font = @workbook.fonts[xf.font_id].dup font.set_underline(underlined) change_column_font(col, Worksheet::UNDERLINE, underlined, font, xf) end |
#change_column_vertical_alignment(col = 0, alignment = 'center') ⇒ Object
319 320 321 322 |
# File 'lib/rubyXL/worksheet.rb', line 319 def change_column_vertical_alignment(col=0,alignment='center') validate_vertical_alignment(alignment) change_column_alignment(col,alignment,false) end |
#change_column_width(col = 0, width = 13) ⇒ Object
286 287 288 289 290 291 292 |
# File 'lib/rubyXL/worksheet.rb', line 286 def change_column_width(col = 0, width = 13) validate_workbook validate_nonnegative(col) ensure_cell_exists(0, col) RubyXL::ColumnRange.update(col, @column_ranges, { 'width' => width, 'customWidth' => 1 }) end |
#change_row_bold(row = 0, bolded = false) ⇒ Object
155 156 157 158 159 160 |
# File 'lib/rubyXL/worksheet.rb', line 155 def change_row_bold(row = 0, bolded=false) ensure_cell_exists(row) font = row_font(row).dup font.set_bold(bolded) change_row_font(row, Worksheet::BOLD, bolded, font) end |
#change_row_border_bottom(row = 0, weight = 'thin') ⇒ Object
224 225 226 |
# File 'lib/rubyXL/worksheet.rb', line 224 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
228 229 230 |
# File 'lib/rubyXL/worksheet.rb', line 228 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
216 217 218 |
# File 'lib/rubyXL/worksheet.rb', line 216 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
220 221 222 |
# File 'lib/rubyXL/worksheet.rb', line 220 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
212 213 214 |
# File 'lib/rubyXL/worksheet.rb', line 212 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
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/rubyXL/worksheet.rb', line 106 def change_row_fill(row = 0, rgb = 'ffffff') validate_workbook validate_nonnegative(row) ensure_cell_exists(row) Color.validate_color(rgb) if @row_styles[(Integer(row)+1)].nil? @row_styles[(Integer(row)+1)] = {} @row_styles[(Integer(row)+1)][:style] = 0 end @row_styles[(Integer(row)+1)][:style] = modify_fill(@workbook,Integer(@row_styles[(Integer(row)+1)][: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
140 141 142 143 144 145 146 |
# File 'lib/rubyXL/worksheet.rb', line 140 def change_row_font_color(row = 0, font_color='000000') ensure_cell_exists(row) Color.validate_color(font_color) font = row_font(row).dup font.set_rgb_color(font_color) change_row_font(row, Worksheet::COLOR, font_color, font) end |
#change_row_font_name(row = 0, font_name = 'Verdana') ⇒ Object
126 127 128 129 130 131 |
# File 'lib/rubyXL/worksheet.rb', line 126 def change_row_font_name(row = 0, font_name = 'Verdana') ensure_cell_exists(row) font = row_font(row).dup font.set_name(font_name) change_row_font(row, Worksheet::NAME, font_name, font) end |
#change_row_font_size(row = 0, font_size = 10) ⇒ Object
133 134 135 136 137 138 |
# File 'lib/rubyXL/worksheet.rb', line 133 def change_row_font_size(row = 0, font_size=10) ensure_cell_exists(row) font = row_font(row).dup font.set_size(font_size) change_row_font(row, Worksheet::SIZE, font_size, font) end |
#change_row_height(row = 0, height = 10) ⇒ Object
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
# File 'lib/rubyXL/worksheet.rb', line 176 def change_row_height(row = 0, height=10) validate_workbook validate_nonnegative(row) ensure_cell_exists(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)].nil? @row_styles[(row+1)] = {} @row_styles[(row+1)][:style] = 0 end @row_styles[(row+1)][:height] = height @row_styles[(row+1)][:customHeight] = '1' end |
#change_row_horizontal_alignment(row = 0, alignment = 'center') ⇒ Object
198 199 200 201 202 203 |
# File 'lib/rubyXL/worksheet.rb', line 198 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
148 149 150 151 152 153 |
# File 'lib/rubyXL/worksheet.rb', line 148 def change_row_italics(row = 0, italicized=false) ensure_cell_exists(row) font = row_font(row).dup font.set_italic(italicized) change_row_font(row, Worksheet::ITALICS, italicized, font) end |
#change_row_strikethrough(row = 0, struckthrough = false) ⇒ Object
169 170 171 172 173 174 |
# File 'lib/rubyXL/worksheet.rb', line 169 def change_row_strikethrough(row = 0, struckthrough=false) ensure_cell_exists(row) font = row_font(row).dup font.set_strikethrough(struckthrough) change_row_font(row, Worksheet::STRIKETHROUGH, struckthrough, font) end |
#change_row_underline(row = 0, underlined = false) ⇒ Object
162 163 164 165 166 167 |
# File 'lib/rubyXL/worksheet.rb', line 162 def change_row_underline(row = 0, underlined=false) ensure_cell_exists(row) font = row_font(row).dup font.set_underline(underlined) change_row_font(row, Worksheet::UNDERLINE, underlined, font) end |
#change_row_vertical_alignment(row = 0, alignment = 'center') ⇒ Object
205 206 207 208 209 210 |
# File 'lib/rubyXL/worksheet.rb', line 205 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
569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 |
# File 'lib/rubyXL/worksheet.rb', line 569 def delete_cell(row = 0, col=0, shift=nil) validate_workbook validate_nonnegative(row) validate_nonnegative(col) return nil if @sheet_data.size <= row || @sheet_data[row].size <= col cell = @sheet_data[row][col] case shift when nil then @sheet_data[row][col] = nil when :left then @sheet_data[row].delete_at(col) @sheet_data[row] << nil (col...(@sheet_data[row].size)).each { |index| if @sheet_data[row][index].is_a?(Cell) @sheet_data[row][index].column -= 1 end } when :up then (row...(@sheet_data.size-1)).each { |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 } @sheet_data.last[col].row -= 1 if @sheet_data.last[col].is_a?(Cell) else raise 'invalid shift option' end return cell end |
#delete_column(col_index = 0) ⇒ Object
482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 |
# File 'lib/rubyXL/worksheet.rb', line 482 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 @column_ranges.each { |range| range.delete_column(col_index) } end |
#delete_row(row_index = 0) ⇒ Object
403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 |
# File 'lib/rubyXL/worksheet.rb', line 403 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_styles.delete_at(row_index) # Change cell row numbers row_index.upto(@sheet_data.size - 1) { |index| @sheet_data[index].each{ |c| c.row -= 1 unless c.nil? } } return deleted end |
#each ⇒ Object
43 44 45 |
# File 'lib/rubyXL/worksheet.rb', line 43 def each @sheet_data.each { |row| yield(row) } end |
#extract_data(args = {}) ⇒ Object
returns 2d array of just the cell values (without style or formula information)
48 49 50 51 |
# File 'lib/rubyXL/worksheet.rb', line 48 def extract_data(args = {}) raw_values = args.delete(:raw) || false return @sheet_data.map {|row| row.map {|c| if c.is_a?(Cell) then c.value(:raw => raw_values) else nil end}} end |
#get_column_border_bottom(col = 0) ⇒ Object
776 777 778 |
# File 'lib/rubyXL/worksheet.rb', line 776 def get_column_border_bottom(col=0) get_column_border(col, :bottom) end |
#get_column_border_diagonal(col = 0) ⇒ Object
780 781 782 |
# File 'lib/rubyXL/worksheet.rb', line 780 def get_column_border_diagonal(col=0) get_column_border(col, :diagonal) end |
#get_column_border_left(col = 0) ⇒ Object
768 769 770 |
# File 'lib/rubyXL/worksheet.rb', line 768 def get_column_border_left(col=0) get_column_border(col, :left) end |
#get_column_border_right(col = 0) ⇒ Object
772 773 774 |
# File 'lib/rubyXL/worksheet.rb', line 772 def get_column_border_right(col=0) get_column_border(col, :right) end |
#get_column_border_top(col = 0) ⇒ Object
764 765 766 |
# File 'lib/rubyXL/worksheet.rb', line 764 def get_column_border_top(col=0) get_column_border(col, :top) end |
#get_column_fill(col = 0) ⇒ Object
749 750 751 752 753 754 |
# File 'lib/rubyXL/worksheet.rb', line 749 def get_column_fill(col=0) validate_workbook validate_nonnegative(col) return nil if @sheet_data[0].size <= col @workbook.get_fill_color(get_col_xf(col)) end |
#get_column_font_color(col = 0) ⇒ Object
711 712 713 714 |
# File 'lib/rubyXL/worksheet.rb', line 711 def get_column_font_color(col = 0) font = column_font(col) font && (font.get_rgb_color || '000000') end |
#get_column_font_name(col = 0) ⇒ Object
701 702 703 704 |
# File 'lib/rubyXL/worksheet.rb', line 701 def get_column_font_name(col = 0) font = column_font(col) font && font.get_name end |
#get_column_font_size(col = 0) ⇒ Object
706 707 708 709 |
# File 'lib/rubyXL/worksheet.rb', line 706 def get_column_font_size(col = 0) font = column_font(col) font && font.get_size end |
#get_column_horizontal_alignment(col = 0) ⇒ Object
756 757 758 |
# File 'lib/rubyXL/worksheet.rb', line 756 def get_column_horizontal_alignment(col=0) get_column_alignment(col, :horizontal) end |
#get_column_style_index(col) ⇒ Object
294 295 296 297 |
# File 'lib/rubyXL/worksheet.rb', line 294 def get_column_style_index(col) range = RubyXL::ColumnRange.find(col, @column_ranges) (range && range.style_index) || 0 end |
#get_column_vertical_alignment(col = 0) ⇒ Object
760 761 762 |
# File 'lib/rubyXL/worksheet.rb', line 760 def get_column_vertical_alignment(col=0) get_column_alignment(col, :vertical) end |
#get_column_width(col = 0) ⇒ Object
736 737 738 739 740 741 742 743 744 745 746 747 |
# File 'lib/rubyXL/worksheet.rb', line 736 def get_column_width(col=0) validate_workbook validate_nonnegative(col) if @sheet_data[0].size <= col return nil end range = RubyXL::ColumnRange.find(col, @column_ranges) (range && range.width) || 10 end |
#get_row_border_bottom(row = 0) ⇒ Object
693 694 695 |
# File 'lib/rubyXL/worksheet.rb', line 693 def get_row_border_bottom(row = 0) return get_row_border(row, :bottom) end |
#get_row_border_diagonal(row = 0) ⇒ Object
697 698 699 |
# File 'lib/rubyXL/worksheet.rb', line 697 def get_row_border_diagonal(row = 0) return get_row_border(row, :diagonal) end |
#get_row_border_left(row = 0) ⇒ Object
685 686 687 |
# File 'lib/rubyXL/worksheet.rb', line 685 def get_row_border_left(row = 0) return get_row_border(row, :left) end |
#get_row_border_right(row = 0) ⇒ Object
689 690 691 |
# File 'lib/rubyXL/worksheet.rb', line 689 def get_row_border_right(row = 0) return get_row_border(row, :right) end |
#get_row_border_top(row = 0) ⇒ Object
681 682 683 |
# File 'lib/rubyXL/worksheet.rb', line 681 def get_row_border_top(row = 0) return get_row_border(row, :top) end |
#get_row_fill(row = 0) ⇒ Object
605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 |
# File 'lib/rubyXL/worksheet.rb', line 605 def get_row_fill(row = 0) validate_workbook validate_nonnegative(row) if @sheet_data.size <= row return nil end if @row_styles[(row+1)].nil? return "ffffff" #default, white end xf = get_row_xf(row) return @workbook.get_fill_color(xf) end |
#get_row_font_color(row = 0) ⇒ Object
632 633 634 635 636 |
# File 'lib/rubyXL/worksheet.rb', line 632 def get_row_font_color(row = 0) font = row_font(row) color = font && font.color color && (color.rgb || '000000') end |
#get_row_font_name(row = 0) ⇒ Object
622 623 624 625 |
# File 'lib/rubyXL/worksheet.rb', line 622 def get_row_font_name(row = 0) font = row_font(row) font && font.get_name end |
#get_row_font_size(row = 0) ⇒ Object
627 628 629 630 |
# File 'lib/rubyXL/worksheet.rb', line 627 def get_row_font_size(row = 0) font = row_font(row) font && font.get_size end |
#get_row_height(row = 0) ⇒ Object
658 659 660 661 662 663 664 665 666 667 668 669 670 671 |
# File 'lib/rubyXL/worksheet.rb', line 658 def get_row_height(row = 0) validate_workbook validate_nonnegative(row) if @sheet_data.size <= row return nil end if @row_styles[(row+1)].nil? return 13 else @row_styles[(row+1)][:height] end end |
#get_row_horizontal_alignment(row = 0) ⇒ Object
673 674 675 |
# File 'lib/rubyXL/worksheet.rb', line 673 def get_row_horizontal_alignment(row = 0) return get_row_alignment(row,true) end |
#get_row_vertical_alignment(row = 0) ⇒ Object
677 678 679 |
# File 'lib/rubyXL/worksheet.rb', line 677 def get_row_vertical_alignment(row = 0) return get_row_alignment(row,false) end |
#get_table(headers = [], opts = {}) ⇒ Object
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 |
# File 'lib/rubyXL/worksheet.rb', line 53 def get_table(headers = [], opts = {}) validate_workbook headers = [headers] unless headers.is_a?(Array) row_num = find_first_row_with_content(headers) return nil if row_num.nil? table_hash = {} table_hash[:table] = [] header_row = @sheet_data[row_num] header_row.each_with_index { |header_cell, index| break if index>0 && !opts[:last_header].nil? && !header_row[index-1].nil? && !header_row[index-1].value.nil? && header_row[index-1].value.to_s==opts[:last_header] next if header_cell.nil? || header_cell.value.nil? header = header_cell.value.to_s table_hash[:sorted_headers]||=[] table_hash[:sorted_headers] << header 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] table_index = current_row - original_row cell_test = (!cell.nil? && !cell.value.nil?) while cell_test || (table_hash[:table][table_index] && !table_hash[:table][table_index].empty?) table_hash[header] << cell.value if cell_test table_index = current_row - original_row if cell_test then table_hash[:table][table_index] ||= {} table_hash[:table][table_index][header] = cell.value end current_row += 1 if @sheet_data[current_row].nil? then cell = nil else cell = @sheet_data[current_row][index] end cell_test = (!cell.nil? && !cell.value.nil?) end } return table_hash end |
#insert_cell(row = 0, col = 0, data = nil, formula = nil, shift = nil) ⇒ Object
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 |
# File 'lib/rubyXL/worksheet.rb', line 539 def insert_cell(row = 0, col = 0, data = nil, formula = nil, shift = nil) validate_workbook validate_nonnegative(row) validate_nonnegative(col) ensure_cell_exists(row, col) case shift when nil then # No shifting at all when :right then @sheet_data[row].insert(col,nil) (row...(@sheet_data[row].size)).each { |index| if @sheet_data[row][index].is_a?(Cell) @sheet_data[row][index].column += 1 end } when :down then @sheet_data << Array.new(@sheet_data[row].size) (@sheet_data.size-1).downto(row+1) { |index| @sheet_data[index][col] = @sheet_data[index-1][col] } else raise 'invalid shift option' 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”
507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 |
# File 'lib/rubyXL/worksheet.rb', line 507 def insert_column(col_index = 0) validate_workbook validate_nonnegative(col_index) ensure_cell_exists(0, col_index) old_range = col_index > 0 ? RubyXL::ColumnRange.find(col_index, @column_ranges) : RubyXL::ColumnRange.new #go through each cell in column @sheet_data.each_with_index do |row, row_index| old_cell = row[col_index] new_cell = nil if old_cell && old_cell.style_index != 0 && old_range && old_range.style_index != old_cell.style_index.to_i then new_cell = Cell.new(self, row_index, col_index) new_cell.style_index = old_cell.style_index end row.insert(col_index, new_cell) end ColumnRange.insert_column(col_index, @column_ranges) #update column numbers @sheet_data.each { |row| (col_index + 1).upto(row.size) { |col| row[col].column = col unless row[col].nil? } } 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”
426 427 428 429 430 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 |
# File 'lib/rubyXL/worksheet.rb', line 426 def insert_row(row_index=0) validate_workbook validate_nonnegative(row_index) ensure_cell_exists(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)].nil? @row_styles[(row_num+1)] = {:style=>0} end if old_cell.style_index != 0 && old_cell.style_index != @row_styles[(row_num+1)][:style] 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] = @row_styles[(i-1)] end if row_index > 0 @row_styles[row_num] = @row_styles[(row_num-1)] else @row_styles[row_num] = 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
721 722 723 724 |
# File 'lib/rubyXL/worksheet.rb', line 721 def is_column_bolded(col = 0) font = column_font(col) font && font.is_bold end |
#is_column_italicized(col = 0) ⇒ Object
716 717 718 719 |
# File 'lib/rubyXL/worksheet.rb', line 716 def is_column_italicized(col = 0) font = column_font(col) font && font.is_italic end |
#is_column_struckthrough(col = 0) ⇒ Object
731 732 733 734 |
# File 'lib/rubyXL/worksheet.rb', line 731 def is_column_struckthrough(col = 0) font = column_font(col) font && font.is_strikethrough end |
#is_column_underlined(col = 0) ⇒ Object
726 727 728 729 |
# File 'lib/rubyXL/worksheet.rb', line 726 def is_column_underlined(col = 0) font = column_font(col) font && font.is_underlined end |
#is_row_bolded(row = 0) ⇒ Object
643 644 645 646 |
# File 'lib/rubyXL/worksheet.rb', line 643 def is_row_bolded(row = 0) font = row_font(row) font && font.is_bold end |
#is_row_italicized(row = 0) ⇒ Object
638 639 640 641 |
# File 'lib/rubyXL/worksheet.rb', line 638 def is_row_italicized(row = 0) font = row_font(row) font && font.is_italic end |
#is_row_struckthrough(row = 0) ⇒ Object
653 654 655 656 |
# File 'lib/rubyXL/worksheet.rb', line 653 def is_row_struckthrough(row = 0) font = row_font(row) font && font.is_strikethrough end |
#is_row_underlined(row = 0) ⇒ Object
648 649 650 651 |
# File 'lib/rubyXL/worksheet.rb', line 648 def is_row_underlined(row = 0) font = row_font(row) font && font.is_underlined end |
#merge_cells(row1 = 0, col1 = 0, row2 = 0, col2 = 0) ⇒ Object
merges cells within a rectangular range
345 346 347 348 |
# File 'lib/rubyXL/worksheet.rb', line 345 def merge_cells(row1 = 0, col1 = 0, row2 = 0, col2 = 0) validate_workbook @merged_cells << RubyXL::Reference.new(row1, row2, col1, col2) end |