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());
}