使用...将Google Apps脚本文件复制到teamDrive

时间:2020-05-15 21:32:36

标签: google-apps-script

我正在尝试使用Apps脚本将Google Apps脚本文件的副本放置在共享驱动器中。

我的代码如下:

function copyFileToSharedDrive(){

var sharedDriveId = "sharedriveidcomeshere";
var sharedDrive = DriveApp.getFolderById(sharedDriveId);
var appsScriptFileId = "appsscriptfileidcomeshere";
DriveApp.getFileById(appsScriptFileId).makeCopy(sharedDrive).setName("This is a copy of the original apps script file");
}

但是,结果是一份应用程序脚本文件的副本,但是它在我的Google云端硬盘的根文件夹中,而不是在共享驱动器中。

如果我使用电子表格,Google Doc或幻灯片做完全相同的事情,那么代码的工作原理就像一个超级按钮。

我还尝试了高级Google服务,并使用了Drive API。那里没有运气...文件仍在执行代码的用户的根文件夹中创建。

  Drive.Files.copy(
    {title: "This is a copy of the appsscript file", parents: [{id: sharedDriveId}]},
    "appsScriptFileId",
    {supportsAllDrives: true}
  );

有帮助吗?

1 个答案:

答案 0 :(得分:1)

这个答案怎么样?

修改点:

  • 很遗憾,无法使用makeCopy将Google Apps脚本文件直接复制到共享驱动器。该文件被复制到MyDrive的根驱动器。我认为这可能是一个错误。

那么在这种情况下,如何使用Drive API?修改脚本后,它如下所示。

修改后的脚本:

运行脚本之前,please enable Drive API at Advanced Google services

function copyFileToSharedDrive(){
  var sharedDriveId = "sharedriveidcomeshere";
  var appsScriptFileId = "appsscriptfileidcomeshere";
  Drive.Files.copy(
    {title: "This is a copy of the original apps script file", parents: [{id: sharedDriveId}]},
    appsScriptFileId,
    {supportsAllDrives: true}
  );
}

参考文献:

已添加:

  • 您要将Google Apps脚本文件复制到共享云端硬盘中的specifc文件夹中。

为此,以下脚本如何?在此脚本中,使用以下流程。

  1. 复制Google Apps脚本文件。此时,文件已创建到根文件夹。
  2. 使用云端硬盘API的Files.patch方法将GAS文件移动到共享云端硬盘中的特定文件夹中。

示例脚本:

function copyFileToSharedDrive(){
  var destinationFolderId = "###";  // Please set the destination folder ID of shared Drive.
  var appsScriptFileId = "###";  // Please set the GAS script ID.

  var file = DriveApp.getFileById(appsScriptFileId)
  var copiedFile = file.makeCopy().setName(file.getName()).getId();
  Drive.Files.patch(
    {title: "This is a copy of the original apps script file"},
    copiedFile,
    {removeParents: "root", addParents: destinationFolderId, supportsAllDrives: true}
  );
}

注意:

  • 关于supportsAllDrivesthe official document如下。

    已弃用-请求的应用程序是否同时支持“我的驱动器”和共享驱动器。该参数仅在2020年6月1日之前有效。之后,假定所有应用程序都支持共享驱动器。

    • 通过这种方式,从2020年6月1日起,即使不使用supportsAllDrives,也可以使用共享的云端硬盘。

参考: