Skip to content Skip to sidebar Skip to footer

Tab Characters Converted To Spaces In Google Apps Script

I have a Google Apps Script I am writing that combines a number of reports into a consolidated tab-delimited (TSV) format. However, I am noticing that whenever I write to file, the

Solution 1:

Nothing is wrong with using the \t string literal - rather you're using the wrong MimeType enum, from the ContentService. DriveApp methods expect MimeType as a string. Try:

fld.createFile(makeid() + ".tab", content, MimeType.CSV);

Here's a snippet that shows the tab characters survive the file write when the MimeType is properly set:

function myFunction() {
  var content = 
        'time' + "\t" +
        'url' + "\t" +
        'code_name' + "\t" +
        'etcetera';
  Logger.log("Initial TSV: " + JSON.stringify(content.split("\t")));

  var newFile = DriveApp.createFile("Delete-me" + ".tab", content, MimeType.CSV);

  // Check our result, by reading back the file
  var blob = newFile.getBlob();
  var docContent = blob.getDataAsString()
  Logger.log("Result TSV: " + JSON.stringify(docContent.split("\t")));
}

Logs:

[15-04-07 14:53:08:767 EDT] Initial TSV: ["time","url","code_name","etcetera"]
[15-04-07 14:53:09:542 EDT] Result TSV: ["time","url","code_name","etcetera"]

Post a Comment for "Tab Characters Converted To Spaces In Google Apps Script"