turtlepower
turtlepower

Reputation: 758

Google Scripts CSV file returning boxes and question marks

I am able to fetch the CSV I want from the Google API; however - I think the encoding is not quite right as I'm getting boxes and question marks.

Anyone know what could be the cause and how I can encode it properly?

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 = '';

}

Just found out in its raw form - Google somehow stores the CSV in this format! RAW

Upvotes: 0

Views: 446

Answers (1)

turtlepower
turtlepower

Reputation: 758

It appears that the encoding was in UTF-16 rather than UTF-8. Once it's forced to UTF-16 using:

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

It works like a treat.

Upvotes: 1

Related Questions