如果我们利用 Roman Vialard 的 DriveApp 库,这是相当简单的。您需要按照此处的说明将库添加到您的项目中,然后您可以使用下面的代码。
/**
* get a String containing the contents of the given document as HTML.
* Uses DriveApp library, key Mi-n8njGmTTPutNPEIaArGOVJ5jnXUK_T.
*
* @param {String} docID ID of a Google Document
*
* @returns {String} Content of document, rendered in HTML.
*
* @see https://sites.google.com/site/scriptsexamples/new-connectors-to-google-services/driveservice
*/
function getDocAsHTML(docID) {
var doc = DriveApp.getFileById(docID);
var html = doc.file.content.src;
var response = UrlFetchApp.fetch(html);
var template = response.getContentText();
return template;
}
function sendEmail(emailAddress, attachment){
var EMAIL_TEMPLATE_ID = 'SOME_GOOGLE_DOC_ID';
var emailTemplate = getDocAsHTML(EMAIL_TEMPLATE_ID);
MailApp.sendEmail(emailAddress, "Subject", emailTemplate.getText(), {
htmlBody: emailTemplate });
}