4

有没有将 Google 文档嵌入电子邮件的好方法?

我可以从:

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

我试过这些方法:

A)MailApp.sendEmail('john@abc.com', doc.getName(), doc.getText()); 文档中的原始文本构成了电子邮件的正文……所有格式都丢失了。

B)MailApp.sendEmail('john@abc.com', doc.getName(), doc.getUrl());文档的 URL 是消息的正文。在您单击源文档之前,不会看到文档的内容。

C)MailApp.sendEmail('john@abc.com', doc.getName(), '', { htmlBody: HtmlService.createHtmlOutput(doc.getAs('blob') });这看起来很有希望,但给出了一个错误:“不支持的转换请求。”

还有其他方法可以嵌入文档吗?

有没有办法获取文档的 HTML 版本?一旦我有了它,我就可以用htmlBody它来插入它。

4

1 回答 1

5

这已经回答了几次,这里是最初由 Henrique Abreu 建议的代码(不要在意评论说它不起作用,它确实!):

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"};
}

请注意,googleOAuth_ 功能需要特殊授权,只能使用脚本编辑器激活才能运行该功能(不能通过 UiApp 或菜单),并且该功能不会出现在“运行”列表中,因为名称末尾的下划线(这是它的工作方式)所以至少从脚本编辑器运行一次,并记住如果/当你最终与其他人共享应用程序时。有关详细信息,请参阅功能请求 677

于 2012-11-17T08:09:45.947 回答