PaH
PaH

Reputation: 21

Attaching Google doc link read from Google spreadsheet to automatic email in script

The following code isn't working, giving the following error:

TypeError: Cannot find function getFileByID in object Drive.

I'm trying to first test by directly providing ID in the script:

var atta = DriveApp.getFileByID('1XD...c'); //ID is Folder and Google Doc ID I                      want to attach

MailApp.sendEmail(emailto, subject, message, {
  cc: emailcc,
  attachments:[atta]
 });

Ultimately, I want to read in the link from a Google Spreadsheet:

var range = active_actions.getRange(i+1,16) //active_actions is the spreadsheet and i+1,16 is the cell with the link

var link = range.getFormula();
var atta = DriveApp.getFileByID(link); 
MailApp.sendEmail(emailto, subject, message, {
  cc: emailcc,
  attachments:[atta]
 });

Upvotes: 1

Views: 134

Answers (1)

EvSunWoodard
EvSunWoodard

Reputation: 1280

This isn't too difficult. Google Docs/Spreadsheets/etc. CANNOT be placed as an attachment in an E-mail. This is because it isn't actually a 'physical document'. It only exists on the cloud. Instead, put the URL in the email, and it will give the receiver a little box at the bottom with the Doc looking like an attachment.

It's not perfect, but it is good enough. For more details, you can see this issue request or Google's App Script Issue page:

https://code.google.com/p/google-apps-script-issues/issues/detail?id=585&q=attachment%20types&sort=-stars&colspec=Stars%20Opened%20ID%20Type%20Status%20Summary%20Component%20Owner

EDIT (To explain data fetching in SS):

To get data out of a range object, (text, number, etc.), you should use this format.

var range = getRange();
var rangeData;
var cellValue;

while (range.hasNext()) {
  rangeData = range.next();
  cellValue = rangeData.getValue();
}

If you an accumulator variable, you can get every bit of data in one list/string/etc.

Now that you have your data, you can E-mail it, or do anything else with it.

Upvotes: 1

Related Questions