Google Apps脚本-在新页面上插入第二张表

时间:2019-05-10 17:14:47

标签: google-apps-script google-docs

我想做的是:

  • 在第1页上,插入表1
  • 移至第2页,插入表2

我能够一个接一个地插入两个表,但是在插入表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()。这将在“非常”端插入一个新页面。但是我需要在表1之后。
  • 我阅读了THIS文档,但由于缺少示例,因此无法实现。
  • 对于THIS的答案,对于getCursor()命令,我得到NULL。同样,theElement.insertPageBreak(0)对我也失败了,因为element没有显示任何insertPageBreak()方法。
  • 对于THIS的答案,如上所述,附加在末尾。

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

尽管我不确定您的实际文档,但是如果使用了新文档,该修改如何?修改后的脚本的流程如下。

  1. 插入表格1。
  2. 插入分页符。
  3. 将表2插入到由分页符创建的新页面中。

修改后的脚本1:

在此脚本中,将表1,分页符和表2放入新文档中。

docId = "###";
var body = DocumentApp.openById(docId).getBody();
var table1 = body.insertTable(1, [["table1"]]);
body.insertPageBreak(2);
var table2 = body.insertTable(3, [["table2"]]);

修改后的脚本2:

在此脚本中,通过提供插入索引,将表1,分页符和表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()

参考文献:

如果我误解了您的问题,而这不是您想要的结果,我深表歉意。