Google Scripts - 搜索多个文档的查找和替换功能 [英] Google Scripts - Find and Replace Function that searches multiple documents

查看:19
本文介绍了Google Scripts - 搜索多个文档的查找和替换功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Google 脚本的新手,我正在寻找一个好的起点,让自己学习能够编写完成以下任务的脚本:

I'm new to Google Scripts and am looking for a good place to start and educate myself on being able to write a script that accomplishes the following:

它会按照代码的指示扫描许多不同的谷歌文档并进行查找和替换,本质上是邮件合并.所以基本上每个文档中都已经有一个 [REPLACE TEXT HERE] 并且我会告诉脚本我希望每个 [REPLACE TEXT HERE] 更改为 [WORD A] 用于许多不同的文档.

It scans a number of different google docs and does a find and replace, essentially a mail merge, as instructed by the code. So basically each doc would have a [REPLACE TEXT HERE] already in it and I'd tell the script that I'd like each [REPLACE TEXT HERE] changed to [WORD A] for a number of different documents.

我知道这可能是一个基本要求,所以如果有一个地方可以指引我了解包含此内容的 google 脚本基础知识,那就太好了 - 我最初的研究没有任何经过磨练的东西正是针对这一特定需求.

I understand this is may be a basic request, so if there's a place to point me towards that walks me through google script basics that would include this, that'd be great - my initial research did not have anything that honed in exactly on this specific need.

感谢大家的帮助!

推荐答案

我是这样做的.(我从文档中了解了如何使用 API.)

Here's how I did it. (I learned how to use the API from the documentation.)

function myFunction() {
  var files = DriveApp.getFiles();   // Note: this gets *every* file in your Google Drive
  while (files.hasNext()) {
    var file = files.next();
    Logger.log(file.getName());
    var doc = DocumentApp.openById(file.getId());
    doc.replaceText("My search string or regex", "My replacement string");
  }
  Logger.log("Done")
}

这篇关于Google Scripts - 搜索多个文档的查找和替换功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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