如何在Team Drive中创建文件夹?

时间:2018-03-27 22:10:46

标签: google-apps-script google-drive-api gsuite google-drive-team-drive

这是我第一次提交问题,所以如果我能更好地问这个问题,请帮助我。

我想:

  • 创建文件夹
  • 在谷歌团队驱动器中
  • 嵌套在现有的Team Drive文件夹“path”
  • 来自Google Apps脚本,可以在脚本文件中或附加到Google表格

我相信这些资源告诉我如何创建Team Drive文件夹:

计划A:使用GAS创建文件夹。经过一些研究,我的理解是普通GAS无法在Team Drive中创建文件夹。

  

[18-03-15 14:55:23:674 EDT]执行失败:无法在Team Drive项目上使用此操作。 (第139行,文件" utils")[1.618秒总运行时间]

//The folder with the current name doesn't exist - create it in the parent folder
var newFolder = DriveApp.createFolder(nextFolderName);
currFolder.addFolder(newFolder);

如果我错了并且有一种简单的方法可以让addFolder()代码与Team Drive一起使用,那么这将是一场胜利。

更新1:可以使用普通GAS在Team Drive中创建一个文件夹。文件夹移动受到限制。这有效:

//The folder with the current name doesn't exist - create it in the parent folder
var newFolder = currFolder.createFolder(nextFolderName);

所以B计划:现在,我正在尝试使用Google Drive REST API v3创建该文件夹。但是,即使TEAM_ROOT_PATH_FOLDER_ID在Team Drive中,我在运行代码时只会在个人驱动器中获取无标题文件。我认为我的问题是将正确的信息提供给UrlFetchApp.fetch。就像请求正文没有被阅读一样。

参考文献:

以下是使用Google Apps脚本和云端硬盘REST API创建文件夹的代码:

var TEAM_ROOT_PATH_FOLDER_ID = "diPcXqabvgpX4tcZJZ-ms2Wg0bXf1Fem"; //TODO fill in based on parent folder

function test() {
  createTeamDriveFolder(TEAM_ROOT_PATH_FOLDER_ID);
}


function createTeamDriveFolder(parentFolderId) {

  //BUILD THE URL
  var baseUrl = "https://www.googleapis.com/upload/drive/v3/files/";

  var urlParams = { //TODO correct for this function (after you figure this out)
    supportTeamDrives: true
    //,alt: "json"
    ,uploadType: "media"
  };

  //CODE CREDIT: https://ctrlq.org/code/20514-list-team-drives-google-drive-apps-script
  var queryString = Object.keys(urlParams).map(function(p) {
    return [encodeURIComponent(p), encodeURIComponent(urlParams[p])].join("=");
  }).join("&");

  var apiUrlStr = baseUrl + "?" + queryString;


  //BUILD THE REQUEST - HEADERS AND BODY
  var token = ScriptApp.getOAuthToken();
  var requestBody = {
    mimeType: "application/vnd.google-apps.folder"
    ,name: "TeamDriveTestFolder"
    ,"parents": [parentFolderId]
  };

  var requestBodyStr = JSON.stringify(requestBody);
  Logger.log("requestBody: " + requestBodyStr);

  var requestParams = {
    method: "POST"
    ,contentType: "application/json"
    ,contentLength: requestBodyStr.length
    ,headers: {"Authorization": "Bearer " + token}
    ,payload: requestBodyStr
  };

  var response = UrlFetchApp.fetch(apiUrlStr, requestParams);

  Logger.log(response.getResponseCode());
  Logger.log(response.getContentText());
}

但是,当我通过API测试运行REST调用时,我可以在我的个人驱动器中显示一个文件夹(仍然是错误的):

supportsTeamDrives>真

WORKS(在个人驱动器根目录中创建的TeamDriveTestFolder文件夹):

{
  "mimeType": "application/vnd.google-apps.folder",
  "name": "TeamDriveTestFolder"
}

失败(404):

{
  "mimeType": "application/vnd.google-apps.folder",
  "name": "TeamDriveTestFolder",
  "parents": ["diPcXqabvgpX4tcZJZ-ms2Wg0bXf1Fem"]
}

我很感激在破解这个Team文件夹创建方面的任何帮助。

2 个答案:

答案 0 :(得分:2)

对于您的第3点"嵌套在现有团队驱动器文件夹"中,您可以直接使用GAS。以下是代码段

var parentFolder=DriveApp.getFolderById('folderId');
var newFolder=parentFolder.createFolder('name'); //replace name with Folder name.

在这里,您需要在folderId字段中传递父文件夹ID。您可以在Team Drive的URL中找到此FolderID。在下面的图像' 0AFmY7fi4434bUk9PVA'是FolderID。 enter image description here

答案 1 :(得分:1)

使用Drive advanced service (V2 API reference),创建文件夹相当简单,即使在团队驱动器中也是如此:

function getNewRootLevelFolderInTeamDrive(newTitle) {
  if(!newTitle) throw new TypeError("Missing argument for new folder's title");

  // Assumes you are working in the first Team Drive.
  var td = Drive.Teamdrives.list().items[0];
  var params = {
    supportsTeamDrives: true,
    includeTeamDriveItems: true,
  };
  // Making the folder uses Drive.Files.insert(). To create a folder, use the folder
  // MimeType, and ensure the resource is otherwise appropriately specified. You can
  // pass content, but it will be discarded, so just use `null` for the mediaBody / Blob.
  var metadata = {
    parents: [td],
    mimeType: MimeType.FOLDER,
    title: newTitle,
  };
  return Drive.Files.insert(metadata, null, params);
}

虽然上述方法创建(并返回)一个文件夹,但它会强制该文件夹成为第一个Team Drive中的根级文件夹。这是一个相当脆弱的力量"但是,我们可以将新文件夹概括为任何给定文件夹的子文件:

function makeSubFolder(parentId, title) {
  if(!parentId) throw new TypeError("Expected a string ID to be passed, identifying the parent folder in which a folder will be created.");
  if(!title) title = "Random Folder Title";

  var params = {
    supportsTeamDrives: true,
    includeTeamDriveItems: true,
    corpora: /* Review the Drive API documentation on corpora and choose the applicable option. */
  };
  var metadata = {
    parents:[],
    mimeType: MimeType.FOLDER,
    title: title,
  };
  // Get the parent folder. Because we explicitly allow team drive items, and
  // declare support for team drives, team drive folders can be used.
  var folder = Drive.Files.get(parentId, params);
  if(folder.mimeType !== MimeType.FOLDER)
    throw new TypeError("Input parent folder ID is a file of type='" + file.mimeType + "'. Folders must be type='" + MimeType.FOLDER + "'.");

  // Make the new folder in the parent folder.
  metadata.parents.push(folder);
  return Drive.Files.insert(metadata, null, params);
}

使用它们的示例函数:

function foo() {
  var rootTDFolder = getNewRootLevelFolderInTeamDrive("stack_overflown");
  var folder = makeSubFolder(rootTDFolder.id, "a subfolder of 'stack_overflown'");
  console.log(rootTDFolder);
  console.log(folder);
}