Class: ExcelOffice::Row

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

Constant Summary collapse

FILL_TYPE =
4
@@cell_map =
("A".."Z").to_a

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(worksheet, row_id) ⇒ Row

Returns a new instance of Row.



198
199
200
201
202
203
204
# File 'lib/excel_office.rb', line 198

def initialize(worksheet, row_id)
	@row_id = row_id
	@cell_count = 0
	@worksheet = worksheet
	@nil_space = []
	@merge_cell = []
end

Instance Attribute Details

#row_idObject (readonly)

Returns the value of attribute row_id.



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

def row_id
  @row_id
end

Instance Method Details

#<<(arr) ⇒ Object

特别注意,由于Toolkit中加入了Array,String的模块所以判断的时候特别注意要是



252
253
254
255
256
257
258
259
260
261
# File 'lib/excel_office.rb', line 252

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

增加单元格



226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
# File 'lib/excel_office.rb', line 226

def add_cell(value, auto_fit = false, style = "NormalStyle")
	range = @worksheet.Range(cell_name(@cell_count))
	begin
		range.Value = value.to_s
		range.Style = style
	rescue Exception
		retry
	end
	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

获得单元格的名字(通过索引)



264
265
266
267
268
269
270
271
272
# File 'lib/excel_office.rb', line 264

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

获得单元格的名字(通过字母)



275
276
277
# File 'lib/excel_office.rb', line 275

def cell_name!(letter)
	return letter + @row_id.to_s
end

#curent_cellObject

此时的单元格



207
208
209
# File 'lib/excel_office.rb', line 207

def curent_cell
	return cell_name(@cell_count)
end

#first_cellObject

第一个单元格



212
213
214
# File 'lib/excel_office.rb', line 212

def first_cell
	return cell_name(0)
end

#height=(height) ⇒ Object

设置行高



217
218
219
220
221
222
223
# File 'lib/excel_office.rb', line 217

def height=(height)
	begin
		@worksheet.rows(@row_id).RowHeight = height
	rescue Exception
		retry
	end
end

#merge(idx_begin, idx_end) ⇒ Object

合并一行中的单元格, 这里仅仅是记录



293
294
295
296
297
298
299
300
# File 'lib/excel_office.rb', line 293

def merge(idx_begin, idx_end)
	cell_begin = "#{idx_begin}#{@row_id}"
	cell_end = "#{idx_end}#{@row_id}"
	@merge_cell << [cell_begin, cell_end]
	tmp = ((idx_begin.upcase)..(idx_end.upcase)).to_a
	tmp.shift
	@nil_space = (@nil_space | tmp).sort
end

#merge!Object



302
303
304
305
306
307
308
# File 'lib/excel_office.rb', line 302

def merge!
	return if @merge_cell.empty?
	@merge_cell.each do |cell_begin, cell_end|
		range = @worksheet.Range("#{cell_begin}:#{cell_end}")
		range.merge
	end
end

#pagebreakObject

对此时的行的下方下分页符



317
318
319
320
321
322
323
# File 'lib/excel_office.rb', line 317

def pagebreak
	begin
		@worksheet.rows(@row_id+1).PageBreak = 1
	rescue Exception
		retry
	end
end

#real_rowObject

返回此时的::Row类



326
327
328
329
# File 'lib/excel_office.rb', line 326

def real_row
	#@worksheet.rows(@row_id)
	@worksheet.range("A#{@row_id}:#{cell_name(@cell_count - 1)}")
end

#set_cell(index, value, auto_fit = false, style = "NormalStyle") ⇒ Object



279
280
281
282
283
284
# File 'lib/excel_office.rb', line 279

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

设置单元格风格



287
288
289
290
# File 'lib/excel_office.rb', line 287

def set_style(letter, style = "NormalStyle")
	range = @worksheet.range(cell_name!(letter))
	range.Style = style
end

#to_letter(index) ⇒ Object

通过索引变成对应的字母



246
247
248
# File 'lib/excel_office.rb', line 246

def to_letter(index)
	@@cell_map.at(index)
end

#wraptext(letter) ⇒ Object

开启自动换行



311
312
313
314
# File 'lib/excel_office.rb', line 311

def wraptext(letter)
	range = @worksheet.range(cell_name!(letter))
	range.WrapText = true
end