survey/app/views/survey_export/export.xlsx.axlsx

56 lines
1.8 KiB
Plaintext

# encoding: utf-8
require 'axlsx'
wb = xlsx_package.workbook
title = survey.title
title = "WorkSheet1" if title.blank?
wb.add_worksheet(name: title) do |sheet|
row = [I18n.t("survey.taken_by"), I18n.t("survey.taken_date")]
row2 = ["", ""]
if survey.enable_consent_feature
row << survey.consent_contents
row2 << ""
end
row << I18n.t("survey.type.0")
row2 << ""
survey_questions.each_with_index do |question, i|
qnum = question.title.match(/^\d+./).nil? ? "#{i+1}. " : ""
start_col = row2.count
if question.type == 2 or question.type == 3 or question.type == 4
col_size = question.survey_question_options.count - 1
sheet.merge_cells "#{Axlsx::cell_r(start_col,0)}:#{Axlsx::cell_r(start_col+col_size,0)}"
question.survey_question_options.each do |option|
row << "#{qnum}#{question.title}"
row2 << "#{option.name}"
end
elsif question.type == 5
col_size = question.survey_question_options.count * question.survey_question_radiogroups.count - 1
sheet.merge_cells "#{Axlsx::cell_r(start_col,0)}:#{Axlsx::cell_r(start_col+col_size,0)}"
question.survey_question_options.each do |option|
question.survey_question_radiogroups.each do |radiogroup|
row << "#{qnum}#{question.title}"
row2 << "#{option.name} - #{radiogroup.name}"
end
end
else
row << "#{qnum}#{question.title}"
row2 << ""
end
if question.custom_option_new_option
row << "#{qnum}#{question.title}"
row2 << "#{t('survey_question.use_custom_option')}"
end
end
sheet.add_row row
sheet.add_row row2
wrap = wb.styles.add_style alignment: {wrap_text: true}
survey_answers.each do |answer|
answer_row = []
sheet.add_row answer.values.flatten, style: wrap
end
end