![](/img/trans.png)
[英]Inserting Google Drive image into templated html using Google Apps Scripts
[英]HTML to Image from Google Sheets to Google Drive on Google Apps Script
我正在尝试将 html 转换为图像并将其保存到 Google Drive。 我在这个网站上找不到例子,然后我问这个问题。
我在脚本上形成 html 文本并将其发送到 email。 它看起来像这样: Email 上的 HTML
我想将 HTML 表转换为图像并将其保存到 Google Drive 一些这样的结构:
file[i] = DriveApp.createFile(img);
file[i].setName(text + "_#" + (i + 1));
我相信你的目标如下。
在这种情况下,下面的示例脚本怎么样? 在这个示例脚本中,我使用了 Class 图表。
const html = '<table border="1" style="border-collapse: collapse" width="100%" height="100%"><tr><th>colA</th><th>colB</th><th>colC</th></tr><tr><td>A1</td><td>B1</td><td>C1</td></tr><tr><td>A2</td><td>B2</td><td>C2</td></tr><tr><td>A3</td><td>B3</td><td>C3</td></tr></table>';
const img = Charts.newTableChart().setDataTable(Charts.newDataTable().addColumn(Charts.ColumnType.STRING, '').addRow([html]).build()).setOption('allowHtml', true).setDimensions(600, 300).build().getBlob();
file[i] = DriveApp.createFile(img);
file[i].setName(text + "_#" + (i + 1));
html
是一个示例 HTML 表。 请为您的 HTML 表修改它。 示例 HTML 表如下。
<table border="1" style="border-collapse: collapse" width="100%" height="100%"><tr><th>colA</th><th>colB</th><th>colC</th></tr><tr><td>A1</td><td>B1</td><td>C1</td></tr><tr><td>A2</td><td>B2</td><td>C2</td></tr><tr><td>A3</td><td>B3</td><td>C3</td></tr></table>
运行上述脚本时,得到如下图像。
在此示例脚本中,图像大小为setDimensions(600, 300)
。 所以请根据您的实际情况进行修改。
例如,如果要将 HTML 表保存为 PDF 文件,请修改img
如下。
const img = Utilities.newBlob(html, MimeType.HTML).getAs(MimeType.PDF);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.