Home > other >  Find and remove blank line
Find and remove blank line

Time:10-26

I am attempting to create a password protected way to place my signature in google docs and create a PDF. I was able to complete the password protection, add the signature, and send to PDF. This is not shown below but I use a term "{{Sig}}" already in the document to be replaced by the image file. I don't want the image to remain in the google doc. the code below removes the signature image. However, I want to put the "{{Sig}}" term back in to allow for future signatures if needed. I am able to do that by searching for "Sincerely," immediately before where "{{Sig}}" should be. However, there ends up being one additional blank line in between "{{Sig}}" and my name. Is there a method of finding and removing that line?

function DelSig(fileID)  {
  var docTarget = DocumentApp.openById(fileID);
  var docBody = docTarget.getBody();
  var docImages = docBody.getImages();
  var docText = docBody.getText();
  var docImage = docImages[0];
  docImage.removeFromParent();
  myFunction = docReplace(docTarget, "Sincerely,", "Sincerely,\n{{Sig}}");
  docTarget.saveAndClose();
  docTarget = DocumentApp.openById(fileID);
  docBody = docTarget.getBody();
}

Also, here is my docReplace function:

function docReplace(docTarget,oldStr,newStr) {
  var docBody = docTarget.getBody();
  docBody.replaceText(oldStr,newStr);
}

The three images below show what is happening. The image to the left shows how I have it to begin in the Google Doc. The center image is a screenshot of the PDF with the rectangle representing the signature. The image to the right is how it ends up in the Google Doc. The code deletes the signature image but leaves a blank line (or empty paragraph?). That empty paragraph would have a different index depending on the document. Is there any way to search for and replace that empty paragraph?

enter image description here enter image description here enter image description here

CodePudding user response:

The basic content element in a Google document is a paragraph. It' might be possible that you will be able to get the the blank line by getting the corresponding paragraph.

If you name is in the last paragraph try get the one before it.

function removeParaBeforeLast(){
  const body = DocumentApp.getActiveDocument().getBody();
  const beforeLast = body.getParagraphs().slice(-2)[0];
  beforeLast.removeFromParent();  
}

Related

CodePudding user response:

The modifications below seem to accomplish what I was after. It does not appear that you can search for an empty line; however, I was able to search for the paragraph above using the forEach loop and then appending the text I wanted onto the empty paragraph. The forEach loop for paragraphs appears to be very powerful in working with Google Docs.

Ruben's answer worked fairly well although I would have had to introduce a new variable based on the number of paragraphs from the bottom. I also ran into a problem of which I was previously unaware - even replacing the text with \n it did not add a paragraph. That led to "Sincerely," being inadvertently replaced the second time through and not present on the PDF.

function DelSig(fileID)  {
  var docTarget = DocumentApp.openById(fileID);
  var docBody = docTarget.getBody();
  var docImages = docBody.getImages();
  var docImage = docImages[0];
  docImage.removeFromParent();
  var docPars = docBody.getParagraphs();
  var fndTxt = false;
  docPars.forEach(function (curPar)  {
    if(fndTxt == true)  {
      curPar.appendText("{{Sig}}");
      fndTxt = false;
    }
    if(curPar.findText("Sincerely,") !== null) {
      fndTxt = true;
    }
  });
  docTarget.saveAndClose();
}
  • Related