Skip to content

Commit

Permalink
Create to_xls method and route, successfully get a test xlsx file
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex committed Sep 5, 2023
1 parent 24e900e commit 4b01b2e
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 0 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ gem "term-ansicolor", "~> 1.3"
gem "terrapin", "~> 0.6.0"
gem "thor", "~> 1.0"
gem "twilio-ruby", "~> 4.2" # Does not use semver after v5, watch out!
gem "spreadsheet" # For XLSForm export

# JS/CSS
gem "bootstrap", "~> 4.3"
Expand Down
4 changes: 4 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,7 @@ GEM
ruby-jmeter (3.1.08)
nokogiri
rest-client
ruby-ole (1.2.12.2)
ruby-progressbar (1.11.0)
ruby-vips (2.1.4)
ffi (~> 1.12)
Expand Down Expand Up @@ -551,6 +552,8 @@ GEM
sexp_processor (4.16.1)
spinjs-rails (1.3)
rails (>= 3.1)
spreadsheet (1.3.0)
ruby-ole
sprockets (3.7.2)
concurrent-ruby (~> 1.0)
rack (> 1, < 3)
Expand Down Expand Up @@ -722,6 +725,7 @@ DEPENDENCIES
sentry-rails (~> 5.0)
sentry-ruby (~> 5.0)
spinjs-rails (~> 1.3.0)
spreadsheet
sprockets (~> 3)
sys-filesystem (~> 1.4)
term-ansicolor (~> 1.3)
Expand Down
7 changes: 7 additions & 0 deletions app/controllers/forms_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,13 @@ def export_xml
send_data(@form.odk_xml.download, filename: "form-#{@form.name.dasherize}-#{Time.zone.today}.xml")
end

# XLSForm export.
def export_xls
exporter = Forms::Export.new(@form)
send_data(exporter.to_xls, filename: "test.xlsx")
# send_file ...
end

# ODK XML export for all published forms.
# Theoretically works for standard forms too, but they have no XML so can't be exported at this time.
def export_all
Expand Down
1 change: 1 addition & 0 deletions app/decorators/action_links/form_link_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ def initialize(form)
unless new_action?
actions << [:export_csv, {url: h.export_form_path(form)}]
actions << [:export_xml, {url: h.export_xml_form_path(form)}] if form.odk_xml.attached?
actions << [:export_xls, {url: h.export_xls_form_path(form)}]
end

unless h.admin_mode?
Expand Down
32 changes: 32 additions & 0 deletions app/models/forms/export.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,43 @@ def to_csv
CSV.generate do |csv|
csv << COLUMNS
@form.preordered_items.each do |q|
Rails.logger.debug("*****************")
Rails.logger.debug(q)
csv << row(q)
end
end
end

def to_xls
book = Spreadsheet::Workbook.new

# Create sheets
questions = book.create_worksheet :name => "survey"
choices = book.create_worksheet :name => "choices"
settings = book.create_worksheet :name => "settings"

# Questions
questions.row(0).push "type", "name", "label", "required", "relevant"
questions.row(1).push "integer", "age", "How old are you?", "yes", ""
questions.row(2).push "select_one yes_no","likes_pizza", "Do you like pizza?", "", ""

# Choices
choices.row(0).push "list_name", "name", "label"
choices.row(1).push "yes_no", "yes"
choices.row(1).push "Yes" # Try adding onto the end of a row
choices.row(2).push "yes_no", "no", "No"

# Settings
settings.row(0).push "form_title", "form_id", "version", "default_language"
settings.row(1).push "Test Form", 1, Time.now.strftime("%Y%m%d"), "English (en)"

# Write
file = StringIO.new
book.write(file)

file.string.html_safe
end

private

def human_readable(klass, qing)
Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@
get "sms-guide", as: "sms_guide", action: "sms_guide"
get "export"
get "export_xml"
get "export_xls"
end
end

Expand Down

0 comments on commit 4b01b2e

Please sign in to comment.