0

I have a variable (TRK) in Google Apps Script and intend to download the content of the variable as text-file. It shall get a GPX-File. How can I download this by clicking a command button in Google Sheets?

This is the content of the variable TRK:

<gpx><trk><trkseg>
<trkpt lat="50.101475" lon="8.530224"></trkpt>
<trkpt lat="50.101612" lon="8.53075"></trkpt>
<trkpt lat="50.101616" lon="8.530927"></trkpt>
<trkpt lat="50.10153" lon="8.531324"></trkpt>
<trkpt lat="50.101451" lon="8.531785"></trkpt>
</trkseg></trk></gpx>
Dirk
  • 145
  • 1
  • 11
  • 1
    Take a look at this: https://stackoverflow.com/questions/17376102/download-file-from-google-drive-to-local-folder-from-google-apps-script. Also just Google `Downloading with Google Apps Script` – Cooper Sep 28 '19 at 02:38
  • Thank you. These are all examples how to download a file. But I am looking for a code to download the content of a variable as explained above. – Dirk Sep 28 '19 at 08:43
  • You said you wanted to download the content of the variable is a text file. If that’s not what you want then change your question. – Cooper Sep 28 '19 at 10:29
  • Yes, I want to download the content of the Variable TRK as text-file. But I do not understand the samples. – Dirk Sep 28 '19 at 10:34
  • What are you gonna do with it when you download it? – Cooper Sep 28 '19 at 11:41
  • I can use the GPX-File for navigation by Smartphone-Apps. – Dirk Sep 28 '19 at 13:38

1 Answers1

1

It actually took me two buttons.

function onOpen() {
  SpreadsheetApp.getUi().createMenu('My Tools')
  .addItem('Save Variable As File', 'saveVariableAsFile')
  .addItem('Download gpx.txt', 'downLoad')
  .addToUi();
}

Use this function or something like it to save the content in a file. The filename is gpx.txt and you need to update the folder id with one of your choosing. You may change the file name if you wish. But it's used in several places so replace them all.

function saveVariableAsFile(gpx) {
  var folder=DriveApp.getFolderById('FolderId');
  var files=folder.getFilesByName("gpx.txt");
  while(files.hasNext()) {files.next().setTrashed(true);}//guarantees that there is only one file with that name in the folder.
  var file=folder.createFile("gpx.txt", gpx);
}

This script is called from a menu or if you want to make it an image button that's upto you. I generates a dialog that allows you to download the file to your computer.

function downLoad() {
  var url=ScriptApp.getService().getUrl();
  var html=Utilities.formatString('<input type="button" value="Download GPX" onClick="window.location.replace(\'%s\');" />',ScriptApp.getService().getUrl());
  var userInterface=HtmlService.createHtmlOutput(html);
  SpreadsheetApp.getUi().showSidebar(userInterface);
}

This function actually performs the download. It gets the file from the above folder so you will need to replace the folder id.

function doGet() {
  var folder=DriveApp.getFolderById('FolderId');
  var file=folder.getFilesByName("gpx.txt").next();//There is only one file in the folder with that name
  return ContentService.createTextOutput().append(file.getBlob().getDataAsString()).downloadAsFile(file.getName());     
}    
Cooper
  • 59,616
  • 6
  • 23
  • 54