Rockadagio
Rockadagio

Reputation: 1

Save Gmail attachment to SHARED Google Drive

Goal: Update code below to save attachments to a "Test" folder within "Shared drive"

So far I've been successful in saving attachments to "My Drive" using the gmail2gdrive script found on https://github.com/ahochsteger/gmail2gdrive

This script uses DriveApp.getRootFolder() to find the root folder, but does not look at shared drives.
I have the "Drive" Advanced Service set up and am able to at least view up to 10 folders in the Shared Drive using getSharedDrives(), but have been unsuccessful updating the code to transfer files to a shared drives.

Upvotes: 0

Views: 496

Answers (2)

Rockadagio
Rockadagio

Reputation: 1

The Folder Drive ID can be found in the URL extension: Folder Drive ID

Use the folders' Drive IDs (from above) to fill in the appropriate section of the code below (Copies files from desired folder into the shared folder):

function movefileToSharedDrive() {
  makeCopy("", ""); //("From My Drive ID", "To Shared Drive ID")
}

function makeCopy(srcFolderId, dstFolderId) {
  var srcFolder = DriveApp.getFolderById(srcFolderId);
  var dstFolder = DriveApp.getFolderById(dstFolderId);
  var files = srcFolder.getFiles();
  while (files.hasNext()) {
    var file = files.next();
    var f = file.makeCopy(dstFolder);
    if (file.getMimeType() == MimeType.GOOGLE_APPS_SCRIPT) {
      Drive.Files.update({"parents": [{"id": dstFolderId}]}, f.getId());
    }
  }
}

Upvotes: 0

Cooper
Cooper

Reputation: 64032

Move a file from MyDrive to Shared Drive

function movefileToSharedDrive() {
  const file = DriveApp.getFileById('');//fileid
  const fldr = DriveApp.getFolderById('');//shared drive id
  Drive.Files.update({"parents": [{"id": fldr.getId()}]}, file.getId(), null, {"supportsAllDrives":true});
}

Drive API Version 2 needs to be enabled

Upvotes: 1

Related Questions