1
votes

I'm trying to place a copy of a Google Apps Script-file in a shared drive using apps script.

My code looks like this:

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

The result, however, is a copy of the apps script file, but it's in my root folder of my Google Drive, instead of in the Shared Drive.

If I do the exact same thing with a Spreadsheet, Google Doc or Slides, the code works like a charm.

I also tried the Advanced Google Services and used the Drive API. No luck there... File is still being created in the root folder of the user executing the code.

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

Any help?

1

1 Answers

1
votes

How about this answer?

Modification points:

  • Unfortunately, the Google Apps Script file cannot be directly copied to the shared Drive using makeCopy. The file is copied to the root drive of MyDrive. I think that this might be a bug.

So in this case, how about using Drive API? When your script is modified, it becomes as follows.

Modified script:

Before you run the script, 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}
  );
}

References:

Added:

  • You want to copy a Google Apps Script file to the specifc folder in the shared Drive.

For this, how about the following script? In this script, the following flow is used.

  1. Copy the Google Apps Script file. At this time, the file is created to the root folder.
  2. Move the GAS file to the specific folder in the shared Drive using the Files.patch method of Drive API.

Sample script:

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

Note:

  • About supportsAllDrives, the official document says as follows.

    Deprecated - Whether the requesting application supports both My Drives and shared drives. This parameter will only be effective until June 1, 2020. Afterwards all applications are assumed to support shared drives.

    • By this, from June 1, 2020, even when supportsAllDrives is not used, the shared Drive can be used.

Reference: