From bfd2b8abebc0ffb9e454a9dd9217834893024bbc Mon Sep 17 00:00:00 2001 From: Harry Bomrah Date: Fri, 9 Dec 2016 19:42:26 +0800 Subject: [PATCH] encoding fix --- app/helpers/admin/surveys_helper.rb | 12 ++++++++++++ app/views/admin/surveys/export.xlsx.axlsx | 10 +++++----- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/app/helpers/admin/surveys_helper.rb b/app/helpers/admin/surveys_helper.rb index 7ddf59f..4e525a9 100644 --- a/app/helpers/admin/surveys_helper.rb +++ b/app/helpers/admin/surveys_helper.rb @@ -1,4 +1,5 @@ module Admin::SurveysHelper + require 'iconv' unless String.method_defined?(:encode) def page_for_survey(survey=nil) ann_page = nil pages = Page.where(:module=>'survey') @@ -28,4 +29,15 @@ module Admin::SurveysHelper request.protocol+(request.host_with_port+'/'+I18n.locale.to_s+ann_page.url+'/').gsub('//','/') rescue "/" end end + + def remove_illegal_utf8(str) + if String.method_defined?(:encode) + str.encode!('UTF-8', 'UTF-8', :invalid => :replace) + else + ic = Iconv.new('UTF-8', 'UTF-8//IGNORE') + str = ic.iconv(str) + end + str + end + end \ No newline at end of file diff --git a/app/views/admin/surveys/export.xlsx.axlsx b/app/views/admin/surveys/export.xlsx.axlsx index f6b11d3..4ba168a 100644 --- a/app/views/admin/surveys/export.xlsx.axlsx +++ b/app/views/admin/surveys/export.xlsx.axlsx @@ -1,25 +1,25 @@ # encoding: utf-8 wb = xlsx_package.workbook -wb.add_worksheet(name: @survey.title[0..15]) do |sheet| +wb.add_worksheet(name: remove_illegal_utf8(@survey.title[0..15])) do |sheet| 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| - row << "#{i+1}. #{question.title} - #{option.name}" + row << "#{i+1}. #{remove_illegal_utf8 question.title} - #{remove_illegal_utf8 option.name}" end elsif question.type == 5 question.survey_question_options.each do |option| question.survey_question_radiogroups.each do |radiogroup| - row << "#{i+1}. #{question.title} - #{option.name} - #{radiogroup.name}" + row << "#{i+1}. #{remove_illegal_utf8 question.title} - #{remove_illegal_utf8 option.name} - #{remove_illegal_utf8 radiogroup.name}" end end else - row << "#{i+1}. #{question.title}" + row << "#{i+1}. #{remove_illegal_utf8 question.title}" end if question.custom_option - row << "#{i+1}. #{question.title} - #{t('survey_question.use_custom_option')}" + row << "#{i+1}. #{remove_illegal_utf8 question.title} - #{t('survey_question.use_custom_option')}" end end