survey/app/views/admin/surveys/export.xlsx.axlsx

96 lines
2.6 KiB
Plaintext
Raw Normal View History

2014-10-06 05:55:25 +00:00
# encoding: utf-8
wb = xlsx_package.workbook
2016-12-09 11:42:26 +00:00
wb.add_worksheet(name: remove_illegal_utf8(@survey.title[0..15])) do |sheet|
2014-10-06 05:55:25 +00:00
row = []
@survey_questions.each_with_index do |question, i|
if question.type == 2 or question.type == 3 or question.type == 4
question.survey_question_options.each do |option|
2016-12-09 11:42:26 +00:00
row << "#{i+1}. #{remove_illegal_utf8 question.title} - #{remove_illegal_utf8 option.name}"
2014-10-06 05:55:25 +00:00
end
elsif question.type == 5
question.survey_question_options.each do |option|
question.survey_question_radiogroups.each do |radiogroup|
2016-12-09 11:42:26 +00:00
row << "#{i+1}. #{remove_illegal_utf8 question.title} - #{remove_illegal_utf8 option.name} - #{remove_illegal_utf8 radiogroup.name}"
2014-10-06 05:55:25 +00:00
end
end
else
2016-12-09 11:42:26 +00:00
row << "#{i+1}. #{remove_illegal_utf8 question.title}"
2014-10-06 05:55:25 +00:00
end
if question.custom_option
2016-12-09 11:42:26 +00:00
row << "#{i+1}. #{remove_illegal_utf8 question.title} - #{t('survey_question.use_custom_option')}"
2014-10-06 05:55:25 +00:00
end
end
sheet.add_row row
@survey_answers.each do |answer|
answer_row = []
@survey_questions.each do |question|
options = question.survey_question_options.collect{|o| o.name }
if question.type == 2 or question.type == 4
options.collect do |o|
if !answer[question.id.to_s].blank? && answer[question.id.to_s].include?(o)
answer_row << '1'
else
answer_row << ''
end
end
elsif question.type == 3
options.collect do |o|
if !answer[question.id.to_s].blank? && answer[question.id.to_s].include?(o)
answer_row << '1'
else
answer_row << ''
end
end
elsif question.type == 5
question.survey_question_options.each do |option|
question.survey_question_radiogroups.each do |radiogroup|
if !answer[question.id.to_s].blank? && answer[question.id.to_s][option.id.to_s] == radiogroup.name
answer_row << '1'
else
answer_row << ''
end
end
end
else
if answer[question.id.to_s]
answer_row << answer[question.id.to_s]
else
answer_row << ''
end
end
if question.custom_option
if !answer[question.id.to_s].blank?
if answer[question.id.to_s].class == Array
answer_row << (answer[question.id.to_s] - options).join
elsif answer[question.id.to_s].class == String
answer_row << ([answer[question.id.to_s]] - options).join
end
else
answer_row << ''
end
end
end
sheet.add_row answer_row
end
end