查找&使用Google Apps脚本替换文件夹内的文件 [英] Find & replace in files within a folder using Google Apps Script
问题描述
我找到了类似的东西,
谷歌脚本 - 查找和替换功能,搜索多个文件
,但一个搜索 Google云端硬盘中的每个文件,而不是在文件夹中。由于我现在只是通过示例学习Google Apps脚本,所以我不能自己做这个小小的步骤。
编辑:进一步替换部分 然而,我找不到 好吧,我再深入挖掘,发现另一个例子, 请帮忙。 thx 第一件事就是抓住你的文件夹,这可以通过几种方式来完成。 > 如果您正在为有限的读者编写脚本,并且只使用特定的文件夹,则可以使用DriveApp.getFolderById(id)。当您导航到文件夹时,可以在您的驱动器Web应用程序的网址中找到该ID: 一次你有这个ID,你可以简单地在你引用的答案中使用相同的代码,迭代该文件夹的文件迭代器,例如: 如果您只知道文件夹的名称,则可以使用DriveApp.getFoldersByName(name)来返回文件夹迭代器。如果你知道你只有一个这个名字的文件夹,那么你只需要在迭代器中获得第一个也是唯一的元素就可以了: 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 However, I can't find the Fine, I then dig deeper, and found another example,
Google Apps Script document not accessible by replaceText(), which indicates that the 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: 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: 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: 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屋!
$ 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
键入文档。
https:// drive.google.com/a/yourcompany.com/?tab=mo#folders/0B5eEwPQVn6GOaUt6Vm1GVjZmSTQ
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(完成)
}
function myFunction(){
var folders = DriveApp.getFoldersByName(myfoldername);
var myFolder = null;
if(folders.hasNext())
myFolder = folders.next();
if(myFolder!== null){
// do stuff
} else {
//正常退出
}
$ c $进一步,如果你有多个同名的文件夹,你将不得不在while循环中迭代它们(类似于文件迭代器中的文件迭代器)代码你链接),并找到一个标记,证明这是你正在寻找的文件夹(即你可以有一个空的文件,在那里有一个特定的名称)DocumentApp.openById
returns a type of Document
right?replaceText
method in the Document
type doc:
https://developers.google.com/apps-script/reference/document/documentreplaceText
method should be invoked from var body = doc.getActiveSection()
. However, I can't find the getActiveSection
method in the Document
type doc either. https://drive.google.com/a/yourcompany.com/?tab=mo#folders/0B5eEwPQVn6GOaUt6Vm1GVjZmSTQ
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")
}
function myFunction() {
var folders = DriveApp.getFoldersByName("myfoldername");
var myFolder = null;
if (folders.hasNext())
myFolder = folders.next();
if (myFolder !== null) {
// do stuff
} else {
// exit gracefully
}