2

I need to take the contents of a Google Doc and turn it into the HTML code equivalent (for sending as an HTML email), via Google Apps Script.

I can read the file and get the text by cycling thru the elements in the body, but how would one get the formatting (bold, italic) into HTML? Via isBold(offset) and isItalic(offset) and cycling thru each character?

Thanks for any help. ~

MrGreggles
  • 6,113
  • 9
  • 42
  • 48

3 Answers3

1

This is perfectly doable using a document to compose your message, converting it to html format and using that as an html body in the message.

the code goes like this :

function sendAsHtmlBody(){
  var id = DocumentApp.getActiveDocument().getId();
  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(); 
  MailApp.sendEmail(Session.getEffectiveUser().getEmail(),'testHTML Body','html content',{'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"};
}

This code will ask for authorization in 2 steps, one for "normal" services (mail, documentApp, ..) and a second for the Oauth function, this latter must be triggered by calling the function from the script editor (if you make a menu and let a new user call that menu it won't work..., you have to use the script editor, see issue 677 here).

And here is a shared document( read only, make a copy to use) to test the code with different fonts, an image and some text.(almost every feature available in Google docs are translated in html... only a few alignment are missing sometimes. I use that very often without noticeable issue)

Serge insas
  • 45,904
  • 7
  • 105
  • 131
  • Note that there are still some people that reject HTML content in emails (yes I know we are in 2014 :-) ) so it is also a good idea to join a PDF version of the HTML content to the message for those people...there is a DocsList method for that and a Drive method as well. If you're interested in details, let me know but it's clearly explained in the documentation. – Serge insas Sep 20 '14 at 22:20
  • This is *really* appreciated Serge. I've been reading up on Issue 677. I'll try this out later. – MrGreggles Sep 20 '14 at 22:39
  • Yes, this issue has a lot of posts but many of them are just missing the subject... see number 39 where I clarify the request...As I explained above it is only a matter of procedure, we have to use the script editor... not a big issue for people like you and me but a bit frightening for non tech users. That's all. – Serge insas Sep 20 '14 at 22:51
  • @Sergeinsas Does it work also on the corporate version of Google Apps? – Jean-Francois T. Mar 28 '15 at 08:33
  • Yes of course. I use it on such an account too. But be aware the the services used here are deprecated and won't work for a very long time. There are other solutions using the new drive extended services – Serge insas Mar 28 '15 at 14:00
0

Is the default sharing option not enough? I know you can select the option to display in email. Just a thought.

enter image description here

lucusp
  • 175
  • 1
  • 2
  • 9
  • I didn't really make it clear, but this is via Google Apps Script. Edit made. Thanks for the reply though. – MrGreggles Sep 20 '14 at 14:02
  • Cool. I was just kind of trying to get at the purpose of scripting it. – lucusp Sep 20 '14 at 16:54
  • I believe it is still the case that you can only create a Draft email via Google Apps Script that in plain text and that attachments aren't supported. That's the stage where I am with that. It works but I have a invoice Doc that I need to send and with the current setup I need to manually save it as a PDF, download it, rename it to add the spaces which get removed when doing that, then attach it to the Draft email. So what I'm doing here is to put the contents of the Draft email into a Doc via code, typing in any special message and then sending with PDF attached from code. – MrGreggles Sep 20 '14 at 19:59
  • Please see answer above, I believe this code will help you to solve your problem efficiently.:-) – Serge insas Sep 20 '14 at 22:16
0

You may want to look for another solution, as I am as well. When I go to use this I hget a message stating:

OAuthConfig API is deprecated

Method UrlFetchApp.addOAuthService is deprecated

Community
  • 1
  • 1
Karl_S
  • 3,364
  • 2
  • 19
  • 33