Find and Replace text in entire Google Drive folder, headers, footers, google docs using a script

心已入冬 提交于 2021-01-05 07:17:27

问题


I have numerous Google Docs with a header and footer. Multiple links on our intranet to these documents exist so I don't want to make new documents. There are hundreds of documents to update.

I need to update the physical address and header/footer letterhead in these documents. I need to copy the images, formatting, tables, etc in the header and footers too.

I wish they were more like CSS or templates that could be changed across multiple documents at once.

Multiple considerations have been exhausted trying to do this:

  1. Apply a template with a new header and footer to an existing document - does not seem to be possible. Each has to be copied and pasted manually.

  2. Applications like Dreamweaver allow you to Find and Replace to all files in a folder. There is no way to do this that I could find for Google Docs.

Is there a way to automate this tedious process? Even a starting point hint!

    function replaceHeaderAndFooter() {
  
 var headerToCopyandPaste = DocumentApp.openById("<SourceDocID>").getHeader().copy();  // ID contains source header
 var footerToCopyandPaste = DocumentApp.openById("<SourceDocID>").getHeader().copy();  // ID contains source footer
  
  
  var files = DriveApp.getFolderById("<FOLDER ID>").getFiles();
  while (files.hasNext()) {
    var file = files.next();
    var doc = DocumentApp.openById(file.getId());
    
  var headerSectionToBeReplaced = doc.getHeader()
  var footerSectionToBeReplaced = doc.getFooter()
    
    headerSectionToBeReplaced.clear()
    
    headerSectionToBeReplaced = headerToCopyandPaste

  }
}

I've also tried to put everything in the template (images, paragraphs, formatting) into a single table in the header, for example. This code does not work either. The following code Results in the following error:

The parameters (String) don't match the method signature for DocumentApp.HeaderSection.appendTable

    function replaceHeaderAndFooter() {
  
 const headerToCopyandPaste = DocumentApp.openById("<SourceDocID>").getHeader().getTables().toString();  // ID contains source header
 const footerToCopyandPaste = DocumentApp.openById("<SourceDocID>").getHeader().copy();  // ID contains source footer
  
  
  var files = DriveApp.getFolderById("<FolderID>").getFiles();  //ID contains folder that has Google Docs that will have Header and Footer Replaced
  while (files.hasNext()) {
    var file = files.next();
    var doc = DocumentApp.openById(file.getId());
    
  var headerSectionToBeReplaced = doc.getHeader()
  var footerSectionToBeReplaced = doc.getFooter()
    
  headerSectionToBeReplaced.clear();

 headerSectionToBeReplaced.appendTable(headerToCopyandPaste)
    
  }
}

回答1:


Yes, there is a way, and that is using replaceText(). If your files do belong in a single folder, then it is easy to search and replace them all.

function replaceTextSpecificFolder() {
  var files = DriveApp.getFolderById("<folder_id>").getFiles();
  while (files.hasNext()) {
    var file = files.next();
    var doc = DocumentApp.openById(file.getId());
   doc.replaceText("<replace>", "<new string>");
  }
}

<folder_id> is everything that comes after “folders/” in the URL when you visit the Google Drive folder.

For example, if the URL was "https://drive.google.com/drive/folders/moqwiSADN921m03uhwdJ",then the Folder ID would be “moqwiSADN921m03uhwdJ”.

EDIT: As for the other items like formatting, tables, images, etc., you can check this documentation. Find it under its respective class on the left pane. (E.g. You can find table functions under Table Class)



来源:https://stackoverflow.com/questions/64990217/find-and-replace-text-in-entire-google-drive-folder-headers-footers-google-do

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