【问题标题】:Creating a Google Document shortcut in a G suite shared drive using Google App Script使用 Google App Script 在 G Suite 共享驱动器中创建 Google Document 快捷方式
【发布时间】:2021-01-20 08:21:45
【问题描述】:

我可以使用“我的云端硬盘”中的快捷方式或“我的云端硬盘”中的文件夹轻松创建 Google 文档文件的快捷方式。这个问题和回复How to create a shortcut in Google Drive Apps Script instead of multiple parents

对这有很大帮助

但是,当我尝试对 G Suite 共享云端硬盘文件夹执行相同操作时,出现以下错误:

GoogleJsonResponseException:对 drive.files.insert 的 API 调用失败 错误:找不到文件:#FILE NUM

使用“我的云端硬盘”而非共享云端硬盘的代码是:

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

我已经查阅了文档:https://developers.google.com/drive/api/v3/shortcuts,但没有运气。

【问题讨论】:

    标签: google-apps-script google-api shortcut


    【解决方案1】:

    问题

    您在文件资源中包含supportsTeamDrives 参数。首先,此参数已弃用,如文档中所示:您现在应使用supportsAllDrives。其次,请求体(你叫resource的那个)和请求参数是有区别的。

    解决方案

    如果您查看 Drive.Files.insert() 函数签名,您会看到有 3 个选项:

    • .insert(File resource);
    • .insert(File resource, Blob mediaData);
    • .insert(File resource, Blob mediaData, Object optionalArgs);

    您使用的是第一个,这不允许指定任何请求参数。因此,您应该使用第三个。

    以下是在您的情况下如何使用它:

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

    参考

    Files v2 insert

    【讨论】:

    • 这很到位,非常感谢您如此清晰的回复和解决方案!
    【解决方案2】:

    在这里找到https://groups.google.com/g/google-apps-script-community/c/bH-kn9UW_cg

    添加参数以将supportsAllDrives 设置为true。并确保您有权将文件添加到驱动器。 高级驱动器服务使用 v2 API。您可以查看参考以获取更多详细信息。

    let sheetFile = Drive.Files.insert( newFile, blob, { convert: true, supportsAllDrives: true } );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-08-14
      • 1970-01-01
      • 2014-08-09
      • 1970-01-01
      • 2017-01-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多