Google script application - paste / paste document into email

Is there a way to put a Google document in an email?

I can start with:

var doc = DocumentApp.openById('1P_KZr_xpg.....xntBqvQ'); 

I tried these approaches:

A) MailApp.sendEmail(' john@abc.com ', doc.getName(), doc.getText()); The raw text from the document forms the body of the letter ... all formatting is lost.

B) MailApp.sendEmail(' john@abc.com ', doc.getName(), doc.getUrl()); The document URL is the body of the message. The contents of the document are not displayed until you go to the original document.

C) MailApp.sendEmail(' john@abc.com ', doc.getName(), '', { htmlBody: HtmlService.createHtmlOutput(doc.getAs('blob') }); This seems promising, but gives the error: "Unsupported conversion requested. "

Is there any other way to embed a document?

Is there a way to get the HTML version of a document? After that, I can use htmlBody to insert it.

+4
source share
1 answer

This has already been answered several times, here is the code originally proposed by Henrique Abreu (don't care about the comments saying that it doesn’t work!):

 function emailDocTest() { var id = 'Doc-Very-Long-ID-Here'; var url = 'https://docs.google.com/feeds/'; var doc = UrlFetchApp.fetch(url+'download/documents/Export?exportFormat=html&format=html&id='+id,               googleOAuth_('docs',url)).getContentText(); var emailAdress = Session.getEffectiveUser().getEmail(); MailApp.sendEmail(emailAdress, 'test doc send by mail as html', 'html only', {htmlBody:doc}); } function googleOAuth_(name,scope) { var oAuthConfig = UrlFetchApp.addOAuthService(name); oAuthConfig.setRequestTokenUrl("https://www.google.com/accounts/OAuthGetRequestToken?scope="+scope); oAuthConfig.setAuthorizationUrl("https://www.google.com/accounts/OAuthAuthorizeToken"); oAuthConfig.setAccessTokenUrl("https://www.google.com/accounts/OAuthGetAccessToken"); oAuthConfig.setConsumerKey('anonymous'); oAuthConfig.setConsumerSecret('anonymous'); return {oAuthServiceName:name, oAuthUseToken:"always"}; } 

Please note that the googleOAuth_ function requires special authorization, which can only be activated using the script editor to run the function (not from UiApp or from the menu) and that this function does not appear in "run" due to the underscore at the end of its name (this is how it should work), so run it at least once from the script editor and remember that if / when you end up sharing the application with someone else. For more information, see feature request 677 .

+5
source

Source: https://habr.com/ru/post/1446605/


All Articles