Wronski
Wronski

Reputation: 1596

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:

NOTES

QUESTION

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

Upvotes: 2

Views: 4216

Answers (1)

Wicket
Wicket

Reputation: 38296

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

Upvotes: 4

Related Questions