Encoding Issue
Is there any options for encoding? I don't seem to be able to find this in the documentation. Like in Rails CSV importer
CSV.foreach(file.path, headers: true, encoding: 'iso-8859-1:utf-8') do |row|
# your code here
end
Cause I'm having a trouble handling error in encoding when user uploads csv file. For a word like this: test¶ØÜó??test.
Thanks before.
Hi @samuel-christian,
unfortunately, at the moment there is no way to define source and destination encoding through reading process and I have no time to work on it. Feel free to open a pull request if you decide to implement it.
Best
I see.
Later today, I found bypass about this for those who also stumble upon it.
You can utilize rails rescue to handle the encoding error (I know it's not the ideal way, but if time is of essence rather than implementing something new, so why not).
So, after reading the file, e.g using this:
@book = SimpleSpreadsheet::Workbook.read(file)
@book.selected_sheet = book.sheets.first
If you access the value of @book.last_column or @book.last_row or maybe @book.cell(n,n), it will throw out " invalid byte sequence in UTF-8" error message. At this part, rescue rails really "rescues" you (pun intended). Try put it into some method "like validation method" and return it.
Example:
def some_method
# assuming attr_accessor is used
@book.last_column rescue nil
end
Then we can use this value as validation, if nil then the file has invalid characters. I know the result maybe a little bit arbitrary, but whatever floats your boat. We can change it into something meaningful for user experience using strings and I18n error messages. That's all.
Hope it helps. Cheers.