Download a CSV File from Spreadsheet Using Google HTML Service

Here, I introduce how to download a CSV file from spreadsheet using Google HTML Service.

  1. Using “onOpen()”, it addes menu for launching a dialog.

  1. After launching the dialog, “getFileUrl()” is launched by pushing a button. “getFileUrl()” exports a CSV file and outputs download URL.

  2. The CSV file is downloaded by “executeDownload()”.

Please put both HTML and GAS to a GAS project.

HTML : download.html

<!DOCTYPE html>
    Download CSV?
      <input type="button" value="ok" onclick="
                                              .getFileUrl();" />
    function executeDownload(url) {
      window.location.href = url;


function onOpen() {
                .addItem('export csv files', 'dialog')

function dialog() {
  var html = HtmlService.createHtmlOutputFromFile('download');
  SpreadsheetApp.getUi().showModalDialog(html, 'CSV download dialog');

function getFileUrl() {
    var filename = "#####"; // CSV file name
    var folder = "#####"; // Folder ID

    var csv = "";
    var v = SpreadsheetApp // Now spreadsheet is an active sheet.
    v.forEach(function(e) {
      csv += e.join(",") + "\n";
    var url = DriveApp.getFolderById(folder)
              .createFile(filename, csv, MimeType.CSV)
    return url;