Module: RubyXL::WorksheetConvenienceMethods

Included in:
Worksheet
Defined in:
lib/rubyXL/convenience_methods.rb

Instance Method Summary collapse

Instance Method Details

#change_column_alignment(column_index, &block) ⇒ Object


605
606
607
608
609
610
611
612
613
614
615
616
617
618
# File 'lib/rubyXL/convenience_methods.rb', line 605

def change_column_alignment(column_index, &block)
  validate_workbook
  ensure_cell_exists(0, column_index)

  cols.get_range(column_index).style_index = @workbook.modify_alignment(get_col_style(column_index), &block)
  # Excel gets confused if width is not explicitly set for a column that had alignment changes
  change_column_width(column_index) if get_column_width_raw(column_index).nil?

  sheet_data.rows.each { |row|
    c = row[column_index]
    next if c.nil?
    c.style_index = @workbook.modify_alignment(c.style_index, &block)
  }
end

#change_column_bold(column_index, bolded = false) ⇒ Object


526
527
528
529
530
531
# File 'lib/rubyXL/convenience_methods.rb', line 526

def change_column_bold(column_index, bolded = false)
  xf = get_col_xf(column_index)
  font = @workbook.fonts[xf.font_id].dup
  font.set_bold(bolded)
  change_column_font(column_index, Worksheet::BOLD, bolded, font, xf)
end

#change_column_border(column_index, direction, weight) ⇒ Object


580
581
582
583
584
585
586
587
588
589
590
# File 'lib/rubyXL/convenience_methods.rb', line 580

def change_column_border(column_index, direction, weight)
  validate_workbook
  ensure_cell_exists(0, column_index)

  cols.get_range(column_index).style_index = @workbook.modify_border(get_col_style(column_index), direction, weight)

  sheet_data.rows.each { |row|
    c = row.cells[column_index]
    c.change_border(direction, weight) unless c.nil?
  }
end

#change_column_border_bottom(column_index, weight = 'thin') ⇒ Object


570
571
572
573
# File 'lib/rubyXL/convenience_methods.rb', line 570

def change_column_border_bottom(column_index, weight = 'thin')
  warn "[DEPRECATION] `#{__method__}` is deprecated.  Please use `change_column_border` instead."
  change_column_border(column_index, :bottom, weight)
end

#change_column_border_diagonal(column_index, weight = 'thin') ⇒ Object


575
576
577
578
# File 'lib/rubyXL/convenience_methods.rb', line 575

def change_column_border_diagonal(column_index, weight = 'thin')
  warn "[DEPRECATION] `#{__method__}` is deprecated.  Please use `change_column_border` instead."
  change_column_border(column_index, :diagonal, weight)
end

#change_column_border_left(column_index, weight = 'thin') ⇒ Object


560
561
562
563
# File 'lib/rubyXL/convenience_methods.rb', line 560

def change_column_border_left(column_index, weight = 'thin')
  warn "[DEPRECATION] `#{__method__}` is deprecated.  Please use `change_column_border` instead."
  change_column_border(column_index, :left, weight)
end

#change_column_border_right(column_index, weight = 'thin') ⇒ Object


565
566
567
568
# File 'lib/rubyXL/convenience_methods.rb', line 565

def change_column_border_right(column_index, weight = 'thin')
  warn "[DEPRECATION] `#{__method__}` is deprecated.  Please use `change_column_border` instead."
  change_column_border(column_index, :right, weight)
end

#change_column_border_top(column_index, weight = 'thin') ⇒ Object


555
556
557
558
# File 'lib/rubyXL/convenience_methods.rb', line 555

def change_column_border_top(column_index, weight = 'thin')
  warn "[DEPRECATION] `#{__method__}` is deprecated.  Please use `change_column_border` instead."
  change_column_border(column_index, :top, weight)
end

#change_column_font_color(column_index, font_color = '000000') ⇒ Object


510
511
512
513
514
515
516
517
# File 'lib/rubyXL/convenience_methods.rb', line 510

def change_column_font_color(column_index, font_color='000000')
  Color.validate_color(font_color)

  xf = get_col_xf(column_index)
  font = @workbook.fonts[xf.font_id].dup
  font.set_rgb_color(font_color)
  change_column_font(column_index, Worksheet::COLOR, font_color, font, xf)
end

#change_column_font_name(column_index = 0, font_name = 'Verdana') ⇒ Object


496
497
498
499
500
501
# File 'lib/rubyXL/convenience_methods.rb', line 496

def change_column_font_name(column_index = 0, font_name = 'Verdana')
  xf = get_col_xf(column_index)
  font = @workbook.fonts[xf.font_id].dup
  font.set_name(font_name)
  change_column_font(column_index, Worksheet::NAME, font_name, font, xf)
end

#change_column_font_size(column_index, font_size = 10) ⇒ Object


503
504
505
506
507
508
# File 'lib/rubyXL/convenience_methods.rb', line 503

def change_column_font_size(column_index, font_size=10)
  xf = get_col_xf(column_index)
  font = @workbook.fonts[xf.font_id].dup
  font.set_size(font_size)
  change_column_font(column_index, Worksheet::SIZE, font_size, font, xf)
end

#change_column_horizontal_alignment(column_index, alignment = 'center') ⇒ Object


547
548
549
# File 'lib/rubyXL/convenience_methods.rb', line 547

def change_column_horizontal_alignment(column_index, alignment = 'center')
  change_column_alignment(column_index) { |a| a.horizontal = alignment }
end

#change_column_italics(column_index, italicized = false) ⇒ Object


519
520
521
522
523
524
# File 'lib/rubyXL/convenience_methods.rb', line 519

def change_column_italics(column_index, italicized = false)
  xf = get_col_xf(column_index)
  font = @workbook.fonts[xf.font_id].dup
  font.set_italic(italicized)
  change_column_font(column_index, Worksheet::ITALICS, italicized, font, xf)
end

#change_column_strikethrough(column_index, struckthrough = false) ⇒ Object


540
541
542
543
544
545
# File 'lib/rubyXL/convenience_methods.rb', line 540

def change_column_strikethrough(column_index, struckthrough=false)
  xf = get_col_xf(column_index)
  font = @workbook.fonts[xf.font_id].dup
  font.set_strikethrough(struckthrough)
  change_column_font(column_index, Worksheet::STRIKETHROUGH, struckthrough, font, xf)
end

#change_column_underline(column_index, underlined = false) ⇒ Object


533
534
535
536
537
538
# File 'lib/rubyXL/convenience_methods.rb', line 533

def change_column_underline(column_index, underlined = false)
  xf = get_col_xf(column_index)
  font = @workbook.fonts[xf.font_id].dup
  font.set_underline(underlined)
  change_column_font(column_index, Worksheet::UNDERLINE, underlined, font, xf)
end

#change_column_vertical_alignment(column_index, alignment = 'center') ⇒ Object


551
552
553
# File 'lib/rubyXL/convenience_methods.rb', line 551

def change_column_vertical_alignment(column_index, alignment = 'center')
  change_column_alignment(column_index) { |a| a.vertical = alignment }
end

#change_row_alignment(row, &block) ⇒ Object


592
593
594
595
596
597
598
599
600
601
602
603
# File 'lib/rubyXL/convenience_methods.rb', line 592

def change_row_alignment(row, &block)
  validate_workbook
  validate_nonnegative(row)
  ensure_cell_exists(row)

  sheet_data.rows[row].style_index = @workbook.modify_alignment(get_row_style(row), &block)

  sheet_data[row].cells.each { |c|
    next if c.nil?
    c.style_index = @workbook.modify_alignment(c.style_index, &block)
  }
end

#change_row_bold(row = 0, bolded = false) ⇒ Object


466
467
468
469
470
471
# File 'lib/rubyXL/convenience_methods.rb', line 466

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(row, direction, weight) ⇒ Object


417
418
419
420
421
422
423
424
425
426
# File 'lib/rubyXL/convenience_methods.rb', line 417

def change_row_border(row, direction, weight)
  validate_workbook
  ensure_cell_exists(row)

  sheet_data.rows[row].style_index = @workbook.modify_border(get_row_style(row), direction, weight)

  sheet_data[row].cells.each { |c|
    c.change_border(direction, weight) unless c.nil?
  }
end

#change_row_border_bottom(row = 0, weight = 'thin') ⇒ Object


407
408
409
410
# File 'lib/rubyXL/convenience_methods.rb', line 407

def change_row_border_bottom(row = 0, weight = 'thin')
  warn "[DEPRECATION] `#{__method__}` is deprecated.  Please use `change_row_border` instead."
  change_row_border(row, :bottom, weight)
end

#change_row_border_diagonal(row = 0, weight = 'thin') ⇒ Object


412
413
414
415
# File 'lib/rubyXL/convenience_methods.rb', line 412

def change_row_border_diagonal(row = 0, weight = 'thin')
  warn "[DEPRECATION] `#{__method__}` is deprecated.  Please use `change_row_border` instead."
  change_row_border(row, :diagonal, weight)
end

#change_row_border_left(row = 0, weight = 'thin') ⇒ Object


397
398
399
400
# File 'lib/rubyXL/convenience_methods.rb', line 397

def change_row_border_left(row = 0, weight = 'thin')
  warn "[DEPRECATION] `#{__method__}` is deprecated.  Please use `change_row_border` instead."
  change_row_border(row, :left, weight)
end

#change_row_border_right(row = 0, weight = 'thin') ⇒ Object


402
403
404
405
# File 'lib/rubyXL/convenience_methods.rb', line 402

def change_row_border_right(row = 0, weight = 'thin')
  warn "[DEPRECATION] `#{__method__}` is deprecated.  Please use `change_row_border` instead."
  change_row_border(row, :right, weight)
end

#change_row_border_top(row = 0, weight = 'thin') ⇒ Object


392
393
394
395
# File 'lib/rubyXL/convenience_methods.rb', line 392

def change_row_border_top(row = 0, weight = 'thin')
  warn "[DEPRECATION] `#{__method__}` is deprecated.  Please use `change_row_border` instead."
  change_row_border(row, :top, weight)
end

#change_row_fill(row_index = 0, rgb = 'ffffff') ⇒ Object


428
429
430
431
432
433
434
435
# File 'lib/rubyXL/convenience_methods.rb', line 428

def change_row_fill(row_index = 0, rgb = 'ffffff')
  validate_workbook
  ensure_cell_exists(row_index)
  Color.validate_color(rgb)

  sheet_data.rows[row_index].style_index = @workbook.modify_fill(get_row_style(row_index), rgb)
  sheet_data[row_index].cells.each { |c| c.change_fill(rgb) unless c.nil? }
end

#change_row_font_color(row = 0, font_color = '000000') ⇒ Object


451
452
453
454
455
456
457
# File 'lib/rubyXL/convenience_methods.rb', line 451

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


437
438
439
440
441
442
# File 'lib/rubyXL/convenience_methods.rb', line 437

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


444
445
446
447
448
449
# File 'lib/rubyXL/convenience_methods.rb', line 444

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


487
488
489
490
491
492
493
494
# File 'lib/rubyXL/convenience_methods.rb', line 487

def change_row_height(row = 0, height = 10)
  validate_workbook
  ensure_cell_exists(row)

  c = sheet_data.rows[row]
  c.ht = height
  c.custom_height = true
end

#change_row_horizontal_alignment(row = 0, alignment = 'center') ⇒ Object


380
381
382
383
384
# File 'lib/rubyXL/convenience_methods.rb', line 380

def change_row_horizontal_alignment(row = 0, alignment = 'center')
  validate_workbook
  validate_nonnegative(row)
  change_row_alignment(row) { |a| a.horizontal = alignment }
end

#change_row_italics(row = 0, italicized = false) ⇒ Object


459
460
461
462
463
464
# File 'lib/rubyXL/convenience_methods.rb', line 459

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


480
481
482
483
484
485
# File 'lib/rubyXL/convenience_methods.rb', line 480

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


473
474
475
476
477
478
# File 'lib/rubyXL/convenience_methods.rb', line 473

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


386
387
388
389
390
# File 'lib/rubyXL/convenience_methods.rb', line 386

def change_row_vertical_alignment(row = 0, alignment = 'center')
  validate_workbook
  validate_nonnegative(row)
  change_row_alignment(row) { |a| a.vertical = alignment }
end

#get_column_alignment(col, type) ⇒ Object


337
338
339
340
341
342
343
# File 'lib/rubyXL/convenience_methods.rb', line 337

def get_column_alignment(col, type)
  validate_workbook
  validate_nonnegative(col)

  xf = @workbook.cell_xfs[get_cols_style_index(col)]
  xf.alignment && xf.alignment.send(type)
end

#get_column_border(col, border_direction) ⇒ Object


328
329
330
331
332
333
334
335
# File 'lib/rubyXL/convenience_methods.rb', line 328

def get_column_border(col, border_direction)
  validate_workbook
  validate_nonnegative(col)

  xf = @workbook.cell_xfs[get_cols_style_index(col)]
  border = @workbook.borders[xf.border_id]
  border && border.get_edge_style(border_direction)
end

#get_column_border_bottom(col = 0) ⇒ Object


370
371
372
373
# File 'lib/rubyXL/convenience_methods.rb', line 370

def get_column_border_bottom(col=0)
  warn "[DEPRECATION] `#{__method__}` is deprecated.  Please use `get_column_border` instead."
  get_column_border(col, :bottom)
end

#get_column_border_diagonal(col = 0) ⇒ Object


375
376
377
378
# File 'lib/rubyXL/convenience_methods.rb', line 375

def get_column_border_diagonal(col=0)
  warn "[DEPRECATION] `#{__method__}` is deprecated.  Please use `get_column_border` instead."
  get_column_border(col, :diagonal)
end

#get_column_border_left(col = 0) ⇒ Object


360
361
362
363
# File 'lib/rubyXL/convenience_methods.rb', line 360

def get_column_border_left(col=0)
  warn "[DEPRECATION] `#{__method__}` is deprecated.  Please use `get_column_border` instead."
  get_column_border(col, :left)
end

#get_column_border_right(col = 0) ⇒ Object


365
366
367
368
# File 'lib/rubyXL/convenience_methods.rb', line 365

def get_column_border_right(col=0)
  warn "[DEPRECATION] `#{__method__}` is deprecated.  Please use `get_column_border` instead."
  get_column_border(col, :right)
end

#get_column_border_top(col = 0) ⇒ Object


355
356
357
358
# File 'lib/rubyXL/convenience_methods.rb', line 355

def get_column_border_top(col=0)
  warn "[DEPRECATION] `#{__method__}` is deprecated.  Please use `get_column_border` instead."
  get_column_border(col, :top)
end

#get_column_fill(col = 0) ⇒ Object


321
322
323
324
325
326
# File 'lib/rubyXL/convenience_methods.rb', line 321

def get_column_fill(col=0)
  validate_workbook
  validate_nonnegative(col)

  @workbook.get_fill_color(get_col_xf(col))
end

#get_column_font_color(col = 0) ⇒ Object


279
280
281
282
# File 'lib/rubyXL/convenience_methods.rb', line 279

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


269
270
271
272
# File 'lib/rubyXL/convenience_methods.rb', line 269

def get_column_font_name(col = 0)
  font = column_font(col)
  font && font.get_name
end

#get_column_font_size(col = 0) ⇒ Object


274
275
276
277
# File 'lib/rubyXL/convenience_methods.rb', line 274

def get_column_font_size(col = 0)
  font = column_font(col)
  font && font.get_size
end

#get_column_horizontal_alignment(col = 0) ⇒ Object


345
346
347
348
# File 'lib/rubyXL/convenience_methods.rb', line 345

def get_column_horizontal_alignment(col=0)
  warn "[DEPRECATION] `#{__method__}` is deprecated.  Please use `get_column_alignment` instead."
  get_column_alignment(col, :horizontal)
end

#get_column_vertical_alignment(col = 0) ⇒ Object


350
351
352
353
# File 'lib/rubyXL/convenience_methods.rb', line 350

def get_column_vertical_alignment(col=0)
  warn "[DEPRECATION] `#{__method__}` is deprecated.  Please use `get_column_alignment` instead."
  get_column_alignment(col, :vertical)
end

#get_column_width(column_index = 0) ⇒ Object


315
316
317
318
319
# File 'lib/rubyXL/convenience_methods.rb', line 315

def get_column_width(column_index = 0)
  width = get_column_width_raw(column_index)
  return RubyXL::ColumnRange::DEFAULT_WIDTH if width.nil?
  (width - (5.0 / RubyXL::Font::MAX_DIGIT_WIDTH)).round
end

#get_column_width_raw(column_index = 0) ⇒ Object

Get raw column width value as stored in the file


305
306
307
308
309
310
311
# File 'lib/rubyXL/convenience_methods.rb', line 305

def get_column_width_raw(column_index = 0)
  validate_workbook
  validate_nonnegative(column_index)

  range = cols.locate_range(column_index)
  range && range.width
end

#get_row_border(row, border_direction) ⇒ Object


228
229
230
231
232
233
234
# File 'lib/rubyXL/convenience_methods.rb', line 228

def get_row_border(row, border_direction)
  validate_workbook
  validate_nonnegative(row)

  border = @workbook.borders[get_row_xf(row).border_id]
  border && border.get_edge_style(border_direction)
end

#get_row_border_bottom(row = 0) ⇒ Object


259
260
261
262
# File 'lib/rubyXL/convenience_methods.rb', line 259

def get_row_border_bottom(row = 0)
  warn "[DEPRECATION] `#{__method__}` is deprecated.  Please use `get_row_border` instead."
  return get_row_border(row, :bottom)
end

#get_row_border_diagonal(row = 0) ⇒ Object


264
265
266
267
# File 'lib/rubyXL/convenience_methods.rb', line 264

def get_row_border_diagonal(row = 0)
  warn "[DEPRECATION] `#{__method__}` is deprecated.  Please use `get_row_border` instead."
  return get_row_border(row, :diagonal)
end

#get_row_border_left(row = 0) ⇒ Object


249
250
251
252
# File 'lib/rubyXL/convenience_methods.rb', line 249

def get_row_border_left(row = 0)
  warn "[DEPRECATION] `#{__method__}` is deprecated.  Please use `get_row_border` instead."
  return get_row_border(row, :left)
end

#get_row_border_right(row = 0) ⇒ Object


254
255
256
257
# File 'lib/rubyXL/convenience_methods.rb', line 254

def get_row_border_right(row = 0)
  warn "[DEPRECATION] `#{__method__}` is deprecated.  Please use `get_row_border` instead."
  return get_row_border(row, :right)
end

#get_row_border_top(row = 0) ⇒ Object


244
245
246
247
# File 'lib/rubyXL/convenience_methods.rb', line 244

def get_row_border_top(row = 0)
  warn "[DEPRECATION] `#{__method__}` is deprecated.  Please use `get_row_border` instead."
  return get_row_border(row, :top)
end

#get_row_fill(row = 0) ⇒ Object


187
188
189
# File 'lib/rubyXL/convenience_methods.rb', line 187

def get_row_fill(row = 0)
  (row = sheet_data.rows[row]) && row.get_fill_color
end

#get_row_font_color(row = 0) ⇒ Object


199
200
201
202
203
# File 'lib/rubyXL/convenience_methods.rb', line 199

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


191
192
193
# File 'lib/rubyXL/convenience_methods.rb', line 191

def get_row_font_name(row = 0)
  (font = row_font(row)) && font.get_name
end

#get_row_font_size(row = 0) ⇒ Object


195
196
197
# File 'lib/rubyXL/convenience_methods.rb', line 195

def get_row_font_size(row = 0)
  (font = row_font(row)) && font.get_size
end

#get_row_height(row = 0) ⇒ Object


221
222
223
224
225
226
# File 'lib/rubyXL/convenience_methods.rb', line 221

def get_row_height(row = 0)
  validate_workbook
  validate_nonnegative(row)
  row = sheet_data.rows[row]
  row && row.ht || 13
end

#get_row_horizontal_alignment(row = 0) ⇒ Object


236
237
238
# File 'lib/rubyXL/convenience_methods.rb', line 236

def get_row_horizontal_alignment(row = 0)
  return get_row_alignment(row, true)
end

#get_row_vertical_alignment(row = 0) ⇒ Object


240
241
242
# File 'lib/rubyXL/convenience_methods.rb', line 240

def get_row_vertical_alignment(row = 0)
  return get_row_alignment(row, false)
end

#is_column_bolded(col = 0) ⇒ Object


289
290
291
292
# File 'lib/rubyXL/convenience_methods.rb', line 289

def is_column_bolded(col = 0)
  font = column_font(col)
  font && font.is_bold
end

#is_column_italicized(col = 0) ⇒ Object


284
285
286
287
# File 'lib/rubyXL/convenience_methods.rb', line 284

def is_column_italicized(col = 0)
  font = column_font(col)
  font && font.is_italic
end

#is_column_struckthrough(col = 0) ⇒ Object


299
300
301
302
# File 'lib/rubyXL/convenience_methods.rb', line 299

def is_column_struckthrough(col = 0)
  font = column_font(col)
  font && font.is_strikethrough
end

#is_column_underlined(col = 0) ⇒ Object


294
295
296
297
# File 'lib/rubyXL/convenience_methods.rb', line 294

def is_column_underlined(col = 0)
  font = column_font(col)
  font && font.is_underlined
end

#is_row_bolded(row = 0) ⇒ Object


209
210
211
# File 'lib/rubyXL/convenience_methods.rb', line 209

def is_row_bolded(row = 0)
  (font = row_font(row)) && font.is_bold
end

#is_row_italicized(row = 0) ⇒ Object


205
206
207
# File 'lib/rubyXL/convenience_methods.rb', line 205

def is_row_italicized(row = 0)
  (font = row_font(row)) && font.is_italic
end

#is_row_struckthrough(row = 0) ⇒ Object


217
218
219
# File 'lib/rubyXL/convenience_methods.rb', line 217

def is_row_struckthrough(row = 0)
  (font = row_font(row)) && font.is_strikethrough
end

#is_row_underlined(row = 0) ⇒ Object


213
214
215
# File 'lib/rubyXL/convenience_methods.rb', line 213

def is_row_underlined(row = 0)
  (font = row_font(row)) && font.is_underlined
end