将 HTML 生成的脚本发送到 Google 具有正确 Table 页边距的文档
Send HTML Generated From Script to Google Doc with Correct Table Margins
我有一个用 Google Apps 脚本编写的脚本,它接受用户输入并生成一个 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 文档中,但是,有一个问题我似乎无法弄清楚。出于某种原因,它会在文档中任何 table 的右侧添加一个 144px
边距。因此,这些不会占据整个页面。这不是文本或图像或其他任何内容的问题。只有 table 秒。这是检查 Google 文档时看到的...
<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 代码中 table 宽度设置为 768px
。如果我通过检查更改代码以使所有边距为 0px 并将宽度设置为 768px
它完全按照我想要的方式工作。当它转换为 Google 文档时,我似乎无法覆盖这些数字。有谁知道为什么会发生这种情况?
解决方法
经过一些测试后,我认为无需手动更改文档即可解决此问题的一种可能方法是像您一样创建文档文件,然后使用 getTables() 在 [=10= 中迭代] 将文档中的所有 table 循环到
- 获取单元格内容并将其存储为数组格式(例如,三行两列的 table 为
[['cell 1','cell 2','cell 3'],['cell 4','cell 5', 'cell 6']]
)。要获取单元格内容,您可以遍历其行和列并相应地存储它们。参考一些有用的方法来实现这个 here.
- 使用 appendTable() 将 table 追加到未格式化的 table 之后,您刚刚从中获取信息(这个新创建的 table 是使用此方法添加的将继承文档的正确右边距)。
- 使用 removeChild() 删除旧的 table。
我有一个用 Google Apps 脚本编写的脚本,它接受用户输入并生成一个 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 文档中,但是,有一个问题我似乎无法弄清楚。出于某种原因,它会在文档中任何 table 的右侧添加一个 144px
边距。因此,这些不会占据整个页面。这不是文本或图像或其他任何内容的问题。只有 table 秒。这是检查 Google 文档时看到的...
<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 代码中 table 宽度设置为 768px
。如果我通过检查更改代码以使所有边距为 0px 并将宽度设置为 768px
它完全按照我想要的方式工作。当它转换为 Google 文档时,我似乎无法覆盖这些数字。有谁知道为什么会发生这种情况?
解决方法
经过一些测试后,我认为无需手动更改文档即可解决此问题的一种可能方法是像您一样创建文档文件,然后使用 getTables() 在 [=10= 中迭代] 将文档中的所有 table 循环到
- 获取单元格内容并将其存储为数组格式(例如,三行两列的 table 为
[['cell 1','cell 2','cell 3'],['cell 4','cell 5', 'cell 6']]
)。要获取单元格内容,您可以遍历其行和列并相应地存储它们。参考一些有用的方法来实现这个 here. - 使用 appendTable() 将 table 追加到未格式化的 table 之后,您刚刚从中获取信息(这个新创建的 table 是使用此方法添加的将继承文档的正确右边距)。
- 使用 removeChild() 删除旧的 table。