Just trying to take the current spreadsheet tab and mail it to an address as a *.csv file. I've gone through a huge number of prior questions but haven't found an answer. This is adapted from a successful send of a PDF file. The only issue is that I need CSV instead of PDF.
function mailCSV() {
// Get the currently active spreadsheet URL (link)
// Or use SpreadsheetApp.openByUrl("<<SPREADSHEET URL>>");
// Send the CSV of the spreadsheet to this email address
var email_send_address = "email@emailsample.com";
var email_subject ='subject'
// Get the currently active spreadsheet URL (link)
var ss = SpreadsheetApp.getActiveSpreadsheet();
var url = "http://spreadsheets.google.com/feeds/download/spreadsheets/Export?key="+ ss + "&exportFormat=csv";
//var csv = DriveApp.getFileById(ss.getId()).getAs('application/csv').getBytes();
//var attachCSV = {fileName: email_subject,content:url,mimeType:'application/csv'};
var res = UrlFetchApp.fetch(url);
var attachments = [{fileName:"dg_request.csv", content: res.getContent(),mimeType:"application/vnd.csv"}];
MailApp.sendEmail(email_send_address, email_subject, {attachments: attachments});
// MailApp.sendEmail(email_send_address, email_subject, {attachments:[attachCSV]});
}
Much better answer which sends as an attachment:
Full credit to: https://gist.github.com/nirajkadam/0b41a01b8e739800c964
function mailRequestCSV() {
// Send the PDF of the spreadsheet to this email address
var recipients = "someone@somewhere.com";
var ssID = SpreadsheetApp.getActiveSpreadsheet().getId();
var sheetName = SpreadsheetApp.getActiveSpreadsheet().getName();
var email = Session.getActiveUser().getEmail();
var subject = "DataGenerationRequest ";
var body = "****NOTE: THIS IS AN AUTO COMPUTER-GENERATED REPORT****";
var requestData = {"method": "GET", "headers":{"Authorization":"Bearer "+ScriptApp.getOAuthToken()}};
var url = "https://docs.google.com/spreadsheets/d/"+ssID+"/export?format=csvx&id="+ssID;
var result = UrlFetchApp.fetch(url, requestData);
var contents = result.getContent();
MailApp.sendEmail(recipients, subject, body, {attachments:[{fileName:sheetName+".csv", content:contents, mimeType:"application//csv"}]});
}