Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

File movement between Google Shared Drive (team drive) folders using Apps Script

I am creating a workflow for Shared Drive (Team Drive) where I have 3 folders under team drive:
TO BE APPROVED
APPROVED
REJECTED

I am sending a document from TO BE APPROVED folder for approval, if user approves it then this document should move to APPROVED folder. Same logic for REJECTED.

Now my question is how can I move a document between Shared Drive folders. DriveApp.getFolderById(folderId).addFile() is not working as I can not have more than one parent in Team Drive. DriveApp.getFolderById(folderId).createFile() is working but it is creating a whole new file with new ID which is not fulfilling my purpose of approval workflow as this is a whole new file.

Is there any way to move file or copy/replace any operations which will not change my file's ID? I tried for REST APIs as well but couldn't found any.

like image 995
Darpan Sanghavi Avatar asked Apr 05 '18 10:04

Darpan Sanghavi


People also ask

Can I move files between Google shared drives?

Move files out of shared drivesIf you have Manager access to a shared drive, you can move files and folders out of shared drive to My Drive or another shared drive. To move files from a shared drive to another, you need Contributor, Content manager, or Manager access in the destination shared drive.

How do I move multiple files from Google Drive to a shared folder?

For example, if you want to move a file to a folder in Google Drive, you can click it, then drag it to the folder you want to, and drop it. Likewise, if you want to move multiple files to a folder in Google Drive, you can select them, and then drag and drop them to the target folder.

Can you drag and drop between Google Drive folders?

If you're using the latest versions of Chrome or Firefox, you can simply drag-and-drop files directly from your computer into Google Drive. You can even drag-and-drop files directly into folders or sub-folders. Upload files using Google Drive. Follow the steps below to select files to upload to Google Drive.

Is Google shared Drive the same as team Drive?

Google shared drives (formerly known as Team Drives) are a feature in Google's Workspace for Education. Unlike files in My Drive, files in shared drives are owned by the team/group rather than an individual. Refer below for a table that outlines the key differences between U-M Google shared drives and My Drive.


1 Answers

Okay looks like I found an answer, via REST API I can update file's parents. I've made that call and it's working.

Here's the sample.

var apiUrl = "https://www.googleapis.com/drive/v3/files/fileId?addParents=newFolderId&removeParents=oldFolderId&supportsTeamDrives=true";
var token = ScriptApp.getOAuthToken();
var header = {"Authorization":"Bearer " + token};
var options = {
"method":"PATCH",
"headers": header
};
var res = UrlFetchApp.fetch(apiUrl, options);

UPDATE Using Advance Services API we can achieve the same, here's the answer I've received from Aliaksei Ivaneichyk

function moveFileToFolder(fileId, newFolderId) {  
  var file = Drive.Files.get(fileId, {supportsTeamDrives: true});

  Drive.Files.patch(file, fileId, {
    removeParents: file.parents.map(function(f) { return f.id; }),
    addParents: [newFolderId],
    supportsTeamDrives: true
  });
}

Here you need to Enable Drive SDK advance services if you are using Appscript. In case of Appmaker Add Drive SDK as Service in Settings Option.

like image 111
Darpan Sanghavi Avatar answered Sep 28 '22 08:09

Darpan Sanghavi