I developed this code to allow myself to append several rows to a table under the table row that the cursor is inside of.
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();
The code executes fine, but when I try to highlight rows that were originally created by the default Google table building function Table\Insert Table→ and rows appended from this program, the page freezes and I get the error message of
File unavailable Sorry, there's a problem with this file. Please reload. If this keeps happening, you can report the error.
I assume that this program is editing the table incorrectly. The table displays correct, but when attempting to highlight original rows and the new rows at the same time, everything freezes.