Lolowr
Lolowr

Reputation: 79

Removing empty last page space

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.

Upvotes: 1

Views: 508

Answers (2)

Yuri Khristich
Yuri Khristich

Reputation: 14502

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.

Upvotes: 2

Lolowr
Lolowr

Reputation: 79

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 );
}

Upvotes: -1

Related Questions