谷歌脚本 - 查找和替换功能,搜索多个文件 [英] Google Scripts - Find and Replace Function that searches multiple documents

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

问题描述

我对Google脚本很陌生,正在寻找一个好地方来开始并教育自己能够编写一个脚本来完成以下任务:

它扫描一些不同的谷歌文档,并执行查找和替换,本质上是一个邮件合并,按照代码的指示。所以基本上每个文档都有一个[REPLACE TEXT HERE]已经在里面,我会告诉脚本我想每个[REPLACE TEXT HERE]都替换成[WORD A]。



我知道这可能是一个基本的要求,所以如果有一个地方指向我,通过谷歌脚本的基础知识,包括这一点,这将是伟大的 - 我最初的研究没有任何东西在这个具体的需要磨练。

感谢您的帮助每个人!

解决方案

我是这么做的。 (我学习了如何使用文档中的API。)

  function myFunction(){
var files = DriveApp.getFiles(); //注意:这会在您的Google云端硬盘
中获得每个*文件,而(files.hasNext()){
var file = files.next();
Logger.log(file.getName());
var doc = DocumentApp.openById(file.getId());
doc.replaceText(我的搜索字符串或正则表达式,我的替换字符串);

Logger.log(完成)
}


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:

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.

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.

Thanks for your help everyone!

解决方案

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

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

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