使用 Apps 脚本在 Google 共享驱动器(团队驱动器)文件夹之间移动文件 [英] File movement between Google Shared Drive (team drive) folders using Apps Script

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

问题描述

我正在为共享驱动器(团队驱动器)创建一个工作流,其中我在团队驱动器下有 3 个文件夹:
待批准
已批准
拒绝

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

我正在从 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.

现在我的问题是如何在共享驱动器文件夹之间移动文档.DriveApp.getFolderById(folderId).addFile() 不起作用,因为我在 Team Drive 中不能有多个父级.DriveApp.getFolderById(folderId).createFile() 正在工作,但它正在使用新 ID 创建一个全新的文件,这不符合我的审批工作流程的目的,因为这是一个全新的文件.

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.

有没有办法移动文件或复制/替换任何不会改变我文件 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.

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

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