英文:
Exception: Access denined: DriveApp when trying to setTrashed(true)
问题
-
我正在编写一个脚本,用于复制文档模板并从表格中提取数据,填充到模板中的占位符,并将链接更新回表格。
-
它还有一个模式,如果上次运行时存在链接,可以删除旧文件。
-
使用创建脚本的帐户运行时,没有任何问题,但当使用不同的帐户运行时,会导致“异常:拒绝访问:DriveApp”(已授予第二个帐户对文件夹的访问权限)
尽管我已经尝试在setTrashed(true)
之前将访问权限设置为任何人,如下所示:
Drive.Permissions.insert(
{
role: 'writer',
type: 'anyone',
value: '',
},
oldDocId
);
Utilities.sleep(2000); // 等待2秒以确保调用完成
或者尝试将写入权限设置为当前用户,如下所示:
const currentUserEmail = Session.getActiveUser().getEmail();
Drive.Permissions.insert(
{
role: 'writer',
type: 'user',
value: currentUserEmail,
},
oldDocId
);
Utilities.sleep(2000); // 等待2秒以确保调用完成
我保存文档的文件夹属于一个组织,我注意到文档会自动将所有者设置为管理员,而不是运行脚本的人,但如果使用相同的用户(而不是管理员)第二次运行时,它可以正常运行。
英文:
The context:
-
I writing a script to copy a doc template and grab the data from the sheet to filling into placeholder in the template and update the link back to the sheet.
-
It also have a mode to delete old file if there's a link existing from last run.
-
I have no problem running it with the account that I use to create the script, but when using different account to run it, its cause
Exception: Access denined: DriveApp
(access to the folder was grant to the 2nd account)
Although I already try to set the access writer to anyone before the setTrashed(true)
by using:
Drive.Permissions.insert(
{
role: 'writer',
type: 'anyone',
value: '',
},
oldDocId
);
Utilities.sleep(2000); // sleep for 2 sec to make sure the call is finished
or even try to set the writer to the current user by:
const currentUserEmail = Session.getActiveUser().getEmail();
Drive.Permissions.insert(
{
role: 'writer',
type: 'user',
value: currentUserEmail,
},
oldDocId
);
Utilities.sleep(2000); // sleep for 2 sec to make sure the call is finished
The folder that I'm save the document is belong to an organization, I noticed that the document automatically set owner to the admin, not the one who run the script, but its running ok on 2nd run if using the same user (not the admin).
答案1
得分: 1
你遇到的问题根本原因是文件夹访问权限没有正确添加;请记住,即使你在代码中添加了权限:
Drive.Permissions.insert(
{
role: 'writer',
type: 'anyone',
value: '',
},
oldDocId
);
运行脚本的人没有足够的文件夹权限,因此无法向文件夹添加权限,这导致了错误消息。
有两种解决方法可解决此问题,我推荐的是使用 Google Drive 网页界面将文件夹与将使用脚本的用户共享。
另一种方法是使用第三方工具,在 Google Apps Script 中使用 OAuth 库 进行冒名顶替,前提是你拥有 Google Workspace。如果你使用 Workspace,你可以为其创建一个用户,并将文件夹与该用户共享,然后在你的应用脚本中冒名顶替该用户。
英文:
The root of the problem you are having is that the permissions for access to the folder are not been added properly; remember that even if you add the permissions on the code using:
Drive.Permissions.insert(
{
role: 'writer',
type: 'anyone',
value: '',
},
oldDocId
);
The one that is running the script doesn't have permissions over the folder, so they cannot add the permission to the folder, which generated the error message.
There are 2 workarounds that you can do to solve the issue, the one I recommend is to share the folder with the users that will use the script using the web UI of Google Drive.
The other one, is to use a 3rd party to use impersonation in Google Apps Script with the OAuth library, this only work if you have Google Workspace. If you have Workspace, you can create a user for, and share the folder with that user, and impersonate that user inside your apps script.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论