Google App Script | Headers and Footers | Replace text

I'm trying to built a script that replace some Data in a Google Document. I'm just observing a problem. The script works well for all the body and the footers from the 2nd to the last page. My first page footer is set as different than the others but i couldn't find a way to modify its content.

Do you have any solutions ? By the way, I'm looking for a way to open the new document in my browser at the end of the script. Any solutions too ? There's a part of my script.

  var documentId = DriveApp.getFileById('1mh8yjFpy7NcjB8meXWvn1SKQofjSMcFdPqWBe4GoC14').makeCopy().getId();
  var body = DocumentApp.openById(documentId).getBody();
  var footer = DocumentApp.openById(documentId).getFooter();
  DriveApp.getFileById(documentId).setName('RM_' + RM + '_' + IDEtude + "_" + Prenom + "_" + Nom);
  
  footer.replaceText("{RM_REF}", RM);
  footer.replaceText("{PROJECT_REF}", IDEtude);

  body.replaceText("{RM_REF}", RM);
  body.replaceText("{PROJECT_REF}", IDEtude);
  body.replaceText("{NumeroBA}", numBA);
  body.replaceText('{PrenomNom}', Prenom + " " +Nom);
  body.replaceText('{adresse}', Adresse);
  body.replaceText('{code postal et ville}', CP);
  body.replaceText('{numero de telephone}', NumTel);
  body.replaceText('{CLIENT_COMPANY}', NomClient);
  body.replaceText('{DatedeFin}', DatedeFin);
  body.replaceText('{DatedeDebut}', DatedeDebut);
  body.replaceText('{FaitLe}',Date);
  body.replaceText('{Ville}',VilleCdP);

Thanks for your help !

Upvotes: 1

Views: 2188

Answers (2)

Yuri Khristich
Yuri Khristich

Reputation: 14502

I decided to add the very important part about How to open a new Document (or any document) from GAS in new tab. Here is the way.

At the end of your main function GenererRM() you need to add this:

redirect('https://docs.google.com/document/d/' + documentId);

It calls the function redirect() with the ID of your new document. The function is below (it's need to add in your script as one more function):

function redirect(SSurl) {
  let html = HtmlService.createTemplateFromFile('redirect.html');
  html.SSurl = SSurl; 
  var htmlOutput = html.evaluate();    
  DocumentApp.getUi().showModalDialog(htmlOutput,'Bon voyage!');
}

Then you'll need to make the new file redirect.html along with your Code.gs.

enter image description here

Here is a content of this file:

<!DOCTYPE html>
<html>
  <body>
    <script>     
      var SSurl = <?= SSurl ?> ;
      window.open(SSurl,"_blank");
      google.script.host.close();
    </script>
  </body>
<html>

It's all. Now your new created document will be open in a new tab automatically every time when the function redirect() is called.

Credits: https://ru.stackoverflow.com/questions/1174330

Upvotes: 0

Yuri Khristich
Yuri Khristich

Reputation: 14502

Try to add these three lines in your Code.gs after the line 37:

first_footer = footer.getParent().getChild(3);
first_footer.replaceText("{RM_REF}", RM);
first_footer.replaceText("{PROJECT_REF}", IDEtude);

...getParent().getChild(3) — is the hack to get access to a footer on a 'different first page' (probably it's your case)

enter image description here

Upvotes: 2

Related Questions