Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google apps script 谷歌应用程序脚本-在新页面上插入第二个表_Google Apps Script_Google Docs - Fatal编程技术网

Google apps script 谷歌应用程序脚本-在新页面上插入第二个表

Google apps script 谷歌应用程序脚本-在新页面上插入第二个表,google-apps-script,google-docs,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 h

我想做的是:

  • 在第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之后立即提交
  • 我阅读了文档,但由于缺少示例,我想我无法实现它
  • 对于答案,我为
    getCursor()
    命令获取NULL。另外
    element.insertPageBreak(0)
    对我来说失败,因为元素没有显示任何
    insertPageBreak()
    方法
  • 如前所述,对于答案,请在末尾添加

任何帮助都将不胜感激。

虽然我不确定您的实际文档,但如果使用新文档,这次修改如何?此修改脚本的流程如下所示

  • 插入表1
  • 插入分页符
  • 在分页符创建的新页面中插入表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"]]);
    
    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"]]);
    
    修改脚本2: 在这个脚本中,通过给出插入索引,表1、分页符和表2从插入索引中放入文档

    docId = "###";
    var body = DocumentApp.openById(docId).getBody();
    var table1 = body.insertTable(1, [["table1"]]);
    body.insertPageBreak(2);
    var table2 = body.insertTable(3, [["table2"]]);
    
    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"]]);
    
    注:
    • 我无法理解
      \u insertPageBreak()
    参考资料:

    如果我误解了你的问题,而这不是你想要的结果,我道歉。

    谢谢@Tanaike。你会想出最好的解决方案。感谢您详细的回答和完美的解决方案。你有一些惊人的谷歌应用脚本技能。
    \u insertPageBreak()
    。这只是一个添加分页符的函数。我已经更新了问题中的代码,将函数改为简单注释,以便将来的用户更容易理解问题。@urwaCFC感谢您的回复。我很高兴你的问题解决了。我也能理解
    \u insertPageBreak()
    。也谢谢你。