Module: Style

Included in:
POIPond
Defined in:
lib/style.rb

Instance Method Summary collapse

Instance Method Details

#create_cell_style(workbook, options) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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
# 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_styleObject



6
7
8
# File 'lib/style.rb', line 6

def excel_cell_style
  Rjb::import('org.apache.poi.ss.usermodel.CellStyle')
end

#hssf_cell_styleObject



10
11
12
# File 'lib/style.rb', line 10

def hssf_cell_style
  Rjb::import('org.apache.poi.hssf.usermodel.HSSFCellStyle')
end

#poi_color(color) ⇒ Object



2
3
4
# 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