axlsx icon indicating copy to clipboard operation
axlsx copied to clipboard

Getting full cell range of sheet

Open arashb31 opened this issue 8 years ago • 2 comments

For a sheet, how can we get the full range of a sheet?

In other words, how do we get the left-top-most cell (presumably A1) and the right-bottom-most cell (e.g. G10).

Basically, I'd like to get a string back like "A1:G10"

I'd like to be able to easily add an autofilter to an entire sheet (which is tabular data).

Thanks,

arashb31 avatar Dec 27 '16 18:12 arashb31

I believe there is no nice way to get the right-bottom cell.
I've done this in a completely ugly way which is ....

def right_bottom_ref(cols,rows)                                                                                                                                              
  row_ref = rows.to_s                                                                                                                                                        
  col_ref = 'A'                                                                                                                                                              
  (cols-1).times{col_ref.next!}                                                                                                                                              
  return "A1:#{col_ref}#{row_ref}"                                                                                                                                           
end                                                                                                                                                                          
                                                                                                                                                                             
p.workbook.add_worksheet(:name => 'sample') do |sheet|                                                                                                                       
  # add some rows                                                                                                                                                            
  ref = right_bottom_ref sheet.cols.length, sheet.rows.length                                                                                                                
  sheet.add_table(ref, :name => 'sample', :style_info => { :name => "TableStyleMedium21" })                                                                                  
end                                                                                                                                                                          

I'd like to make something like "Worksheet#right_bottom" if feasible.

sato-s avatar Feb 07 '17 16:02 sato-s

What about the following, assuming sheet is a Worksheet?

'A1:' + sheet.rows[-1].cells[-1].reference

hendrikgit avatar Nov 03 '20 15:11 hendrikgit