Google Apps 脚本 - 在新页面上插入第二个 table
Google Apps Script - insert second table on a new page
我想做的是:
- 在第 1 页,插入 table 1
- 移至第2页,插入table 2
我可以一个接一个地插入两个 table,但在插入 table 2.
之前无法跳转到新页面
docId = "YOUR_DOC_ID";
var doc = DocumentApp.openById(docId);
var body = doc.getBody();
var table1 = body.insertTable(2, data[0]);
formatTable(table1);
// insert page break here
var table2 = body.insertTable(3, data[1]);
formatTable(table2);
doc.saveAndClose();
insertPageBreak() 是我正在努力解决的问题。
到目前为止我尝试过的:
- 我唯一能找到的
appendPageBreak
在 body
上。所以我尝试了body.appendPageBreak()
。这会在 "very" 末尾插入一个新页面。但是我在 table 1. 之后就需要它
- 我阅读了 THIS 文档,但我猜是因为缺少示例而无法实施。
- 对于 THIS 的回答,我对于
getCursor()
命令得到 NULL。另外 theElement.insertPageBreak(0)
对我来说失败了,因为元素没有显示任何 insertPageBreak()
方法。
- 对于THIS的答案,如上所述,在最后追加。
任何帮助将不胜感激。
虽然我不确定你的实际Document,但是如果使用new Document,这个修改怎么样?修改后的脚本流程如下
- 插入一个table1.
- 插入分页符。
- 在分页符创建的新页中插入一个table2。
修改后的脚本 1:
在此脚本中,table 1、分页符和 table 2 按顺序放入新文档。
docId = "###";
var body = DocumentApp.openById(docId).getBody();
var table1 = body.insertTable(1, [["table1"]]);
body.insertPageBreak(2);
var table2 = body.insertTable(3, [["table2"]]);
修改脚本 2:
在此脚本中,通过提供插入索引,table 1、分页符和 table 2 从插入索引按顺序放入文档。
var insertIndex = 1; // Please set this
docId = "###";
var body = DocumentApp.openById(docId).getBody();
var table1 = body.insertTable(insertIndex, [["table1"]]);
var pageBreak = body.insertPageBreak(body.getChildIndex(table1) + 1);
var table2 = body.insertTable(body.getChildIndex(pageBreak.getParent()) + 1, [["table2"]]);
注:
- 我无法理解
_insertPageBreak()
。
参考文献:
如果我误解了你的问题,这不是你想要的结果,我深表歉意。
我想做的是:
- 在第 1 页,插入 table 1
- 移至第2页,插入table 2
我可以一个接一个地插入两个 table,但在插入 table 2.
之前无法跳转到新页面 docId = "YOUR_DOC_ID";
var doc = DocumentApp.openById(docId);
var body = doc.getBody();
var table1 = body.insertTable(2, data[0]);
formatTable(table1);
// insert page break here
var table2 = body.insertTable(3, data[1]);
formatTable(table2);
doc.saveAndClose();
insertPageBreak() 是我正在努力解决的问题。
到目前为止我尝试过的:
- 我唯一能找到的
appendPageBreak
在body
上。所以我尝试了body.appendPageBreak()
。这会在 "very" 末尾插入一个新页面。但是我在 table 1. 之后就需要它
- 我阅读了 THIS 文档,但我猜是因为缺少示例而无法实施。
- 对于 THIS 的回答,我对于
getCursor()
命令得到 NULL。另外theElement.insertPageBreak(0)
对我来说失败了,因为元素没有显示任何insertPageBreak()
方法。 - 对于THIS的答案,如上所述,在最后追加。
任何帮助将不胜感激。
虽然我不确定你的实际Document,但是如果使用new Document,这个修改怎么样?修改后的脚本流程如下
- 插入一个table1.
- 插入分页符。
- 在分页符创建的新页中插入一个table2。
修改后的脚本 1:
在此脚本中,table 1、分页符和 table 2 按顺序放入新文档。
docId = "###";
var body = DocumentApp.openById(docId).getBody();
var table1 = body.insertTable(1, [["table1"]]);
body.insertPageBreak(2);
var table2 = body.insertTable(3, [["table2"]]);
修改脚本 2:
在此脚本中,通过提供插入索引,table 1、分页符和 table 2 从插入索引按顺序放入文档。
var insertIndex = 1; // Please set this
docId = "###";
var body = DocumentApp.openById(docId).getBody();
var table1 = body.insertTable(insertIndex, [["table1"]]);
var pageBreak = body.insertPageBreak(body.getChildIndex(table1) + 1);
var table2 = body.insertTable(body.getChildIndex(pageBreak.getParent()) + 1, [["table2"]]);
注:
- 我无法理解
_insertPageBreak()
。
参考文献:
如果我误解了你的问题,这不是你想要的结果,我深表歉意。