When I try to save data with ruby and Google spreadsheet gem, it becomes An invalid XML character (Unicode:0x14) was found and cannot be saved.

Asked 2 years ago, Updated 2 years ago, 84 views

Array data is as follows:

 parent=['a', 'b', ['c', 'd']]

I want to save this to a spreadsheet using google_drive's gem with the following code, but I can't...

@worksheet=google_drive_session.spreadsheet_by_key(GOOGLE_DOCS_WS_KEY).worksheet_by_title(WORKSHEET_TITLE)
parent.each do | child |
    @worksheet.update_cells (@worksheet.num_rows+1,1,[child])
end

An error similar to the following occurs:

GoogleDrive::Error: Update has failed: An invalid XML character (Unicode:0x14) was found in the value of attribute "inputValue" and element is "gs:cell".

As far as this article is concerned,

This seems to be because control characters were inserted due to the use of emoticons and garbled characters.

I don't know how to avoid this with ruby and save the data without any errors.

I look forward to your kind cooperation.

ruby:2.1.0

ruby rubygems

2022-09-30 16:32

1 Answers

I think you are asking about string conversion in ruby.
As for the error provided, Unicode:0x14, how about deleting non-printable characters?

[46]pry(main)>"\x14ab".gsub(/[\x00-\x1f]/, "")
=>"ab"

I tried [:graph:] to see if I could play with printable characters, but it didn't work, so I specified it myself.


2022-09-30 16:32

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.