Deleting empty rows in Google doc table

1,104 views
Skip to first unread message

Ryan McKinnie

unread,
Apr 17, 2019, 6:36:16 AM4/17/19
to Google Apps Script Community

I am trying to write a Google apps script that will look at a table in Google docs, scan for any row with a missing data column and then delete the entire row. I want it to continue scanning the entire table and loop that command. Any help with getting set up? I have watched some Youtube videos and have a ready made script, the problem is that it only erases the row when both columns are empty, and all my tables have only 1 column empty. Please see my images for further explanation.

One last request: Is it possible to have an app script run on all docs in a folder?

The script###

function removeBlankRows(docId) {

var document = docId?
DocumentApp.openById(docId) :
DocumentApp.getActiveDocument();

var body = document.getBody();
var search = null;
var tables = [];

// Extract all the tables inside the Google Document
while (search = body.findElement(DocumentApp.ElementType.TABLE, search)) {
tables.push(search.getElement().asTable());
}

tables.forEach(function (table) {
var rows = table.getNumRows();
// Iterate through each row of the table
for (var r = rows - 1; r >= 0; r--) {
// If the table row contains no text, delete it
if (table.getRow(r).getText().replace(/\s/g, "") === "") { // Need to change something here
table.removeRow(r);
}
}
});

document.saveAndClose();
}

Ryan McKinnie

unread,
May 3, 2019, 12:56:14 AM5/3/19
to Google Apps Script Community
any ideas????

Romain Vialard

unread,
May 3, 2019, 3:56:32 AM5/3/19
to Google Apps Script Community
It seems you are checking the entire row content, instead of the content of a specific cell.
Instead of getting the whole row content with getRow(r).getText(), you could get the content of the right column, with something like getRow(r).getCell(0).getText() (to check content of the first column).

If the empty column isn't always the same, you could add an additional loop to check each cell in a row.
for (var r = rows - 1; r >= 0; r--) {

 
var row = table.getRow(r);
 
var nbOfCells = row.getNumCells();
 
for (var s = 0; s < nbOfCells; s++) {
   
if(row.getCell(s).getText() === "") {
      table
.removeRow(r);
     
break;
   
}
 
}
}

Ryan McKinnie

unread,
May 3, 2019, 6:38:17 AM5/3/19
to Google Apps Script Community
Romain,

Thanks for your help, it works like a charm. One last question. If I want to run the same script on 100 documents, what would be the simplest way to do it?

Charles Maxson

unread,
May 3, 2019, 8:17:11 PM5/3/19
to Google Apps Script Community
Hi Ryan,

The FileIterator Class is useful especially >> https://developers.google.com/apps-script/reference/drive/file-iterator

..here's a video that shows how it can be used; may want to find the mute button first ;)

Ryan McKinnie

unread,
May 7, 2019, 4:52:16 AM5/7/19
to Google Apps Script Community
Hi Charles,

Thanks for the video and information about the FileIterator. I have tried inputting both of these codes within a google script but it's not working when I run it. Do I run the fileIterator script within the delete row script? I am a bit confused on how to run two scripts simultaneously. Thanks so much for everyone's support. 

Ryan McKinnie

unread,
May 8, 2019, 6:44:11 AM5/8/19
to Google Apps Script Community
This is the script that I am running. I know I have something wrong in the order of executing the scripts but don't know how to fix it. 

Iterate French Bac.png

Reply all
Reply to author
Forward
0 new messages