1

I am trying to create a letterhead template for coworkers. I have tried the following, but it does not work:

function myFunction() {
  // Display dialog boxes
  var ui = DocumentApp.getUi();
  var nameResponse = ui.prompt('Enter your name');
  var positionResponse = ui.prompt('Enter your position');
  var phoneResponse = ui.prompt('Enter your phone number');
  var docNameResponse = ui.prompt('Enter a name for your Google Doc');

  //Make a copy of the template file
  var documentId = DriveApp.getFileById('ID-goes-here').makeCopy().getId();

  //Rename the copied file
  DriveApp.getFileById(documentId).setName(docNameResponse.getResponseText());  

  //Get the document header as a variable
  var header = DocumentApp.openById(documentId).getHeader();

  //Insert the entries into the document
  header.replaceText('##name##', nameResponse.getResponseText());
  header.replaceText('##position##', positionResponse.getResponseText());
  header.replaceText('##phone##', phoneResponse.getResponseText()); 
 }

If I change the header variable to .getBody, I am able to replace the placeholder text (providing I copy it to the body section), but it does not work with getHeader.

Ognar
  • 11
  • 1
  • https://stackoverflow.com/questions/46790756/google-apps-script-header-footer-clear-and-replace – Cooper May 06 '20 at 19:48
  • I took a look and I am able to get and edit the body without issue using .getBody. however .getHeader does not seem to be working. I think I understand the link you provide and understand that header body and footer are different sections, but I just do not understand how the script I am using can use var header = DocumentApp.openById(documentId).getBody(); but not var header = DocumentApp.openById(documentId).getHeader(); – Ognar May 06 '20 at 20:20
  • It works for me. – Cooper May 06 '20 at 20:28
  • 2
    Is your header set to 'Different first page' (i.e. the first page has a different header than the rest)? In that case .getHeader() will retrieve the main header (the one on the following pages--which may be an empty header), not the first-page header. – Aaron Dunigan AtLee May 06 '20 at 20:33
  • Can you share a copy of your template document? – Aaron Dunigan AtLee May 06 '20 at 20:33
  • I think this is going to have to been an addon otherwise where will run from? If you a make a copy that won't be the document that you currently have open. If you opened a new document then where is the script? The script does work it creates and copy but you have to go find it and open it. – Cooper May 06 '20 at 20:50
  • It was the "first page has a different header..." setting. So that leads to my next question: Is there a way to change that "different header?" I'll check the documentation tonight, but it doesn't hurt to ask. - Thanks – Ognar May 06 '20 at 21:03
  • This might be what I am looking for: https://stackoverflow.com/questions/31598708/google-apps-script-targeting-header-on-doc-with-different-first-page-headers – Ognar May 06 '20 at 21:33
  • @Ognar, did the post you share helped you? – alberto vielma May 07 '20 at 07:58
  • @albertovielma I haven't gotten that far yet. Keeping it simple for now. – Ognar May 08 '20 at 14:26
  • 1
    @albertovielma yes I was able to get everything working after following that post. – Ognar May 08 '20 at 23:15
  • Found the answer here https://stackoverflow.com/a/30202004/1166642 – Sebastian G. Marinescu Jun 11 '20 at 18:38

1 Answers1

0

For anybody getting this problem in the future.

getHeader() only works for me if I go to the header in google docs double click it. Then it will open the header section and then click on "Different first page". Only then it worked for me.

Don't ask why

Nick
  • 1