diff --git a/app/controllers/admin/surveys_controller.rb b/app/controllers/admin/surveys_controller.rb index 5c5454e..680df17 100644 --- a/app/controllers/admin/surveys_controller.rb +++ b/app/controllers/admin/surveys_controller.rb @@ -146,18 +146,21 @@ class Admin::SurveysController < OrbitAdminController end def checkforthread - render :json => {"status" => @download_thread.alive?}.to_json + running = !File.exists?("public/uploads/survey_export/#{params[:survey_id]}/#{params[:survey_title]}.xlsx") + render :json => {"status" => running}.to_json end def export - @download_thread = Thread.new do - # %x[rake survey_tasks:prepare_download[#{@survey.id}]] - (1..10).each do |i| - sleep(1) - end + I18n.locale = :zh_tw + title = @survey.title.gsub(/[ "'*@#$%^&()+=;:.,?>|\\\/<~_!:,、。!?;「」〈〉【】/]/,'') + f = "public/uploads/survey_export/#{@survey.id}/#{title}.xlsx" + File.delete(f) if File.exists?(f) + + Thread.new do + system "rake survey_tasks:prepare_download[#{@survey.id}] >> #{Rails.root}/log/rake.log &" end - @download_thread.join - render :json => {"success" => true}.to_json + + render :json => {"success" => true, "title" => title}.to_json # @chart_data, @survey_questions, @survey_answers = @survey.generate_chart_data # respond_to do |format| # format.xlsx { diff --git a/app/views/admin/surveys/_index.html.erb b/app/views/admin/surveys/_index.html.erb index 626fb83..7cd4742 100644 --- a/app/views/admin/surveys/_index.html.erb +++ b/app/views/admin/surveys/_index.html.erb @@ -30,7 +30,7 @@
  • <%= t('survey.duplicate_it') %>
  • <%= t('survey.jump') %>
  • <%= t('survey.set_answers') %>
  • -
  • <%= t('survey.export_csv') %>
  • +
  • <%= t('survey.export_csv') %>
  • <%= t('survey.chart') %>
  • <%= t(:delete_) %>
  • <% end %> diff --git a/app/views/admin/surveys/index.html.erb b/app/views/admin/surveys/index.html.erb index 53f7f4e..9f343c0 100644 --- a/app/views/admin/surveys/index.html.erb +++ b/app/views/admin/surveys/index.html.erb @@ -5,7 +5,7 @@ <%= render 'layouts/delete_modal', delete_options: @delete_options %> - - + \ No newline at end of file diff --git a/app/views/survey_export/export.xlsx.axlsx b/app/views/survey_export/export.xlsx.axlsx index 488857e..2cf4b65 100644 --- a/app/views/survey_export/export.xlsx.axlsx +++ b/app/views/survey_export/export.xlsx.axlsx @@ -1,7 +1,7 @@ # encoding: utf-8 wb = xlsx_package.workbook -wb.add_worksheet(name: remove_illegal_utf8(survey.title[0..15])) do |sheet| +wb.add_worksheet(name: "WorkSheet1") do |sheet| row = [] survey_questions.each_with_index do |question, i| diff --git a/lib/tasks/survey_tasks.rake b/lib/tasks/survey_tasks.rake index 216e8cf..703c656 100644 --- a/lib/tasks/survey_tasks.rake +++ b/lib/tasks/survey_tasks.rake @@ -11,9 +11,9 @@ namespace :survey_tasks do chart_data, survey_questions, survey_answers = survey.generate_chart_data ac = ActionController::Base.new() xlsx = ac.render_to_string handlers: [:axlsx], formats: [:xlsx], template: "survey_export/export", locals: {survey: survey, survey_questions: survey_questions, survey_answers: survey_answers} - dirname = "public/survey_export/#{id}" + dirname = "public/uploads/survey_export/#{id}" FileUtils.mkdir_p(dirname) unless File.exists?(dirname) - f = "#{dirname}/#{survey.title.gusb(' ', '_')}.xlsx" + f = "#{dirname}/#{survey.title.gsub(/[ "'*@#$%^&()+=;:.,?>|\\\/<~_!:,、。!?;「」〈〉【】/]/,'')}.xlsx" if File.exists?(f) File.delete(f) end