DocsList文件getContentAsString()缺少unicode字符 [英] DocsList File getContentAsString() missing unicode characters

查看:396
本文介绍了DocsList文件getContentAsString()缺少unicode字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用Google App Script导入带有法国口音的CSV文件,使用getContentAsString()读取文件,然后将其处理到Google Spreadsheet中。这似乎是unicode字符发回垃圾。

I am trying to import a CSV file with french accents using Google App Script, reading the file using the getContentAsString() and then processing it into a Google Spreadsheet. It would seems the unicode characters are send back as garbage.

调查后,它似乎getContentAsString()使用UTF-8打开文件。当使用Western Mac OS Roman或Western Windows Latin 1创建文件时导致问题 - 在导出CSV时,在较早的Excel上使用默认编码。

After investigation, it would seems getContentAsString() open files using UTF-8. This cause problems when the file is created using Western Mac OS Roman or Western Windows Latin 1 - default encoding on older Excel when exporting CSV.

有关如何绕过此操作的任何建议问题?
$ b

Any suggestion on how to circumvent this problem?


示例: quipement应该是Équipement



function Test() {
  var filename = 'BV_period_2.csv';
  var files = DocsList.getFiles();
  var csvFile = "";

  for (var i = 0; i < files.length; i++) {
    if (files[i].getName() == filename ) {
      csvFile = files[i].getContentAsString(); //csvFile will have �     
      break;
    }
  }

  var csvData = CSVToArray(csvFile, ",");
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName('TestBV');
  ...


推荐答案

您可以选择charset。这里有一个UTF-16示例。

You can optionally choose the charset. Here's a UTF-16 example.

DocsList.getFileById(<some id>).getBlob().getDataAsString("UTF-16")

这篇关于DocsList文件getContentAsString()缺少unicode字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆