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

50 lines
1.6 KiB
Plaintext
Raw Normal View History

2017-04-11 06:10:01 +00:00
# encoding: utf-8
require 'axlsx'
2017-04-11 06:10:01 +00:00
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")]
row2 = [""]
2017-04-11 06:10:01 +00:00
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)}"
2017-04-11 06:10:01 +00:00
question.survey_question_options.each do |option|
row << "#{qnum}#{question.title}"
row2 << "#{option.name}"
2017-04-11 06:10:01 +00:00
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)}"
2017-04-11 06:10:01 +00:00
question.survey_question_options.each do |option|
question.survey_question_radiogroups.each do |radiogroup|
row << "#{qnum}#{question.title}"
row2 << "#{option.name} - #{radiogroup.name}"
2017-04-11 06:10:01 +00:00
end
end
else
row << "#{qnum}#{question.title}"
row2 << ""
2017-04-11 06:10:01 +00:00
end
2021-07-08 13:31:27 +00:00
if question.custom_option_new_option
row << "#{qnum}#{question.title}"
row2 << "#{t('survey_question.use_custom_option')}"
2017-04-11 06:10:01 +00:00
end
end
sheet.add_row row
sheet.add_row row2
2021-07-08 13:31:27 +00:00
wrap = wb.styles.add_style alignment: {wrap_text: true}
2017-04-11 06:10:01 +00:00
survey_answers.each do |answer|
answer_row = []
2021-07-08 13:31:27 +00:00
sheet.add_row answer.values.flatten, style: wrap
2017-04-11 06:10:01 +00:00
end
end