问题描述
我想做什么
我在第 2 步遇到问题。
问题
我在将 Shift-JIS CSV 转换为 UTF-8 CSV 时遇到问题。
谷歌电子表格中的字符串都是乱码。
这是我的 Google 电子表格代码和屏幕截图:
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);
}
解决方法
我认为在您的脚本中,需要从文件(shift-jis 的 CSV 数据)中检索值作为 shift-jis
。在这种情况下,不需要使用 const newBlob = Utilities.newBlob('',MimeType.CSV).setDataFromString(csv,'UTF-8');
。所以当你的脚本被修改后,就变成了这样。
修改后的脚本:
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);
}