encoding fix

This commit is contained in:
Harry Bomrah 2016-12-09 19:42:26 +08:00
parent 9917cb186d
commit bfd2b8abeb
2 changed files with 17 additions and 5 deletions

View File

@ -1,4 +1,5 @@
module Admin::SurveysHelper module Admin::SurveysHelper
require 'iconv' unless String.method_defined?(:encode)
def page_for_survey(survey=nil) def page_for_survey(survey=nil)
ann_page = nil ann_page = nil
pages = Page.where(:module=>'survey') 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 "/" request.protocol+(request.host_with_port+'/'+I18n.locale.to_s+ann_page.url+'/').gsub('//','/') rescue "/"
end end
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 end

View File

@ -1,25 +1,25 @@
# encoding: utf-8 # encoding: utf-8
wb = xlsx_package.workbook 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 = [] row = []
@survey_questions.each_with_index do |question, i| @survey_questions.each_with_index do |question, i|
if question.type == 2 or question.type == 3 or question.type == 4 if question.type == 2 or question.type == 3 or question.type == 4
question.survey_question_options.each do |option| 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 end
elsif question.type == 5 elsif question.type == 5
question.survey_question_options.each do |option| question.survey_question_options.each do |option|
question.survey_question_radiogroups.each do |radiogroup| 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
end end
else else
row << "#{i+1}. #{question.title}" row << "#{i+1}. #{remove_illegal_utf8 question.title}"
end end
if question.custom_option 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
end end