lib/rspreadsheet/tools.rb in rspreadsheet-0.4.4 vs lib/rspreadsheet/tools.rb in rspreadsheet-0.4.5

- old
+ new

@@ -11,54 +11,53 @@ # converts cell adress like 'F12' to pair od integers [row,col] def self.convert_cell_address_to_coordinates(*addr) if addr.length == 1 addr[0].to_s.match(/^([A-Za-z]{1,3})(\d{1,8})$/) colname = $~[1] - rowname = $~[2].to_i + rowi = $~[2].to_i elsif addr.length == 2 a = addr[0]; b = addr[1] if a.kind_of?(Integer) and b.kind_of?(Integer) # most common case first - colname,rowname = b,a + colname,rowi = b,a elsif only_letters?(a) if kind_of_integer?(b) - colname,rowname = a,b.to_i + colname,rowi = a,b.to_i else raise 'Wrong parameters - first is letters, but the seconds is not digits only' end elsif kind_of_integer?(a) if only_letters?(b) - colname,rowname = b,a.to_i + colname,rowi = b,a.to_i elsif kind_of_integer?(b) - colname,rowname = b.to_i,a.to_i + colname,rowi = b.to_i,a.to_i else raise 'Wrong second out of two paremeters - mix of digits and numbers' end else raise 'Wrong first out of two paremeters - mix of digits and numbers' end else raise 'Wrong number of arguments' end + return [rowi,convert_column_name_to_index(colname)] + end + + def self.convert_column_name_to_index(colname) + return colname if colname.kind_of?(Integer) ## first possibility how to implement it # colname=colname.rjust(3,'@') - # col = (colname[-1].ord-64)+(colname[-2].ord-64)*26+(colname[-3].ord-64)*26*26 + # return (colname[-1].ord-64)+(colname[-2].ord-64)*26+(colname[-3].ord-64)*26*26 ## second possibility how to implement it - # col=(colname.to_i(36)-('A'*colname.size).to_i(36)).to_s(36).to_i(26)+('1'*colname.size).to_i(26) + # return (colname.to_i(36)-('A'*colname.size).to_i(36)).to_s(36).to_i(26)+('1'*colname.size).to_i(26) - if colname.kind_of?(Integer) - col=colname - else ## third possibility how to implement it (second one little shortened) - s=colname.size - col=(colname.upcase.to_i(36)-(36**s-1).div(3.5)).to_s(36).to_i(26)+(26**s-1)/25 - end - - row = rowname - return [row,col] + s=colname.size + return (colname.to_s.upcase.to_i(36)-(36**s-1).div(3.5)).to_s(36).to_i(26)+(26**s-1)/25 end + def self.convert_cell_coordinates_to_address(*coords) coords = coords[0] if coords.length == 1 raise 'Wrong number of arguments' if coords.length != 2 row = coords[0].to_i # security against string arguments col = coords[1].to_i @@ -198,6 +197,6 @@ class Range def size res = self.end-self.begin+1 res>0 ? res : 0 end -end \ No newline at end of file +end