Class: ExcelWps::Row
- Inherits:
-
Object
- Object
- ExcelWps::Row
- Defined in:
- lib/excel_wps.rb
Overview
SheetWork Class
Constant Summary collapse
- FILL_TYPE =
4- @@cell_map =
("A".."Z").to_a
Instance Attribute Summary collapse
-
#row_id ⇒ Object
readonly
Returns the value of attribute row_id.
Instance Method Summary collapse
-
#<<(arr) ⇒ Object
特别注意,由于Toolkit中加入了Array,String的模块 所以判断的时候特别注意要是.
-
#add_cell(value, auto_fit = false, style = "NormalStyle") ⇒ Object
增加单元格.
-
#cell_name(index) ⇒ Object
获得单元格的名字(通过索引).
-
#cell_name!(letter) ⇒ Object
获得单元格的名字(通过字母).
-
#curent_cell ⇒ Object
此时的单元格.
-
#first_cell ⇒ Object
第一个单元格.
-
#height=(height) ⇒ Object
设置行高.
-
#initialize(worksheet, row_id) ⇒ Row
constructor
A new instance of Row.
-
#merge(idx_begin, idx_end) ⇒ Object
合并一行中的单元格.
-
#pagebreak ⇒ Object
对此时的行的下方下分页符.
-
#real_row ⇒ Object
返回此时的::Row类.
- #set_cell(index, value, auto_fit = false, style = "NormalStyle") ⇒ Object
-
#set_style(letter, style = "NormalStyle") ⇒ Object
(also: #style)
设置单元格风格.
-
#to_letter(index) ⇒ Object
通过索引变成对应的字母.
-
#wraptext(letter) ⇒ Object
开启自动换行.
Constructor Details
#initialize(worksheet, row_id) ⇒ Row
Returns a new instance of Row.
197 198 199 200 201 202 |
# File 'lib/excel_wps.rb', line 197 def initialize(worksheet, row_id) @row_id = row_id @cell_count = 0 @worksheet = worksheet @nil_space = [] end |
Instance Attribute Details
#row_id ⇒ Object (readonly)
Returns the value of attribute row_id.
194 195 196 |
# File 'lib/excel_wps.rb', line 194 def row_id @row_id end |
Instance Method Details
#<<(arr) ⇒ Object
特别注意,由于Toolkit中加入了Array,String的模块所以判断的时候特别注意要是
242 243 244 245 246 247 248 249 250 251 |
# File 'lib/excel_wps.rb', line 242 def << (arr) case arr when ::Array arr.size.times do |t| add_cell(arr[t]) end when ::String add_cell(arr) end end |
#add_cell(value, auto_fit = false, style = "NormalStyle") ⇒ Object
增加单元格
220 221 222 223 224 225 226 227 228 229 230 231 232 233 |
# File 'lib/excel_wps.rb', line 220 def add_cell(value, auto_fit = false, style = "NormalStyle") range = @worksheet.Range(cell_name(@cell_count)) range.Value = value.to_s range.Style = style range.Columns.AutoFit if auto_fit @cell_count += 1 while(@nil_space.include?(to_letter(@cell_count))) do range = @worksheet.Range(cell_name(@cell_count)) range.Value = "" range.Style = style @cell_count += 1 end end |
#cell_name(index) ⇒ Object
获得单元格的名字(通过索引)
254 255 256 257 258 259 260 261 262 |
# File 'lib/excel_wps.rb', line 254 def cell_name(index) second = index % 26 first = (index - second) / 26 if first == 0 return @@cell_map[second] + @row_id.to_s end first -= 1 return @@cell_map[first] + @@cell_map[second] + @row_id.to_s end |
#cell_name!(letter) ⇒ Object
获得单元格的名字(通过字母)
265 266 267 |
# File 'lib/excel_wps.rb', line 265 def cell_name!(letter) return letter + @row_id.to_s end |
#curent_cell ⇒ Object
此时的单元格
205 206 207 |
# File 'lib/excel_wps.rb', line 205 def curent_cell return cell_name(@cell_count) end |
#first_cell ⇒ Object
第一个单元格
210 211 212 |
# File 'lib/excel_wps.rb', line 210 def first_cell return cell_name(0) end |
#height=(height) ⇒ Object
设置行高
215 216 217 |
# File 'lib/excel_wps.rb', line 215 def height=(height) @worksheet.rows(@row_id).RowHeight = height end |
#merge(idx_begin, idx_end) ⇒ Object
合并一行中的单元格
283 284 285 286 287 288 289 290 291 |
# File 'lib/excel_wps.rb', line 283 def merge(idx_begin, idx_end) cell_begin = "#{idx_begin}#{@row_id}" cell_end = "#{idx_end}#{@row_id}" range = @worksheet.Range("#{cell_begin}:#{cell_end}") range.merge tmp = ((idx_begin.upcase)..(idx_end.upcase)).to_a tmp.shift @nil_space = (@nil_space | tmp).sort end |
#pagebreak ⇒ Object
对此时的行的下方下分页符
300 301 302 |
# File 'lib/excel_wps.rb', line 300 def pagebreak @worksheet.rows(@row_id+1).PageBreak = 1 end |
#real_row ⇒ Object
返回此时的::Row类
305 306 307 |
# File 'lib/excel_wps.rb', line 305 def real_row @worksheet.rows(@row_id) end |
#set_cell(index, value, auto_fit = false, style = "NormalStyle") ⇒ Object
269 270 271 272 273 274 |
# File 'lib/excel_wps.rb', line 269 def set_cell(index, value, auto_fit = false, style = "NormalStyle") range = @worksheet.Range(cell_name(index)) range.Value = value.to_s range.Style = style range.Columns.AutoFit if auto_fit end |
#set_style(letter, style = "NormalStyle") ⇒ Object Also known as: style
设置单元格风格
277 278 279 280 |
# File 'lib/excel_wps.rb', line 277 def set_style(letter, style = "NormalStyle") range = @worksheet.range(cell_name!(letter)) range.Style = style end |
#to_letter(index) ⇒ Object
通过索引变成对应的字母
236 237 238 |
# File 'lib/excel_wps.rb', line 236 def to_letter(index) @@cell_map.at(index) end |
#wraptext(letter) ⇒ Object
开启自动换行
294 295 296 297 |
# File 'lib/excel_wps.rb', line 294 def wraptext(letter) range = @worksheet.range(cell_name!(letter)) range.WrapText = true end |