[英]In Google-App-Script for Google Drive how do I move certain files from root to a certain folder if their title includes a certain word?
I want to use Google-App-Script to move certain files into a specific folder if their title contains a certain word 我想使用Google-App-Script将某些文件移动到特定文件夹(如果它们的标题包含特定单词)
I have tried something like this but the error message states that the method is not defined. 我已经尝试过类似的操作,但是错误消息指出该方法未定义。 Any pointers/suggestions? 有任何指示/建议吗?
This is my code: 这是我的代码:
function myFunction() {
var searchFor ='title contains "Copyright"';
var names =[];
var files = DriveApp.searchFiles(searchFor);
var destination = DriveApp.getFolderById("1xN0AUclE2t9yqlNzTwgRup3nn7G0Qsik");
while (files.hasNext()) {
files.next().destination.addFile(file);
}
} }
It looks like you did not define the "file" variable anywhere in your code. 看来您没有在代码中的任何地方定义“文件”变量。
You can either define it in your while loop, or pass files.next() as an argument to addFile(). 您可以在while循环中定义它,也可以将files.next()作为addFile()的参数传递。
Finally, you should remove files.next() from the beginning of line 7, as this chaining is incorrect and is likely causing your error! 最后,您应该从第7行的开头删除files.next(),因为此链接不正确,可能会导致您的错误!
Also, note that the file will not be removed from its original folder, but will be accessible from both locations. 另外,请注意,该文件不会从其原始文件夹中删除,但可以从两个位置访问。 If you wish to remove the file from the original location, let me know and I will edit my code to add this. 如果您想从原始位置删除文件,请告诉我,我将编辑代码以添加此文件。
Try this: 尝试这个:
function myFunction() {
var searchFor ='title contains "Copyright"';
var names =[];
var files = DriveApp.searchFiles(searchFor);
var destination = DriveApp.getFolderById("1xN0AUclE2t9yqlNzTwgRup3nn7G0Qsik");
while (files.hasNext()) {
var file = files.next();
destination.addFile(file);
}
}
or this: 或这个:
function myFunction() {
var searchFor ='title contains "Copyright"';
var names =[];
var files = DriveApp.searchFiles(searchFor);
var destination = DriveApp.getFolderById("1xN0AUclE2t9yqlNzTwgRup3nn7G0Qsik");
while (files.hasNext()) {
destination.addFile(files.next());
}
}
Note that your code doesn't do what you say it does. 请注意,您的代码并没有像您所说的那样执行。 Namely, it doesn't actually 'move' files from the root folder to the specified folder (if that's what you want to accomplish). 也就是说,它实际上并没有将文件从根文件夹“移动”到指定的文件夹(如果要完成此操作)。
In Google Drive, folders and files can have more than one parent. 在Google云端硬盘中,文件夹和文件可以有多个父对象。 Think of folders as 'labels' applied to files and other folders. 将文件夹视为应用于文件和其他文件夹的“标签”。 Calling the addFile(file) method of the Folder class will add the file to the new folder but that same file will also remain in its original parent folder(s). 调用Folder类的addFile(file)方法会将文件添加到新文件夹,但是相同的文件也将保留在其原始父文件夹中。
This could cause problems as you may accidentally remove the file from one of the folders thinking it's a copy when in fact it's the same file stored in multiple folders. 这可能会导致问题,因为您可能会意外地从一个文件夹中删除该文件,并认为它实际上是一个副本,而实际上它是存储在多个文件夹中的同一文件。
Since there's no direct way to 'move' the file to another folder in Google Drive, the actual process consists of 2 steps. 由于无法直接将文件“移动”到Google云端硬盘中的另一个文件夹,因此实际过程包括2个步骤。
Getting the list of parent folders for the file: 获取文件的父文件夹列表:
var folderIterator = file.getParents();
Adding the file to the destination folder and removing it from the previously saved parent folders 将文件添加到目标文件夹并将其从以前保存的父文件夹中删除
targetFolder.addFile(file);
//Remove from parents
while (folderIterator.hasNext()) {
folderIterator.next().removeFile(file);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.