Skip to content

Loading…

Variables in cells, or acts_as_template (feature request) #4

Closed
denispeplin opened this Issue · 2 comments

2 participants

@denispeplin

In my code I have this method:

  def self.xls(infile, outfile, var_hash)
    book = Spreadsheet.open infile
    sheet = book.worksheet 0

    sheet.each_with_index do |row, i|
      row.each_with_index do |cell, j|
        sheet[i, j] = cell.gsub(/\[[A-Z0-9_]+\]/) { |var| var_hash[ var.gsub!(/[\[\]]/, '').downcase.to_sym ] } if cell
      end
    end

    book.write outfile
    return outfile
  end

To use this method I must populate hash with variables and place this variables (hash keys) in square brackets into xls template.

var_hash[:name] = 'Bob'

and in xls [NAME] will be substituted by Bob.

So with this code spreadsheet gem acts like odf-report gem, and it is much easier for me to fill template with variables, than to count rows and cells, because documents that I use have complex structure with many merged cells. It is also easy to delegate task of template modification to somebody else.

Maybe it is nice feature for spreadsheet gem to have.

@zdavatz
Owner

good stuff! Please clone form my directory and send me a pull request please. Don't forget to write the test if you have time.

@zdavatz zdavatz closed this
@denispeplin

Sorry, have no time to complete this task. Maybe it is better to create short wiki article?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.