Search code examples
ruby-on-railsrubyaxlsx

Incrementing Letters in ruby


I'm working with axlsx in rails and I want to be able to set up the columns according to the number of elements in a collection:

In my controller:

 @task_categories = TaskCategories.all

In my xlsx.axlsx view I would like to do something like:

 sheet.merge_cells("I2:#{'I'.next(@task_categories.count)}2")

but I get a no explicit conversion to Array error.

I can write a helper that loops the .next by count, but it seems that this is a common enough issue that there should be a built-in short-cut that I'm just missing.

EDIT:

I wrote a helper that works:

def increment_letter(l, c)
  (1..c).map { l = l.next }
  l
end

with the call:

 sheet.merge_cells("I2:#{increment_letter('I', @task_categories.count)}2")

But I am still looking for The Rails Way™.


Solution

  • This looks like a quoting problem

    "I2:#{'I'.next(@task_categories.count)}2"
    

    Note my use of single quotes ' around your I

    Otherwise you could also use %Q{...} if that looks more readable to you

    %Q{I2:#{"I".next(@task_categories.count)}2}
    

    Lastly, String#next does not take arguments, so you'll end up with something like this

    %Q{I2:#{"I".next}2} #=> "I2:J2"