Google Apps脚本将文档导出到Gmail

时间:2017-11-02 15:43:45

标签: google-apps-script gmail export converter google-docs

我想将Google文档转换为电子邮件,例如文档中的Google功能:"文件>电子邮件协作者> "将项目本身粘贴到电子邮件中" 。 我试图在html中转换文档,但css样式不是内联的:

function convertToHtml(fileId) {
  var url = "https://docs.google.com/feeds/download/documents/export/Export?id="+fileId+"&exportFormat=html&format=html";
  var param = {
method      : "get",
headers     : {"Authorization": "Bearer " + ScriptApp.getOAuthToken()},
muteHttpExceptions:true,
contentType : "text/html; charset=UTF-8"
  };
  var html = UrlFetchApp.fetch(url,param).getContentText();
  return html;
}

我需要一个函数来在html中使用内联css转换它。

例如: 不内联:

<html>
<head>
<style> p { color: red; } </style> <link rel="stylesheet" href="style.css">
</head>
<body>
<p>Test</p>
</body>
</html>

直列:

<html>
<head></head>
<body>
<p style="color: red">Test</p>
</body>
</html>

或将Google文档转换为电子邮件的其他方式。

提前致谢

1 个答案:

答案 0 :(得分:0)

使用文件>电子邮件> 文件>下载> 获得的HTML格式与将 UrlFetchApp.fetch 与您已采用的端点。但是,可以选择使用Drive APIexport方法在HTML中获取内联样式。您只需将url更改为

var url = "https://www.googleapis.com/drive/v3/files/" + fileId + "/export?mimeType=text%2Fhtml";