Creating a script to Save range as CSV in drive then send order via emial as attatchment

Viewed 170

Hope all is well. I am trying to create a simple ordering tool. I want to export the range as CSV. For this, I am using this simple formula. https://docs.google.com/spreadsheets/d/ID/export?format=csv&range=B3:E (This is working fine) What I want to do is save this range into my google drive. Then, send export (CSV) in attachment. All in one button. Can someone help me out? I am a newbie in coding so don't have much of a code.

1 Answers

I believe your goal as follows.

  • You want to save the CSV data as a file from the endpoint of https://docs.google.com/spreadsheets/d/ID/export?format=csv&range=B3:E.

In this case, how about using Google Apps Script for achieving your goal? The sample script is as follows.

Sample script:

For example, please copy and paste the following script to the script editor of Spreadsheet and set the URL by including spreadsheet ID, and run the function. By this, after the authorization for scopes was done, the script creates the CSV file to the root folder.

function myFunction() {
  const url = "https://docs.google.com/spreadsheets/d/ID/export?format=csv&range=B3:E";
  const res = UrlFetchApp.fetch(url, {headers: {authorization: "Bearer " + ScriptApp.getOAuthToken()}});
  const blob = res.getBlob().setName("sample.csv"); // You can set the filename here.
  DriveApp.createFile(blob);
}

References:

Related