Google Apps Script Header/Footer Clear and Replace

被刻印的时光 ゝ 提交于 2021-02-16 15:17:09

问题


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);

回答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()
    }

}



回答2:


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
}


来源:https://stackoverflow.com/questions/46790756/google-apps-script-header-footer-clear-and-replace

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!