Isaac Legg
Isaac Legg

Reputation: 1

Output google sheet via email

I'm trying to output my custom google sheet to a set email address

I have tried adding into a blob before sending but every time the email is sent the excel sheet that is attached is blank. Any help would be appreciated

 var file          = DriveApp.getFilesByName(nSheet.getName());
var url           = 'https://docs.google.com/spreadsheets/d/'+nSheet.getId()+'/export?format=xlsx';
var token         = ScriptApp.getOAuthToken();
var response      = UrlFetchApp.fetch(url, {
  headers: {
    'Authorization': 'Bearer ' +  token
  }
});

var fileName = (nSheet.getName()) + '.xlsx';
var blobs   = [response.getBlob().setName(fileName)];

var emailAddress = "[email protected]"
var subject = "PCS v5 Validated Sheet"
var emailbody = "The XLSX file is attached"


GmailApp.sendEmail(emailAddress, subject, emailbody, {attachments: blobs});

Upvotes: 0

Views: 74

Answers (1)

roma
roma

Reputation: 1580

You script works fine. I tried it for simple sheet and I'm getting nice xlsx on my email.

https://docs.google.com/spreadsheets/d/1p8NzX0J1XhHFq6C1iSNWbs7h3OXzHKC7cDiQGYmuD_0/edit#gid=0

So, if your is blank it might be related to what data you have in sheet.

Are you using some =QUERY of =IMPORTXML function to get data which isn't diplayed?

Upvotes: 1

Related Questions