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.

12
  • stackoverflow.com/questions/46790756/… Commented May 6, 2020 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(); Commented May 6, 2020 at 20:20
  • It works for me. Commented May 6, 2020 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. Commented May 6, 2020 at 20:33
  • 1
    @albertovielma yes I was able to get everything working after following that post. Commented May 8, 2020 at 23:15

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.