Send Doc as HTML email or output to webapp

Send Doc as HTML email or output to webapp

Send Doc as HTML email or output to webapp

Use the document ID along with a fetch request to the URL to return the web contents as HTML.

function getContent() {
const url = ‘https://docs.google.com/feeds/download/documents/export/Export?id=’+DOCID+’&exportFormat=html’;
const param = {
method : ‘get’,
headers : {
‘Authorization’ : ‘Bearer ‘+ ScriptApp.getOAuthToken()
},
muteHttpExceptions: true
};
let html = UrlFetchApp.fetch(url,param).getContentText();
html = html.replace(/.<\/head>/, ”); html = html.replace(/<(span|\/span|body|\/body|html|\/html)>/g, ”); html = html.replace(/ (id|class|style|start|colspan|rowspan)=”[^”]“/g, ”);
//Logger.log(html);
return html;
}
This can then also be used to output as a web app, using the HTML code from the doc for the contents of the web app.

function doGet(){
return HtmlService.createHtmlOutput(getContent());
}

You can also use the HTML contents of the doc as the htmlbody contents for emailing the doc as an html body. To use as a template you can use the replace method to select and replace text content within the doc.

function sendMyEmail(){
const email = Session.getActiveUser().getEmail();
const subject = DriveApp.getFileById(DOCID).getName();
const htmlBody = getContent();
MailApp.sendEmail(email,subject,’Body’,{
htmlBody:htmlBody
});
}

Connect to the document by its id

const DOCID = ‘1DEgXliv8_j4Je6c0YEEYyVE-lyGISQ27xHcaMENZUrQ’;

function temp(){
let doc;
try {
doc = DriveApp.getFileById(DOCID);
}
catch(err) {
throw ‘Please enter a Google Doc ID ‘ + err.message
}
const docName = doc.getName();
Logger.log(docName);
}

Full Code

const DOCID = ‘1DEgXliv8_j4Je6c0YEEYyVE-lyGISQ27xHcaMENZUrQ’;

function temp(){
let doc;
try {
doc = DriveApp.getFileById(DOCID);
}
catch(err) {
throw ‘Please enter a Google Doc ID ‘ + err.message
}
const docName = doc.getName();
Logger.log(docName);
}

function sendMyEmail(){
const email = Session.getActiveUser().getEmail();
const subject = DriveApp.getFileById(DOCID).getName();
const htmlBody = getContent();
MailApp.sendEmail(email,subject,’Body’,{
htmlBody:htmlBody
});
}