Insert multiple table rows into document using google script

I designed this code to allow myself to add multiple rows to the table below the table row the cursor is in.

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 runs fine, but when I try to highlight the rows that were originally created by default for the Google Sheets function Table\Insert Table→

and the rows added from that program, the page freezes and I get an error

File not available Sorry, there is a problem with this file. Reboot.

If this happens, you can report a bug.

My guess is that this program is not editing the table correctly. The table displays correctly, but when trying to select original lines and new lines at the same time, everything freezes.

+3


source to share


1 answer


I agree with the KRR above; there is nothing wrong with your snippet. Something probably messed up your document and you just need to use a new one.



You should be able to just make a copy of the document (File -> Make a Copy ...) and then copy / paste the script into the new document. If the problem is with you in a new document, you will have to copy / paste all content into the document that you created from scratch (using the "+" button on docs.google.com)

0


source







All Articles