0

Let's explain the context of my request : I'm trying to output the content of a google spreadsheet in a .txt tabulated file, encoded in UTF-16, because I need this charset in a later task with this .txt file.

Actually, I've got the right output in the right folder and with the name I want ect ... but the charset is UTF-8 (i've check with an basic text editor).

The problem is I can't find any documentation about charset manipulation with google script. My only solution for now, is a basic manual charset manipulation in sublim text ...

Here's my code (translated from french).

Thank's for your replies !

Maxime

    function export() {
  //sheet manipulation part
  //Get the sheet and set data range i need
  var sheet = SpreadsheetApp.getActiveSheet();
  var range = sheet.getDataRange();
  var values = sheet.getRange(2, 1, sheet.getLastRow()-1, sheet.getLastColumn()).getValues();
  var text = values.map(function (a) {return a.join('\t');}).join('\n');

  //Path setting and export part
  // Get sheet info, define path and create file
  var id = SpreadsheetApp.getActiveSpreadsheet().getId();
  var idString = id.toString();
  var thisFile = DriveApp.getFileById(idString);
  var parentFold = thisFile.getParents();
  var folder = parentFold.next();
  var theId = folder.getId();
  var targetFolder = DriveApp.getFolderById(theId);
  targetFolder.createFile('liste du ' + new Date() + '.txt', text, MimeType.PLAIN_TEXT);
}
random-parts
  • 2,137
  • 2
  • 13
  • 20
Maxime
  • 39
  • 1
  • 11
  • Hi Maxime, did you find any solution to this problem? I convert files by SublimeText too now, but I cannot leave it that way for longer. – Peter222 Mar 13 '18 at 06:34

2 Answers2

2

This helped me:

var string = 'your text here';
var blob = Utilities.newBlob('').setDataFromString(string, "UTF-16");
blob.setName('your_file_name.txt');
var file = DriveApp.createFile(blob);

As a result createFile method will use UTF-16.

Birmin
  • 101
  • 1
  • 4
0

This has already been answered in this SO post:

Utilities.newBlob("").setDataFromString("foo", "UTF-8").getDataAsString("UTF-16")
ReyAnthonyRenacia
  • 17,219
  • 5
  • 37
  • 56
  • Hey, thanks for your answer but this is not the right solution, because the characters inside the file are probably encoded in UTF-16, but the **exported** file with ‘DriveApp.createFile‘ is not encoded with UTF-16, you see what i mean ? thx again. – Maxime Dec 14 '17 at 09:29