使用脚本在整个Google云端硬盘文件夹,页眉,页脚,Google文档中查找和替换文本 [英] Find and Replace text in entire Google Drive folder, headers, footers, google docs using a script

查看:85
本文介绍了使用脚本在整个Google云端硬盘文件夹,页眉,页脚,Google文档中查找和替换文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有许多带有页眉和页脚的Google文档.我们的Intranet上存在指向这些文档的多个链接,所以我不想制作新文档.有数百个文档要更新.

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.

我希望它们更像CSS或模板,可以一次在多个文档中进行更改.

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. 将具有新页眉和页脚的模板应用于现有文档-似乎无法实现.每个都必须手动复制和粘贴.

  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.

Dreamweaver之类的应用程序使您可以查找和替换文件夹中的所有文件.我无法在Google文档中找到做到这一点的方法.

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:

参数(字符串)与DocumentApp.HeaderSection.appendTable的方法签名不匹配

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

推荐答案

是的,有一种方法,那就是使用replaceText().如果您的文件确实属于一个文件夹,则可以轻松搜索并全部替换.

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>是访问Google云端硬盘文件夹时URL中"folders/"之后的所有内容.

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

例如,如果URL是"https://drive.google.com/drive/folders/moqwiSADN921m03uhwdJ",则文件夹ID为"moqwiSADN921m03uhwdJ".

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

至于格式,表格,图像等其他项目,您可以检查文档.在左窗格的相应类别下找到它.(例如,您可以在表格类"下找到表格函数)

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)

这篇关于使用脚本在整个Google云端硬盘文件夹,页眉,页脚,Google文档中查找和替换文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆