I have a Google Docs document with multiple tables that currently contain 20 empty rows each for student data. I need to expand each table to have 40 rows instead to accommodate more students.
Manually adding rows to each table is really tedious. I tried selecting all 20 rows and right-clicking to add rows below, but doing this for every table takes forever.
I attempted to write some Google Apps Script code even though I’m not familiar with JavaScript:
function expandTables() {
for (let currentTable in DocumentApp.getActiveDocument().getTables()) {
for (let j = 0; j < 20; j++) {
currentTable.insertTableRow(20);
}
}
}
But I keep getting this error message:
Error TypeError: currentTable.insertTableRow is not a function
What’s the correct way to programmatically add rows to all tables in a Google Docs file using Apps Script? I need to double the row count efficiently without doing it manually for each table.
Your loop syntax is wrong - that’s what’s causing the error. When you use for...in with getTables(), you get index numbers as strings, not the actual table objects. You need to access the tables with bracket notation and convert the index.
Here’s the fix:
function expandTables() {
var doc = DocumentApp.getActiveDocument();
var tables = doc.getTables();
for (var i = 0; i < tables.length; i++) {
var currentTable = tables[i];
for (var j = 0; j < 20; j++) {
currentTable.insertTableRow(currentTable.getNumRows());
}
}
}
I changed insertTableRow(20) to use getNumRows() because you want to append rows at the end, not shove them all at position 20. This way each new row gets added after the last existing row. The function will hit all tables in your document automatically.
Your original code was treating table indices like actual table objects. I hit this same problem when I started using Apps Script for document automation. The for...in loop gives you string indices, not the table elements themselves. One thing the other answers missed: insertTableRow() gets slow when called repeatedly. I’ve worked with larger documents where inserting rows one by one causes timeouts. You should add error handling around those insertion calls, especially with documents that have many tables or complex formatting. Also, if your tables have merged cells or special formatting in existing rows, new rows inherit formatting from the row above where they’re inserted. This can create unexpected results, so test the script on a document copy first.
run the script from the script editor and you’re good to go. just save your doc first - apps script can be glitchy and u dont want to lose work if the table modifications mess up.