【问题标题】:Insert multiples table rows into a document with google script使用谷歌脚本将多个表格行插入到文档中
【发布时间】:2015-07-05 20:35:49
【问题描述】:

我开发了这段代码,允许自己在光标所在的表行下的表中追加几行。

var ui = DocumentApp.getUi();
var answer = DocumentApp.getUi().prompt("Add Rows", "Insert the number of rows you want to add in the box below.\nMake sure you have your cursor placed in the table you want to append the rows to.",     DocumentApp.getUi().ButtonSet.OK_CANCEL);
if(answer.getSelectedButton() == ui.Button.OK)
{
  var doc = DocumentApp.getActiveDocument();
  var pos = doc.getCursor();
  var elementin = pos.getElement();
  var tablecell = elementin.getParent();
  var tablerow = tablecell.getParent();
  var table = tablerow.getParent();

  for(var i = 0; i < answer.getResponseText(); i++)
      {
        var row = table.asTable().insertTableRow(table.getChildIndex(tablerow)+1);
        for(var j = 0; j < tablerow.getNumChildren(); j++)
            {
              row.insertTableCell(0);
            }
      }
}
doc.saveAndClose();

代码执行良好,但是当我尝试突出显示最初由默认 Google 表构建函数 Table\Insert Table→ 创建的行和从该程序附加的行时,页面冻结并且我收到错误消息

文件不可用 抱歉,这个文件有问题。请重新加载。

如果这种情况持续发生,您可以报告错误。

我认为这个程序正在错误地编辑表格。表格显示正确,但是当试图同时突出显示原始行和新行时,一切都冻结了。

【问题讨论】:

  • 试用了 sn-p 提供的代码,对我来说效果很好。您是如何在文档中创建初始表的?通过 Doc ui 还是通过不同的应用程序脚本功能?
  • 我在没有脚本的情况下手动创建了表格。我只是将鼠标悬停在表创建器上并选择了行和列。就像 jjjjoe 说的,我的文件可能只是损坏了,而且代码很好。

标签: google-apps-script google-docs tablerow


【解决方案1】:

我同意上述 KRR;你的sn-p没有问题。您的文档可能有问题,您只需使用新的即可。

您应该能够简单地制作文档的副本(文件 -> 制作副本...),然后将脚本复制/粘贴到新文档中。如果问题跟随您到新文档,那么您必须将整个内容复制/粘贴到您从头开始创建的文档中(通过 docs.google.com 上的“+”按钮)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多