3

I have a Google spreadsheet file containing just one sheet with one column. Everytime it is changed, it should be saved as a simple text (*.txt) file on my Google drive. So I would use the "on edit" trigger to run the script.

I found a few scripts that supposedly once did exactly what I need, but they seem to use outdated references, like this one which was posted two years ago. So my question is, how to export a sheet to text file now, in 2016?

Community
  • 1
  • 1
AlexSell
  • 41
  • 1
  • 3

1 Answers1

9

Here is a simple export function that works for me: it exports the current sheet as tab-delimited text file, whose name includes the date of creation.

You can set it to be triggered on edit using Resources > Current Project's Triggers in the Script Editor.

function export() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var values = sheet.getDataRange().getValues();
  var text = values.map(function (a) {return a.join('\t');}).join('\n');
  DriveApp.createFile('Data as of ' + new Date(), text);
}

Depending on your editor/OS, you may want join('\r\n') instead of join('\n') here.

  • 1
    Thank you, this got me on the right track. I added a delete-old-file-line (just want one file) and an \r to the delimiter (so in a text file, everything isn't just one long line), and it works perfectly for me: function export() { DriveApp.getFilesByName('testfile.txt').next().setTrashed(true); var sheet = SpreadsheetApp.getActiveSheet(); var values = sheet.getDataRange().getValues(); var text = values.map(function (a) {return a.join('\t');}).join('\r\n'); DriveApp.createFile('testfile.txt', text, MimeType.PLAIN_TEXT); } – AlexSell Apr 23 '16 at 17:40