查找&使用Google Apps脚本替换文件夹内的文件 [英] Find & replace in files within a folder using Google Apps Script

查看:270
本文介绍了查找&使用Google Apps脚本替换文件夹内的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您可以发布一个代码段,找到&请替换文件夹中的所有文件?

我找到了类似的东西,
谷歌脚本 - 查找和替换功能,搜索多个文件



,但一个搜索 Google云端硬盘中的每个文件,而不是在文件夹中。由于我现在只是通过示例学习Google Apps脚本,所以我不能自己做这个小小的步骤。



编辑:进一步替换部分
$ b

DocumentApp.openById 返回一个类型的文档

然而,我找不到 replaceText 方法在文档键入文档:
https: //developers.google.com/apps-script/reference/document/document

好吧,我再深入挖掘,发现另一个例子,
无法通过replaceText()访问的Google Apps脚本文档 ,这表明应该从 var body = doc.getActiveSection()调用 replaceText 方法。但是,我无法在 getActiveSection 方法=nofollow noreferrer> Document 键入文档

请帮忙。 thx

解决方案

第一件事就是抓住你的文件夹,这可以通过几种方式来完成。 >

如果您正在为有限的读者编写脚本,并且只使用特定的文件夹,则可以使用DriveApp.getFolderById(id)。当您导航到文件夹时,可以在您的驱动器Web应用程序的网址中找到该ID:

  https:// drive.google.com/a/yourcompany.com/?tab=mo#folders/0B5eEwPQVn6GOaUt6Vm1GVjZmSTQ 

一次你有这个ID,你可以简单地在你引用的答案中使用相同的代码,迭代该文件夹的文件迭代器,例如:

  function myFunction(){
var files = DriveApp.getFolderById(0B5eEwPQVn6GOaUt6Vm1GVjZmSTQ)。getFiles();
while(files.hasNext()){
var file = files.next();
Logger.log(file.getName());
var doc = DocumentApp.openById(file.getId());
doc.replaceText(我的搜索字符串或正则表达式,我的替换字符串);

Logger.log(完成)
}

如果您只知道文件夹的名称,则可以使用DriveApp.getFoldersByName(name)来返回文件夹迭代器。如果你知道你只有一个这个名字的文件夹,那么你只需要在迭代器中获得第一个也是唯一的元素就可以了:

  function myFunction(){
var folders = DriveApp.getFoldersByName(myfoldername);
var myFolder = null;
if(folders.hasNext())
myFolder = folders.next();
if(myFolder!== null){
// do stuff
} else {
//正常退出
}

Could you post a Snippet that does find & replace across all files within a folder please?

I did find something similar, Google Scripts - Find and Replace Function that searches multiple documents

but that one search in every file in Google Drive, not within a folder. Since I'm only learning Google Apps Script by example now, I can't make that tiny step myself.

EDIT: Further on the replacing part.

The DocumentApp.openById returns a type of Document right?

However, I can't find the replaceText method in the Document type doc: https://developers.google.com/apps-script/reference/document/document

Fine, I then dig deeper, and found another example, Google Apps Script document not accessible by replaceText(), which indicates that the replaceText method should be invoked from var body = doc.getActiveSection(). However, I can't find the getActiveSection method in the Document type doc either.

Please help. thx

解决方案

The first thing is to get a hold of your folder, this can be done in a few ways.

If you are writing a script for a limited audience, and you are working with only a particular folder you can use DriveApp.getFolderById(id). The id can be found in the url of your drive web app when you navigate to a folder as such:

https://drive.google.com/a/yourcompany.com/?tab=mo#folders/0B5eEwPQVn6GOaUt6Vm1GVjZmSTQ

Once you have that id, you can simply using the same code in the answer you reference, iterate through the file iterator for that particular folder as such:

function myFunction() {
  var files = DriveApp.getFolderById("0B5eEwPQVn6GOaUt6Vm1GVjZmSTQ").getFiles();
  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")
}

Alternative way of getting the folder if you only know its name are the use of DriveApp.getFoldersByName(name) which returns a folder iterator. If you know you have only one folder of that name, then you need to simply get the first and only element in the iterator as such:

function myFunction() {
  var folders = DriveApp.getFoldersByName("myfoldername");
  var myFolder = null;
  if (folders.hasNext())
    myFolder = folders.next();
  if (myFolder !== null) {
    // do stuff
  } else {
    // exit gracefully
  }

Further if you have multiple folders with the same name, you would have to iterate through them in a while loop (similar to the file iterator in the code you linked) and find a marker that proves this is the folder you are looking for (i.e. you could have an empty file with a particular name in there)

这篇关于查找&使用Google Apps脚本替换文件夹内的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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