What I want to do
- Fetch data from a CSV file (charset: Shift-JIS)
- Convert the charset to UTF-8
- Set the values into Google Spread Sheets
I have a problem on step 2.
Issue
I'm having a trouble converting Shift-JIS CSV to UTF-8 CSV.
The strings in Google Spread Sheets are all garbled.
Here is my code and screenshots of Google Spread Sheets:
function myFunction() {
const fileId = 'xxxxxx' // Shift-JIS CSV file's ID
const blob = DriveApp.getFileById(fileId).getBlob();
const csv = blob.getDataAsString();
const newBlob = Utilities.newBlob('', MimeType.CSV).setDataFromString(csv, 'UTF-8');
const newCSV = newBlob.getDataAsString();
const values = Utilities.parseCsv(newCSV);
const sheet = SpreadsheetApp.getActiveSheet();
sheet.getRange(1, 1, values.length, values[0].length).setValues(values);
}


I think that in your script, it is required to retrieve the values from the file (CSV data of shift-jis) as
shift-jis. In this case,const newBlob = Utilities.newBlob('', MimeType.CSV).setDataFromString(csv, 'UTF-8');is not required to be used. So when your script is modified, it becomes as follows.Modified script:
Reference: