module Style

Public Instance Methods

create_cell_style(workbook, options) click to toggle source
# File lib/style.rb, line 14
def create_cell_style workbook, options
  workbook_font = workbook.createFont
  style = workbook.createCellStyle
  options[:font_height] ? workbook_font.setFontHeightInPoints(options[:font_height]) : nil
  options[:font_color] && poi_color(options[:font_color]) ? workbook_font.setColor(poi_color(options[:font_color])) : nil
  options[:font_name] ? workbook_font.setFontName(options[:font_name]) : nil
  options[:bold] ? workbook_font.setBoldweight(workbook_font.BOLDWEIGHT_BOLD) : nil
  nil_out_runtime_error {options[:horizontal_alignment] ? 
                          style.setAlignment(excel_cell_style.send(options[:horizontal_alignment])) : nil}
  nil_out_runtime_error {options[:vertical_alignment] ? 
                          style.setVerticalAlignment(excel_cell_style.send(options[:vertical_alignment])) : nil}
  nil_out_runtime_error {options[:border_left] ? 
                          style.setBorderLeft(excel_cell_style.send(options[:border_left])) : nil}
  nil_out_runtime_error {options[:border_right] ? 
                          style.setBorderRight(excel_cell_style.send(options[:border_right])) : nil}
  nil_out_runtime_error {options[:border_top] ? 
                          style.setBorderTop(excel_cell_style.send(options[:border_top])) : nil}
  nil_out_runtime_error { options[:border_bottom] ? 
                          style.setBorderBottom(excel_cell_style.send(options[:border_bottom])) : nil }
  options[:border_left_color] && poi_color(options[:border_left_color]) ? 
                          style.setLeftBorderColor(poi_color options[:border_left_color]) : nil
  options[:border_right_color] && poi_color(options[:border_right_color]) ? 
                          style.setRightBorderColor(poi_color options[:border_right_color]) : nil
  options[:border_top_color] && poi_color(options[:border_top_color]) ? 
                          style.setTopBorderColor(poi_color options[:border_top_color]) : nil
  options[:border_bottom_color] && poi_color(options[:border_bottom_color]) ? 
                          style.setBottomBorderColor(poi_color options[:border_bottom_color]) : nil
  options[:wrap_text] ? style.setWrapText(options[:wrap_text]) : nil
  options[:indentation] ? style.setIndention(options[:indentation]) : nil

  if options[:border]
    nil_out_runtime_error {
                            style.setBorderBottom excel_cell_style.send(options[:border])
                            style.setBorderTop excel_cell_style.send(options[:border])
                            style.setBorderLeft excel_cell_style.send(options[:border])
                            style.setBorderRight excel_cell_style.send(options[:border])
    }
  end

  if options[:border_color] && poi_color(options[:border_color])
    style.setTopBorderColor poi_color(options[:border_color])
    style.setBottomBorderColor poi_color(options[:border_color])
    style.setLeftBorderColor poi_color(options[:border_color])
    style.setRightBorderColor poi_color(options[:border_color])
  end

  if options[:background_color] && poi_color(options[:background_color])
    style.setFillForegroundColor(poi_color(options[:background_color])) 
    style.setFillPattern 1 
  end
  
  style.setFont workbook_font
  # next three lines are a hack to test fonts due to a rjb bug
  def style.set_font(font); @font = font; end;
  def style.get_font; @font; end;
  style.set_font workbook_font
  style
end
excel_cell_style() click to toggle source
# File lib/style.rb, line 6
def excel_cell_style
  Rjb::import('org.apache.poi.ss.usermodel.CellStyle')
end
hssf_cell_style() click to toggle source
# File lib/style.rb, line 10
def hssf_cell_style
  Rjb::import('org.apache.poi.hssf.usermodel.HSSFCellStyle')
end
poi_color(color) click to toggle source
# File lib/style.rb, line 2
def poi_color color
  nil_out_runtime_error {Rjb::import('org.apache.poi.ss.usermodel.IndexedColors').send(color).getIndex}
end

Private Instance Methods

nil_out_runtime_error(&block) click to toggle source
# File lib/style.rb, line 74
def nil_out_runtime_error(&block)
  begin
    block.call
  rescue RuntimeError
    nil
  end
end