You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
educoder/app/views/polls/commit_result.xlsx.axlsx

94 lines
4.6 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

wb = xlsx_package.workbook
# wb.use_autowidth = false
wb.styles do |s|
sz_all = s.add_style :sz => 10,:border => { :style => :thin, :color =>"000000"},:alignment => {wrap_text: true,:horizontal => :left}
blue_cell = s.add_style :bg_color => "FAEBDC", :sz => 10,:height => 20,:b => true, :border => { :style => :thin, :color =>"000000" },:alignment => {wrap_text: true,:horizontal => :left}
wb.add_worksheet(:name => "统计结果") do |sheet|
sheet.sheet_view.show_grid_lines = false
poll_users_info = %w(序号)
poll_ques_titles = poll_questions.pluck(:question_title).map {|k| strip_export_title(k) if k.present?}
poll_un_anony = poll.un_anonymous
if poll_un_anony #是否匿名默认为false
user_info = %w(登陆名 真实姓名 邮箱 学号)
else
user_info = []
end
poll_users_info = poll_users_info + user_info + poll_ques_titles
poll_questions.each do |q|
if q.question_type != 3 #问题不为主观题
question_vote_user = q.poll_votes.find_current_vote("user_id",poll_commit_ids).count #该问题的有效填写量
sheet_row = ["第#{q.question_number}题"] #选择题答案选项的数组
sheet_answer_row = ["小计"] #选择题回答的答案人数,数组
sheet_answer_percent = ["比例"]
sheet_answer_useful = ["有效填写人次",question_vote_user]
q.poll_answers.each do |a| #问卷的答案选项
answer_users_count = a.poll_votes.find_current_vote("user_id",poll_commit_ids).count
answer_percent = number_to_percentage((answer_users_count.to_f / question_vote_user.to_f)*100,precision:1)
sheet_row.push(a.answer_text)
sheet_answer_row.push(answer_users_count)
sheet_answer_percent.push(answer_percent.to_s)
end
sheet.add_row sheet_row, :height =>15,:style => blue_cell
sheet.add_row sheet_answer_row, :height =>15, :style => sz_all
sheet.add_row sheet_answer_percent, :height =>15, :style => sz_all
sheet.add_row sheet_answer_useful, :height =>15, :style => sz_all
#合并单元格但无法填充style
# sheet.merge_cells (Axlsx::cell_r(1,sheet.rows.last.row_index) + ':' + Axlsx::cell_r(sheet_row.count-1,sheet.rows.last.row_index))
# sheet.rows[sheet.rows.last.row_index].style = sz_all
sheet.add_row []
else #主观题答案
main_show_row = ["第#{q.question_number}题",q.question_title]
sheet.add_row main_show_row,:height =>15, :style => blue_cell
q.poll_votes.each do |v| #主观题的答案
sheet.add_row ["",v.vote_text.present? ? v.vote_text : "--"],:height =>15, :style => sz_all
end
sheet.add_row [], :style => sz_all
end
end #each_with_index
sheet.add_row poll_users_info, :height =>15, :style => blue_cell
poll_users.includes(user: :user_extension).each_with_index do |u,index|
u_user = u.user
user_answer_array = []
poll_questions.each do |q|
user_poll_votes = u_user.poll_votes.find_current_vote("poll_question_id",q.id)
if user_poll_votes.present?
user_poll_answer_ids = user_poll_votes.pluck(:poll_answer_id).reject(&:blank?)
user_poll_vote_texts = user_poll_votes.pluck(:vote_text).reject(&:blank?)
if user_poll_answer_ids.count > 0
answer_content = q.poll_answers.find_answer_by_custom("id",user_poll_answer_ids)
if user_poll_answer_ids.count >1
u_answer = answer_content.pluck(:answer_text).join(";")
else
u_answer = answer_content.first.answer_text
end
elsif user_poll_vote_texts.count > 0
if user_poll_vote_texts.count > 1
u_answer = user_poll_vote_texts.join(";")
else
u_answer = user_poll_vote_texts.first
end
else
u_answer = "--"
end
else
u_answer = "--"
end
user_answer_array.push(u_answer)
end
user_cell = [index+1]
if poll_un_anony
user_login = u_user.login
user_name = u_user.real_name.present? ? u_user.real_name : "--"
user_student_id = u_user.student_id.present? ? u_user.student_id : "--"
user_cell += [user_login,user_name, u_user.mail, user_student_id]
end
all_user_cell = user_cell + user_answer_array
sheet.add_row all_user_cell, :height =>15,:style => sz_all
end
sheet.column_widths *([25]*sheet.column_info.count)
sheet.column_info.first.width = 10
end #add_worksheet
end