html - 将脚本生成的 HTML 发送到具有正确表格边距的 Google Doc
问题描述
我有一个用 Google Apps Script 编写的脚本,它接受用户输入并生成一个 HTML 文档。这一切都正确格式化,一切都按预期工作。我接下来要做的是获取该 HTML 并通过以下代码将其发送到 Google Docs:
var html = HtmlService.createHtmlOutput(htmlbody); //printing out 'html' gives you the expected html generated from the script.
var hBlobber = html.getBlob();
var datePDF = Utilities.formatDate(new Date(), sendDetails[3], 'yyyyMMdd');
var title = datePDF + ' - ' + subject.substring(0,subject.indexOf(':'));
var DocID = Drive.Files.insert({title: title}, hBlobber, {convert:true}).id;
//Drive.Files.insert({title: 'html'},hBlobber2);
var margins = {};
margins[DocumentApp.Attribute.MARGIN_BOTTOM]=18;
margins[DocumentApp.Attribute.MARGIN_LEFT]=18;
margins[DocumentApp.Attribute.MARGIN_RIGHT]=18;
margins[DocumentApp.Attribute.MARGIN_TOP]=18;
margins[DocumentApp.Attribute.PAGE_WIDTH]=600;
var doc2 = DocumentApp.openById(DocID).getBody().editAsText().setAttributes(margins);
DocumentApp.openById(DocID).saveAndClose();
这会将其复制到 Google Doc,但是,有一个问题我似乎无法弄清楚。出于某种原因,它会144px
在文档中任何表格的右侧添加一个边距。因此,这些不会占据整个页面。这对于文本或图像或其他任何东西都不是问题。只有表。这是检查 Google Doc 时看到的内容...
<div class="kix-tablerenderer-container" style="margin: 0px 144px 0px 0px;">
<table class="kix-tablerenerer-table" dir="ltr" style="margin-left: 0px; margin-right: 0px; width: 624px;">
在 HTML 代码中,表格宽度设置为768px
. 如果我通过检查更改代码以使所有边距为 0px 并将宽度设置为768px
它完全符合我的要求。当它转换为 Google Doc 时,我似乎无法覆盖这些数字。有谁知道为什么会发生这种情况?
解决方案
解决方法
经过一些测试后,我认为无需手动更改文档即可解决此问题的一种可能方法是像您一样创建文档文件,然后使用getTables()循环遍历for
文档中的所有表以
- 获取单元格内容并将其以数组格式存储(例如,这是一个三行两列的表格
[['cell 1','cell 2','cell 3'],['cell 4','cell 5', 'cell 6']]
)。要获取单元格内容,您可以遍历其行和列并相应地存储每个内容。参考一些有用的方法来实现这里。 - 使用appendTable()将表格附加到您刚刚从中获取信息的未格式化表格之后(使用此方法添加的这个新创建的表格将继承文档的正确右边距)。
- 使用removeChild()删除旧表。
推荐阅读
- google-cloud-platform - 所需的复合索引不存在,但在 index.yaml 中定义
- java - Kafka Consumer 输出过多的 DEBUG 语句
- azure - 如何在 Azure cosmos DB 中使用双引号保存数据
- c++ - 使用 sftp_open() 在 C++ 中使用 SFTP libssh 将文件从本地复制到远程时,文件返回 NULL 值
- bootloader - 如何从保护模式(或 C 代码)调用 PXE API?
- java - AnimationDrawable 在片段中不起作用。
- ios - GoogleService-Info.plist 文件中的 iOS Firebase IS_ADS_ENABLED 标志
- java - 错误的中位数答案
- variables - Pentaho 数据集成 setVariable 不起作用
- javascript - 使用 Javascript 中的对象对数组项进行分组