Google Apps Script简单递归脚本运行非常慢



我不小心将Google Drive文件夹设为私有(通过右键单击share->set to private),它将权限递归地更改为私有…

文件夹结构如下

videos
--videos1
----1
------A
--------vid0192.mp4
--------vid2319.mp4
--------etc...
------B
--------vid7192.mp4
--------vid2919.mp4
--------etc...
----2
------A
--------vid0192.mp4
--------vid2319.mp4
--------etc...
------B
--------vid7192.mp4
--------vid2919.mp4
--------etc...
----3 etc...
--videos2 ...

在"视频"文件夹(videos1, videos2等)中大约有500个文件夹,每个文件夹包含5个文件夹(1,2,3,4,5),每个文件夹包含2-3个文件夹(A, B, C)。每个字母文件夹包含多个文件,大约5-10个,通常是视频。

我写了一个Apps Script来修复我的严重错误,但令我惊讶的是,它运行得非常慢。

代码如下:

function setAllFilesPermissions(folderId) {
// Get the folder using the provided folderId
var folder = DriveApp.getFolderById(folderId);

// Set the permissions for all files and subfolders in the current folder
setPermissionsRecursively(folder);
}
function setPermissionsRecursively(folder) {
// Get all files in the current folder
var files = folder.getFiles();
// Iterate through the files and set their permissions
while (files.hasNext()) {
var file = files.next();
Logger.log(file);
file.setSharing(DriveApp.Access.ANYONE, DriveApp.Permission.VIEW);
}
// Get all subfolders in the current folder
var subfolders = folder.getFolders();
// Iterate through the subfolders and call the function recursively
while (subfolders.hasNext()) {
var subfolder = subfolders.next();
Logger.log(folder);
folder.setSharing(DriveApp.Access.ANYONE, DriveApp.Permission.VIEW);
setPermissionsRecursively(subfolder);
}
}
function main() {
setAllFilesPermissions("folder-id");
}

我怎样才能使它运行得更快?(或者,我如何解决这个权限问题)

感谢@Tanaike,这个问题解决了。

奇怪的是,将videos文件夹更改为private,然后再更改为public确实有效!

我已经这样做了,但可能发生了一个错误,并不是所有的文件都受到影响。再做一次效果很好。

但是,如果有人对脚本有一个想法,为什么它这么慢,这将是一个伟大的贡献。

最新更新