1

I have a script that updates Google Docs' header and footer (by retrieving parameters from the AODocs add-on) when a new document version is published.

My problem is that the footer.clear() method doesn't seem to erase the header (or footer) and leaves a carriage return at the top of the section. Subsequent versions keep "growing" in the footer space.

Is this a known issue? Am I doing it wrong? (I pull in a line from a template so that I can have pagination, too.

Here's a code-snippet for the footer portion:

var footerTemplate = headerFooterTemplateDoc.getFooter();
var footerParagraphs = footerTemplate.getParagraphs();

// Bring in Parameters
var title = request.parameter.title;
var owner = request.parameter.owner;
var revDate = request.parameter.revDate;
var version = request.parameter.version;
var driveFileID = request.parameter.driveFileID;   

if (!DocumentApp.openById(driveFileID).getFooter()) {
 var footer = DocumentApp.openById(driveFileID).addFooter();
 } else {
 var footer = DocumentApp.openById(driveFileID).getFooter();
 footer.clear();
 }

//Write the values
footer.clear();
footer.appendHorizontalRule();
footer.appendParagraph(footerParagraphs[0].copy());
footer.appendParagraph('Title: ' + title + ' - Owner: ' + owner);
footer.appendParagraph('Version: ' + version + ' - Last Revised: ' + revDate).setSpacingAfter(18);

2 Answers 2

1

From Apps Script's perspective, a Google Doc is structured much like an HTML document. A Google doc contains 3 main section.

  • body section
  • Header Section
  • Footer Section

Each of these sections may contains differents elements.

So, if you want to clear all elements in a Google doc, you have to clear all elements in each of his section.

function ClearAllSectionsOfDocument() {

  var doc = DocumentApp.getActiveDocument(); 
  var bodySection = doc.getBody()
  var headerSection = doc.getHeader()
  var footerSection = doc.getFooter()

  try {
     bodySection.clear();         
    } catch (e) {
     // case last element in body is partial and can't be cleared
     bodySection.appendParagraph("");
     bodySection.clear()
    }

  try {
     headerSection.clear();         
    } catch (e) {
     // case last element in header is partial and can't be cleared
     headerSection.appendParagraph("");
     headerSection.clear()
    }

  try { 
    footerSection.clear();  
    } catch (e) {
     // case last element in footer is partial and can't be cleared
     footerSection.appendParagraph("");
     footerSection.clear()
    }

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

Comments

1

I use this script, it work well:

/**
 * It clears the footer and adds the current date every time you open the document for editing
 * https://webapps.stackexchange.com/questions/102984/how-do-i-add-current-date-to-a-document-footer-automatically-in-google-docs
 */
function insertFooterDate() {
  var doc = DocumentApp.getActiveDocument();
  var footer = doc.getFooter();  //gets the footer
  footer.clear();  //clears all data in footer

  //Get date
  var date = Utilities.formatDate(new Date(), "GMT", "dd-MM-yyyy");
  var filename = doc.getName();

  footer.appendParagraph(date + '    ' + filename);  //adds date to footer with filename
}

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.