Skip to content

Instantly share code, notes, and snippets.

@tanaikech
Last active Oct 13, 2021
Embed
What would you like to do?
Downloading File Using Button of Dialog Box on Google Docs

Downloading File Using Button of Dialog Box on Google Docs

This is a sample script for downloading a file using a button of dialog box on Google Docs (Spreadsheet, Document and Slides).

Please use this sample script at script editor on Google Docs (Spreadsheet, Document and Slides). And please set file ID in the script.

FLow :

The flow of this sample script is as follows.

  1. Run dialog().
    • Open a dialog.
  2. When users click a download button, retrieve file ID at GAS side.
  3. Create download URL from the file ID. Download URL and filename are sent to download(obj) of Javascript.
  4. Create a tag for downloading and click it at Javascript side.
  • By this, users can download the file of file ID.

Code.gs

function dialog() {
  var html = HtmlService.createHtmlOutputFromFile('download');
  SpreadsheetApp.getUi().showModalDialog(html, 'Sample dialog'); // If you use other Google Docs, please modify here.
}

function getDownloadUrl() {
  var id = "### file id ###";

  var file = DriveApp.getFileById(id);
  return {
    url: file.getDownloadUrl().replace("?e=download&gd=true",""),
    filename: file.getName()
  };
}

download.html

<input type="button" value="download" onclick="getUrl()" />
<script>
  function getUrl() {
    google.script.run.withSuccessHandler(download).getDownloadUrl();
  }

  function download(obj) {
    var d = document.createElement('a');
    d.href = obj.url;
    d.download = obj.filename;
    d.click();
  }
</script>

Another method

For example, when the text data is downloaded, this method can be also used. https://stackoverflow.com/q/66308153/7108653 In this method, the text data is converted to the data URL. By this, the text data is not required to be created as a file. Also, for example, when you want to make users download the image data and PDF data by downloading from the URL, this method can be also used.

@dginovker

This comment has been minimized.

Copy link

@dginovker dginovker commented Oct 13, 2021

Still works in 2021. Similar SO post with more generic file solution: https://stackoverflow.com/questions/20281272/initiate-a-download-from-google-apps-script/69548483#69548483

@tanaikech

This comment has been minimized.

Copy link
Owner Author

@tanaikech tanaikech commented Oct 13, 2021

Thank you for your comment. As another method, for example, when the text data is downloaded, this method can be also used. https://stackoverflow.com/q/66308153/7108653 In this method, the text data is converted to the data URL. By this, the text data is not required to be created as a file. Also, for example, when you want to make users download the image data and PDF data by downloading from the URL, this method can be also used.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment