Google Team Drive使用Apps脚本在团队驱动器文件夹之间移动文件 [英] Google Team Drive Move file between team drive folders using Apps Script

查看:170
本文介绍了Google Team Drive使用Apps脚本在团队驱动器文件夹之间移动文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Appmaker中为Team Drive创建一个工作流,其中在Team Drive下有3个文件夹:
待批准
已批准
拒绝

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.

现在我的问题是如何在Team Drive文件夹之间移动文档. DriveApp.getFolderById(folderId).addFile()无法正常工作,因为我不能在Team Drive中拥有多个父母. DriveApp.getFolderById(folderId).createFile()正在工作,但是它正在创建一个具有新ID的全新文件,由于这是一个全新文件,因此无法满足我的批​​准工作流程的目的.

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.

是否有任何方法可以移动文件或复制/替换不会更改文件ID的任何操作?我也尝试了REST API,但找不到任何.

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.

推荐答案

好吧,好像我找到了答案,通过REST API,我可以更新文件的父级.我已经打了那个电话,它正在工作.

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.

这是样品.

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

更新 使用Advance Services API,我们可以实现相同的目标,这是我从 Aliaksei Ivaneichyk

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

如果您使用的是Appscript,则需要在此处启用Drive SDK高级服务.如果是Appmaker,请在设置"选项中将Drive SDK添加为服务.

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.

这篇关于Google Team Drive使用Apps脚本在团队驱动器文件夹之间移动文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆