Down load Sheet as CSV

48 views
Skip to first unread message

Timothy Hanson

unread,
Oct 16, 2023, 11:56:22 AM10/16/23
to Google Apps Script Community
I am hoping someone can help with this. 

I want to automate the creation and downloading of a Google sheet as a CSV file to a desktop with a script.

I have been searching for a script to do this but can find nothing that works or that I can make work. 

Thanks

Timothy Hanson

unread,
Oct 16, 2023, 12:53:46 PM10/16/23
to Google Apps Script Community
Why this is marked as abuse? It has been marked as abuse.
Report not abuse
So I found this which works great for emailing the CSV files.

But how to modify it to download CSV file to DesktoP?

function myFunction() {
  var emailAddress = "XXX.com"; // Please set your email address.
  var sheetNames = ["Data", "Elements"]; // Please set your 2 sheet names you want to use.

  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var ssID = ss.getId();
  var requestData = { "method": "GET", "headers": { "Authorization": "Bearer " + ScriptApp.getOAuthToken() } };
  var blobs = sheetNames.map(s => {
    var params = ssID + "/export?gid=" + ss.getSheetByName(s).getSheetId() + "&format=csv";
    var url = "https://docs.google.com/spreadsheets/d/" + params;
    return UrlFetchApp.fetch(url, requestData).getBlob().setName(s); // <--- Modified
  });
  //MailApp.sendEmail({ to: emailAddress, subject: "sample subject", body: "sample body", attachments: blobs });
}

Reply all
Reply to author
Forward
0 new messages