Tanaike's script removes only one last paragraph and it removes it without of checking if the paragraph is empty.
If you want to remove all empty paragraphs at the end of a doc you can do it this way (beware, it makes call to API for every empty paragraph, see my updated version):
function main() {
var doc = DocumentApp.getActiveDocument();
var id = doc.getId();
var text = doc.getBody().getText();
var pgfs = text.split('\n');
while(pgfs.pop().replace(/\s+/,"") == "") remove_last_pgf(id);
}
// original Tanaike's script goes here
function remove_last_pgf(docId) {
var c = Docs.Documents.get(docId, {
fields: "body.content"
}).body.content.pop();
Docs.Documents.batchUpdate(
{
requests: [
{
deleteContentRange: {
range: { startIndex: c.startIndex - 1, endIndex: c.endIndex - 1 }
}
}
]
},
docId
);
}
Don't forget to add Google Docs API in Services of Script Editor:

Update
I've refined the script a little bit further:
function main() {
var doc = DocumentApp.getActiveDocument();
var docId = doc.getId();
var empty_tail = doc.getBody().getText().search(/\s+$/) + 1;
if (empty_tail == 0) return; // prevent the error for empty docs
var content = Docs.Documents.get(docId,{fields: "body.content"}).body.content.pop();
var range = { startIndex: empty_tail, endIndex: content.endIndex-1 };
var req = { deleteContentRange: { range } };
Docs.Documents.batchUpdate( {requests: [req] }, docId );
}
Now it should work faster since it doesn't call API for every empty line. It gets the position where the empty characters \s+
start and removes them all with one call to API.
does not work for my use
? - Iamblichus