Skip to content

Instantly share code, notes, and snippets.

@vontell068
Created June 22, 2018 08:32
Show Gist options
  • Save vontell068/0140f01becb57b0f3c60c1ec37d85a4e to your computer and use it in GitHub Desktop.
Save vontell068/0140f01becb57b0f3c60c1ec37d85a4e to your computer and use it in GitHub Desktop.
test
module ExportServices
class QuestionnaireResponses
SIMPLE_QUESTION_HEADERS = [
"选项",
"值",
"提交人"
].freeze
DORPDOWN_GIRD_QUESTION_HEADERS = [
"行",
"列",
"选项",
"值",
"提交人"
].freeze
MULTIPLE_CHOICE_GIRD_QUESTION_HEADERS = [
"行",
"列",
"值",
"提交人"
].freeze
def initialize(questionnaire:)
@questionniare = questionnaire
@workbook = RubyXL::Workbook.new
@workbook.worksheets.clear
end
def execute
render_workbook
DownloadService.execute(@workbook, "#{@questionniare.title}_#{Time.now.strftime('%Y-%m-%d')}.xlsx")
end
private
def render_workbook
@questionniare.questions.each do |question|
render_worksheet(worksheet: @workbook.add_worksheet(question.title), question: question)
end
end
def render_worksheet(worksheet:, question:)
render_worksheet_header(worksheet: worksheet, header_type: question.question_type)
index = 1
question.answers.limit(255).each do |answer|
result = query answer
next if result.nil?
if question.question_type == "单选题"
worksheet.add_cell(index, 0, result[0])
worksheet.add_cell(index, 1, result[1])
worksheet.add_cell(index, 2, result[2])
index += 1
next
end
if question.question_type == "多选题"
contents = result[0].split(",")
values = result[1].split(",")
contents.each_with_index do |content, i|
worksheet.add_cell(index, 0, content)
worksheet.add_cell(index, 1, values[i])
worksheet.add_cell(index, 2, result[2])
index += 1
end
next
end
if question.question_type == "下拉网格题"
question_content_meta = result[0].split(",")
question_value_meta = result[1].split(",")
worksheet.add_cell(index, 0, question_content_meta[0])
worksheet.add_cell(index, 1, question_content_meta[1])
worksheet.add_cell(index, 2, question_content_meta[2])
worksheet.add_cell(index, 3, question_value_meta.last)
worksheet.add_cell(index, 4, result[2])
index += 1
next
end
if question.question_type == "单选网格题"
question_content_meta = result[0].split(",")
question_value_meta = result[1].split(",")
worksheet.add_cell(index, 0, question_content_meta[0])
worksheet.add_cell(index, 1, question_content_meta[1])
worksheet.add_cell(index, 2, question_value_meta.last)
worksheet.add_cell(index, 3, result[2])
index += 1
next
end
next
end
end
def render_worksheet_header(worksheet:, header_type:)
header = {
"单选题" => SIMPLE_QUESTION_HEADERS,
"多选题" => SIMPLE_QUESTION_HEADERS,
"下拉网格题" => DORPDOWN_GIRD_QUESTION_HEADERS,
"单选网格题" => MULTIPLE_CHOICE_GIRD_QUESTION_HEADERS,
}[header_type]
header.each_with_index do |h, index|
worksheet.add_cell(0, index, h)
end
end
def query(answer)
ResponseAnswer.where(id: answer)
.joins(:questionnaire_response, questionnaire_response: :user)
.limit(1)
.select("content, value, users.nickname")
.pluck(:content, :value, :nickname).first
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment