Search code examples
csvgoogle-apps-scriptcharacter-encodinggoogle-drive-apitext-parsing

Google Apps script txt file getDataAsString() returns � and square symbols between characters


CURRENTLY

I have a .txt file which contains some data:

                      Z Financial

Company name of the tax subject :

I have a google script file to get the data from the .txt and convert into JSON format to paste into Google Sheet (like a manual import in Google Sheets would do)

Snippet:

    let files = folder.getFiles();
    var file = files.next();
    var raw = file.getBlob().getDataAsString();
    console.log(raw);
    var data = Utilities.parseCsv(raw)

ISSUE

console.log(raw) is returning: enter image description here

data variable the returns as array of length 1 with value: �� and nothing else.

From the .txt snippet above, I would (roughly) expect an array of length = 3, with lines as follows:

  • Z Financial
  • Company name of the tax subject :

NOTES

  • replacing default UTF-8 encoding with ISO-8859-1 doesn't fix the issue. Returns: enter image description here

QUESTION

What am I missing to get the txt parsed as a CSV?


Solution

  • The code is not setting a charset. Try using the correct charset.

    var charset = 'ISO-8859-1'; // This is just a suggestion to try first
    var raw = file.getBlob().getDataAsString(charset);
    

    NOTE: The OP mentioned in a comment that the charset used by their file is UTF-16.

    Reference

    Related