[英]Creating a Google Document shortcut in a G suite shared drive using Google App Script
I am able to easily create shortcuts to Google Document files, with the shortcut in My Drive, or a folder within My Drive.我可以使用“我的云端硬盘”中的快捷方式或“我的云端硬盘”中的文件夹轻松创建 Google 文档文件的快捷方式。 This was helped greatly by this question and response How to create a shortcut in Google Drive Apps Script instead of multiple parents
这个问题和回复如何在 Google Drive Apps Script 中创建快捷方式而不是多个父项对此有很大帮助
However, when I try and do the same with a G Suite shared Drive folder, I am given the following error:但是,当我尝试对 G Suite 共享云端硬盘文件夹执行相同操作时,出现以下错误:
GoogleJsonResponseException: API call to drive.files.insert failed with error: File not found: #FILE NUM
GoogleJsonResponseException:对 drive.files.insert 的 API 调用失败,错误:找不到文件:#FILE NUM
The code, as working with My Drive, but not a shared drive is:与“我的云端硬盘”(而非共享云端硬盘)一起使用的代码是:
function createShortcut() {
const targetId = "TARGET DOCUMENT ID";
const shortcutName = "Test";
const folderId = "TARGET FOLDER ID";
const resource = {
shortcutDetails: { targetId: targetId },
title: shortcutName,
mimeType:"application/vnd.google-apps.shortcut",
supportsTeamDrives:true,
parents: [{id: folderId}]
};
const shortcut = Drive.Files.insert(resource);
}
I have consulted the documentation: https://developers.google.com/drive/api/v3/shortcuts with no luck.我已经查阅了文档: https : //developers.google.com/drive/api/v3/shortcuts没有运气。
You are including the supportsTeamDrives
parameter in the File resource.您将在文件资源中包含
supportsTeamDrives
参数。 First, this parameter is deprecated as shown in the documentation: you shall now use supportsAllDrives
.首先,如文档中所示,不推荐使用此参数:您现在应使用
supportsAllDrives
。 Second, there is a difference between request body (the one you called resource
) and request parameters.其次,请求正文(您称为
resource
那个)和请求参数之间存在差异。
If you look at the Drive.Files.insert()
functions signatures you will see that there are 3 options:如果您查看
Drive.Files.insert()
函数签名,您将看到有 3 个选项:
.insert(File resource)
; .insert(File resource)
;.insert(File resource, Blob mediaData)
; .insert(File resource, Blob mediaData)
;.insert(File resource, Blob mediaData, Object optionalArgs)
; .insert(File resource, Blob mediaData, Object optionalArgs)
; You are using the first one and this doesn't allow to specify any request parameters.您正在使用第一个,这不允许指定任何请求参数。 You should therefore use the third one.
因此,您应该使用第三个。
Here is how to use it in your case:以下是如何在您的情况下使用它:
// Since you are not creating nor uploading any file you shall leave the Blob mediatada parameter as null
const resource = {
shortcutDetails: { targetId: targetId },
title: shortcutName,
mimeType:"application/vnd.google-apps.shortcut",
parents: [{id: folderId}]
};
const options = { supportsAllDrives: true };
const shortcut = Drive.Files.insert(resource, null, options);
Find here https://groups.google.com/g/google-apps-script-community/c/bH-kn9UW_cg在这里找到https://groups.google.com/g/google-apps-script-community/c/bH-kn9UW_cg
Add in the parameter for supportsAllDrives set to true.添加支持所有驱动器设置为 true 的参数。 and ensure you have the permission to add the file to the drive.
并确保您有权将文件添加到驱动器。 The advanced drive services use the v2 API.
高级驱动器服务使用 v2 API。 You can check the reference for further details.
您可以查看参考资料以获取更多详细信息。
let sheetFile = Drive.Files.insert( newFile, blob, { convert: true, supportsAllDrives: true } );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.