Google脚本CSV文件返回框和问号

时间:2018-10-11 21:55:32

标签: javascript google-apps-script encoding

我能够从Google API中获取所需的CSV;但是-我觉得编码不正确,因为我收到了方框和问号。

任何人都知道可能是什么原因,如何正确编码?

enter image description here

function getPlayData(){
  var main = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('main');
  var token = tokenRefresh();
  var now = new Date();
  var oneMonthAgo = new Date();
  oneMonthAgo.setMonth(oneMonthAgo.getMonth() - 1);
  var oneMonthAgo = Utilities.formatDate(oneMonthAgo, "GMT+1", "yyyyMM"); 

  var currentMonth = Utilities.formatDate(new Date(), "GMT+1", "yyyyMM"); 
  for(i=0; i<1; i++){
    var options = {
      'headers':
      {
        'Authorization' : 'Bearer '+token
      }
    }
    var url = "https://storage.googleapis.com/XXXXXXX/stats/installs/installs_overview.csv";
    var csv_response = UrlFetchApp.fetch(url,options);
    var response = Utilities.parseCsv(csv_response, ',');
    for(j=0; j<response.length; j++)
    {
      main.getRange(3+j, 2, 1, 1).setValue(response[j][0])
    }
  }

  var a = '';

}

仅以原始格式发现-Google以某种格式存储CSV! RAW

1 个答案:

答案 0 :(得分:1)

似乎编码采用UTF-16,而不是UTF-8。一旦使用以下命令将其强制为UTF-16:

var csv_response = UrlFetchApp.fetch(url,options).getContentText("UTF-16");

它就像一种对待。