Google Team Drive Move file between team drive folders using Apps Script

荒凉一梦 提交于 2019-12-17 16:52:09

问题


I am creating a workflow in Appmaker for 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 Team 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.


回答1:


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.



来源:https://stackoverflow.com/questions/49670353/google-team-drive-move-file-between-team-drive-folders-using-apps-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!