2

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?

Rubén
  • 34,714
  • 9
  • 70
  • 166
Wronski
  • 1,506
  • 3
  • 18
  • 37
  • Just because the file name contains ".txt" doesn't make it a text file. If you click on the file in your Google Drive, and look at the "Details" pane on the right, does it state that it's a text file? Did you create the text file? If so, how did you create it? – Alan Wells Sep 21 '20 at 22:26
  • @AlanWells, yes the right hand side pane in google drive has `type: Text` but I did not create the file. It's sent to me via email from an external software system. – Wronski Sep 21 '20 at 22:33
  • Also realize that the file is not a CSV. If it's delimited, you'll need to pass delimiter along in your `Utilities.parseCsv()` call. – Diego Sep 21 '20 at 23:07
  • @Diego, need to get a workable string first. – Wronski Sep 21 '20 at 23:30

1 Answers1

4

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

Rubén
  • 34,714
  • 9
  • 70
  • 166
  • The default charset is `UTF-8`. I have already tried the alternative `ISO-8859-1`, which only replaced the � symbol, but not the square symbols. – Wronski Sep 21 '20 at 23:27
  • @Wronski What is the charset used by your file? – Rubén Sep 21 '20 at 23:31
  • 1
    perfect. I didn't realise Google Script supported other options. The encoding of the file was `UTF-16` and once I used that as a parameter it fixed the issue! Thank you :) – Wronski Sep 21 '20 at 23:37