Class: Worksheet

Inherits:
Object
  • Object
show all
Defined in:
lib/jruby_excelcom/worksheet.rb

Instance Method Summary collapse

Constructor Details

#initialize(java_ws) ⇒ Worksheet

Returns a new instance of Worksheet.



5
6
7
# File 'lib/jruby_excelcom/worksheet.rb', line 5

def initialize(java_ws)
  @ws = java_ws
end

Instance Method Details

#border_color(range) ⇒ Object Also known as: getBorderColor

gets the border color of cells in range. Throws a NullpointerException if range contains multiple colors



129
130
131
# File 'lib/jruby_excelcom/worksheet.rb', line 129

def border_color(range)
  @ws.getBorderColor(range)
end

#border_color=(hash) ⇒ Object

sets border color of cells in range

hash

must contain :range and :color

Raises:

  • (ArgumentError)


122
123
124
125
126
# File 'lib/jruby_excelcom/worksheet.rb', line 122

def border_color=(hash)
  raise ArgumentError, 'cannot set border color, argument is not a hash' unless hash.is_a? Hash
  raise ArgumentError, 'cannot set border color, hash does not contain :range or :color key' if hash[:range].nil? or hash[:color].nil?
  set_border_color hash[:range], hash[:color]
end

#comment(range) ⇒ Object Also known as: getComment

gets the comment of cells in range. Throws a NullpointerException if range contains multiple comments



151
152
153
# File 'lib/jruby_excelcom/worksheet.rb', line 151

def comment(range)
  @ws.getComment(range)
end

#comment=(hash) ⇒ Object

sets comment of cells in range

hash

must contain :range and :comment

Raises:

  • (ArgumentError)


144
145
146
147
148
# File 'lib/jruby_excelcom/worksheet.rb', line 144

def comment=(hash)
  raise ArgumentError, 'cannot set border color, argument is not a hash' unless hash.is_a? Hash
  raise ArgumentError, 'cannot set border color, hash does not contain :range or :comment key' if hash[:range].nil? or hash[:comment].nil?
  set_comment hash[:range], hash[:comment]
end

#content(range = 'UsedRange') ⇒ Object Also known as: getContent

returns the content in range as a matrix, a vector or a single value, depending on range‘s dimensions

range

range with content to get, default value is UsedRange



28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/jruby_excelcom/worksheet.rb', line 28

def content(range = 'UsedRange')
  c = @ws.getContent(range).to_a.map!{ |row| row.to_a }
  columns = c.size
  rows = columns > 0 ? c[0].size : 0
  if columns == 1 and rows == 1 # range is one cell
    c[0][0].is_a?(Java::JavaUtil::Date) ? Time.at(c[0][0].getTime/1000) : c[0][0]
  elsif (columns > 1 and rows == 1) or (columns == 1 and rows > 1) # range is one column or row
      c.flatten.map!{|cell| cell.is_a?(Java::JavaUtil::Date) ? Time.at(cell.getTime/1000) : cell }
  else # range is a matrix
    c.map!{|row| row.map!{|cell| cell.is_a?(Java::JavaUtil::Date) ? Time.at(cell.getTime/1000) : cell }}
  end
end

#content=(hash) ⇒ Object

sets content in a range

hash

must contain :range and :content, e.g. {:range => 'A1:A3', :content => [1,2,3]}. Otherwise an ArgumentError is raised

Raises:

  • (ArgumentError)


62
63
64
65
66
# File 'lib/jruby_excelcom/worksheet.rb', line 62

def content=(hash)
  raise ArgumentError, 'cannot set content, argument is not a hash' unless hash.is_a? Hash
  raise ArgumentError, 'cannot set content, hash does not contain :range or :content key' if hash[:range].nil? or hash[:content].nil?
  set_content hash[:range], hash[:content]
end

#deleteObject

deletes this worksheet



22
23
24
# File 'lib/jruby_excelcom/worksheet.rb', line 22

def delete
  @ws.delete
end

#fill_color(range) ⇒ Object Also known as: getFillColor

gets the fill color of cells in range. Throws a NullpointerException if range contains multiple colors



85
86
87
# File 'lib/jruby_excelcom/worksheet.rb', line 85

def fill_color(range)
  @ws.getFillColor(range)
end

#fill_color=(hash) ⇒ Object

fills cells in range with color

hash

must contain :range and :color

Raises:

  • (ArgumentError)


78
79
80
81
82
# File 'lib/jruby_excelcom/worksheet.rb', line 78

def fill_color=(hash)
  raise ArgumentError, 'cannot set fill color, argument is not a hash' unless hash.is_a? Hash
  raise ArgumentError, 'cannot set fill color, hash does not contain :range or :color key' if hash[:range].nil? or hash[:color].nil?
  set_fill_color hash[:range], hash[:color]
end

#font_color(range) ⇒ Object Also known as: getFontColor

gets the font color of cells in range. Throws a NullpointerException if range contains multiple colors



107
108
109
# File 'lib/jruby_excelcom/worksheet.rb', line 107

def font_color(range)
  @ws.getFontColor(range)
end

#font_color=(hash) ⇒ Object

sets font color of cells in range

hash

must contain :range and :color

Raises:

  • (ArgumentError)


100
101
102
103
104
# File 'lib/jruby_excelcom/worksheet.rb', line 100

def font_color=(hash)
  raise ArgumentError, 'cannot set font color, argument is not a hash' unless hash.is_a? Hash
  raise ArgumentError, 'cannot set font color, hash does not contain :range or :color key' if hash[:range].nil? or hash[:color].nil?
  set_font_color hash[:range], hash[:color]
end

#nameObject Also known as: getName

gets the name of this worksheet



16
17
18
# File 'lib/jruby_excelcom/worksheet.rb', line 16

def name
  @ws.getName
end

#name=(name) ⇒ Object Also known as: setName

sets the name of this worksheet



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

def name=(name)
  @ws.setName(name)
end

#set_border_color(range, color) ⇒ Object Also known as: setBorderColor

sets border color of cells in range

range

range to be colorized

color

color to be used, must be an ExcelColor, e.g. ExcelColor::RED



115
116
117
# File 'lib/jruby_excelcom/worksheet.rb', line 115

def set_border_color(range, color)
  @ws.setBorderColor(range, color)
end

#set_comment(range, comment) ⇒ Object Also known as: setComment

sets comment of cells in range

range

range

comment

comment text



137
138
139
# File 'lib/jruby_excelcom/worksheet.rb', line 137

def set_comment(range, comment)
  @ws.setComment(range, comment)
end

#set_content(range, content) ⇒ Object Also known as: setContent

sets content in a range

range

range in worksheet, e.g. ‘A1:B3’

content

may be a matrix, a vector or a single value. If it’s a matrix or vector, its dimensions must be equal to range‘s dimensions



45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/jruby_excelcom/worksheet.rb', line 45

def set_content(range, content)
  if content.is_a?(Array)
    if content[0].is_a?(Array) # content is a matrix
      @ws.java_send :setContent, [java.lang.String, java.lang.Object[][]], range, content
    elsif JavaExcelcom::Util::getRangeSize(range)[0] == 1 # content is a row
      @ws.java_send :setContent, [java.lang.String, java.lang.Object[][]], range, [content]
    else # content is a column
      @ws.java_send :setContent, [java.lang.String, java.lang.Object[][]], range, content.map{|cell| [cell] }
    end
  else # content is a single value
    @ws.java_send :setContent, [java.lang.String, java.lang.Object], range, content
  end
end

#set_fill_color(range, color) ⇒ Object Also known as: setFillColor

fills cells in range with color

range

range to be colorized

color

color to be used, must be an ExcelColor, e.g. ExcelColor::RED



71
72
73
# File 'lib/jruby_excelcom/worksheet.rb', line 71

def set_fill_color(range, color)
  @ws.setFillColor(range, color)
end

#set_font_color(range, color) ⇒ Object Also known as: setFontColor

sets font color of cells in range

range

range to be colorized

color

color to be used, must be an ExcelColor, e.g. ExcelColor::RED



93
94
95
# File 'lib/jruby_excelcom/worksheet.rb', line 93

def set_font_color(range, color)
  @ws.setFontColor(range, color)
end