简体   繁体   English

使用 Google App Script 在 G 套件共享驱动器中创建 Google 文档快捷方式

[英]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没有运气。

Problem问题

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那个)和请求参数之间存在差异。

Solution解决方案

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);

Reference参考

Files v2 insert 文件 v2 插入

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.

相关问题 是否可以将 Google Apps Script Web App 发布到 G Suite Marketplace? - Is it possible to publish a Google Apps Script Web App to G Suite Marketplace? 使用 Google Apps 脚本创建共享云端硬盘(以前称为团队云端硬盘) - Create a Shared Drive (formerly Team Drive) using Google Apps Script 应用程序脚本不发送 email 包含指向给定 G Suite 域上的 Google Drive 文件的链接,没有错误消息 - Apps script does not send email containing links to Google Drive files on a given G Suite domain, no error message 共享脚本导致Google Drive错误 - Google Drive Error with Shared Script 使用Google云端硬盘和Google Apps脚本自动生成日历文档 - Automatically generate a calendar document using Google Drive and Google Apps Script 使用Google Apps脚本将Google云端硬盘图片插入文档 - Inserting a Google Drive image into a document using Google Apps Script 从谷歌表格应用程序脚本创建文档 - Creating document from google sheets app script 使用 Google Script 创建和打开新文档 - Creating and opening a new Document using Google Script 使用Google脚本将共享文件移到“我的云端硬盘”中吗? - Move a shared file into My Drive using Google Script? 使用 REST Apps 脚本获取共享驱动器时未找到 Google 共享驱动器 - Google Shared drive not found when getting Shared Drive using REST Apps Script
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM