【发布时间】: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