Convert CSV charset from Shift-JIS to UTF-8

1.2k Views Asked by At

What I want to do

  1. Fetch data from a CSV file (charset: Shift-JIS)
  2. Convert the charset to UTF-8
  3. 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);
}

Expected: Ideal

Instead of: Actual

1

There are 1 best solutions below

0
Tanaike On BEST ANSWER

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:

function myFunction() {
  const fileId = 'xxxxxx' // Shift-JIS CSV file's ID
  const blob = DriveApp.getFileById(fileId).getBlob();

  const csv = blob.getDataAsString("shift-jis");  // Modified

  const values = Utilities.parseCsv(csv);
  const sheet = SpreadsheetApp.getActiveSheet();
  sheet.getRange(1, 1, values.length, values[0].length).setValues(values);
}

Reference: