Pregunta

I am trying to send a Google Document (nothing fancy here, just a simple, script-built text document) as a plain text email attachment with Google Apps Script. I can do this manually by going into my Drive and selecting "File>Email As Attachment...". From there, a window pops up asking for the recipients, file type (Where I can choose plain text), subject, message, etc. How can I do this through script?

I have another document using the following for PDFs (Where TimeDataId and email_address are properly defined):

//Get Data as .pdf file
var TimeData = DocsList.getFileById(TimeDataId).getAs('application/pdf');

// Attach pdf and send the email to customer
var subject = 'Time Data';
var body = 'Please see the attached Data.' + '<br/><br/>Thank you,';
MailApp.sendEmail(email_address, subject, body, {htmlBody: body, attachments: TimeData});

And this works flawlessly for PDFs. However, I am really looking for something like this:

//Get Data as .txt file
var TimeData = DocsList.getFileById(TimeDataId).getAs(MimeType.PLAIN_TEXT);

// Attach txt and send the email to customer
var subject = 'Time Data';
var body = 'Please see the attached Data.' + '<br/><br/>Thank you,';
MailApp.sendEmail(email_address, subject, body, {htmlBody: body, attachments: TimeData});

All I ever get with this method is a failure notice with the report of "Authorization is required to perform that action".

I know I could just paste the plain text information into the body of my email, but I would really like to have a downloadable file, as this saves a step for the user (this file will be used later for data import into another program). Has this question already been asked and answered? Does anyone have any ideas? Thanks for any input.

¿Fue útil?

Solución

Here is how I would get text from a Document as a plain text file.

var txt = DocumentApp.openById(TimeDataId).getText();
var TimeData = Utilities.newBlob(txt, 'text/plain','myattachment.txt')

It takes an extra level of indirection but for now getAs is only supported for PDFs.

I believe you are getting that error message as the MimeType class for the new DriveApp but I haven't verified that.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top