1
votes

Removing empty last page space, tab from Google document using Google Apps Script

hello, I am new to Script

I often have documents containing several pages with spaces and tabs after copy / paste

I'm trying to erase everything behind the last character entered to print without having to select the number of pages

I only need the first page to print

Can you give me the lead?

The tanaikech script does not work for my use ... Too bad

https://tanaikech.github.io/2020/01/17/deleting-last-empty-page-of-google-document-using-google-apps-script/

3
Can you please clarify what's the difference between the situation described in the link you provided and the one you're currently facing? If it's the same, can you please provide more details about does not work for my use? - Iamblichus

3 Answers

1
votes

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:

enter image description here


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.

0
votes

I Obtain this error message when there is nothing to erase. how to pass through this problem ?

Error Appscript

0
votes
function main() {
  var doc = DocumentApp.getActiveDocument();
  var docId = doc.getId();
  var empty_tail = doc.getBody().getText().search(/\s+$/) + 1;
  
  var content = Docs.Documents.get(docId,{fields: 
  "body.content"}).body.content.pop();
  if (empty_tail == 0) return;
  var range = { startIndex: empty_tail, endIndex: content.endIndex-1 };
  var req = { deleteContentRange: { range } };
  Docs.Documents.batchUpdate( {requests: [req] }, docId );
}