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

Closed
denispeplin opened this Issue Jun 6, 2012 · 2 comments

Comments

Projects
None yet
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

This comment has been minimized.

Show comment
Hide comment
@zdavatz

zdavatz Jun 7, 2012

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.

Owner

zdavatz commented Jun 7, 2012

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 Oct 10, 2012

@denispeplin

This comment has been minimized.

Show comment
Hide comment
@denispeplin

denispeplin Nov 15, 2012

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

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