我有一个脚本,它在共享的Google Drive文件夹中创建一个文件,这就是脚本:
var spr = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Klantenlijst');
var data = spr.getDataRange().getValues();
var klanNumbers = data; //some var declared before this piece of code
var file = DriveApp.createFile(fileName, JSON.stringify(klanNumbers));
这个文件需要经常更新,为此我删除了现有文件并创建了一个新文件来替换它(用新数据)。问题是,当我尝试以文件所有者以外的用户身份执行setTrashed
操作时,会弹出以下错误:
您无权执行该操作。
关于如何解决这个问题,有什么想法吗?:)
谢谢!
编辑:我可以和其他用户一起手动删除驱动器中的文件。我看过这篇文章,但我完全不同意这样的结论,即问题是";过于本地化";。环顾谷歌,你会发现同样的问题没有一个像样的解决方案。
目前的解决方案:
- 重命名文件
- 将其移动到另一个文件夹
- 在旧文件夹中创建新文件
我不会删除这篇文章,这样人们可以在这里添加其他想法。
您只能丢弃您拥有的文件。当您手动删除文件(使用GUI清除文件)时,看起来您已经清除了该文件,但实际上您并没有在其上设置垃圾标记。相反,您是在自己的Google Drive中将其从视图中删除,而不会影响其他人。所有者仍然可以看到它与您共享,任何其他合作者都不受影响。事实上,如果您按文件的全名搜索该文件,或者使用"最近使用的"文件列表等替代视图,或者使用文件的URL,您仍然可以看到该文件。
要从脚本中获得相同的效果,请使用removeFile()
。
这里有一个实用程序,它将以不同的方式对待所有者和合作者的文件,要么丢弃,要么删除
/**
* Remove the given file from view in the user's Drive.
* If the user is the owner of the file, it will be trashed,
* otherwise it will be removed from all of the users' folders
* and their root. Refer to the comments on the removeFile()
* method:
*
* https://developers.google.com/apps-script/reference/drive/drive-app#removeFile(File)
*
* @param {File} file File object to be trashed or removed.
*/
function deleteOrRemove( file ) {
var myAccess = file.getAccess(Session.getActiveUser());
if (myAccess == DriveApp.Permission.OWNER) {
// If I own the file, trash it.
file.setTrashed(true);
}
else {
// If I don't own the file, remove it.
var parents = file.getParents();
while (parents.hasNext()) {
// Remove the file from the current folder.
parents.next().removeFile(file);
}
// Remove the given file from the root of the user's Drive.
DriveApp.removeFile(file);
}
}
示例:
function test_deleteOrRemove() {
var files = DriveApp.getFilesByName('536998589.mp3');
while (files.hasNext()) {
var file = files.next();
deleteOrRemove( file );
}
}