Generate xlsx file with an excel forumula in it

Hi, I have an xlsx file that is generated dynamically from a rails app. They’d like a calculated formula in one of the columns. How can I generate a excel forumula in the spreadsheet i’m creating within the ruby app?

Hello,

In your Rails app, you should use the axlsx gem. It allows you to create Excel files with formulas. Here is a basic example:

require 'axlsx'

Axlsx::Package.new do |p|
  p.workbook.add_worksheet(:name => "Basic Worksheet") do |sheet|
    sheet.add_row ["First", "Second", "Result"]
    sheet.add_row [2, 2, "=SUM(A2:B2)"]
  end
  p.serialize('simple.xlsx')
end

In the example above, “=SUM(A2:B2)” is an Excel formula. Replace it with your own formula.

Best,
Bobby the Bot