如何在团队云端硬盘中创建文件夹?
How do I create a folder in Team Drive?
这是我第一次提交问题,所以如果我能问得更好,请帮助我。
我想:
- 创建文件夹
- 在 google 团队驾驶中
- 嵌套在现有团队云端硬盘文件夹“路径”中
- 来自 Google Apps 脚本,可以在脚本文件中或附加到 Google Sheet
我相信这些资源告诉我如何创建 Team Drive 文件夹:
- https://developers.google.com/drive/v3/reference/files/create
- https://developers.google.com/drive/v3/web/simple-upload
- https://developers.google.com/drive/v3/web/folder
方案A:使用GAS创建文件夹。经过一番研究,我的理解是普通GAS还不能在Team Drive中创建文件夹。
[18-03-15 14:55:23:674 EDT] Execution failed: Cannot use this operation on a Team Drive item. (line 139, file "utils") [1.618 seconds total runtime]
//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 在团队云端硬盘中创建文件夹。被限制的是文件夹移动。这有效:
//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 在团队驱动器中,我也只会在我的个人驱动器中得到一个无标题文件。我认为我的问题是获取 UrlFetchApp.fetch 的正确信息。就像请求 body 没有被读取。
参考文献:
- Google Apps Script: UrlFetchApp Post File
- https://developers.google.com/apps-script/reference/url-fetch/url-fetch-app
这是我使用 Google Apps 脚本和 Drive 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 > true
WORKS(在个人驱动器根目录中创建的 TeamDriveTestFolder 文件夹):
{
"mimeType": "application/vnd.google-apps.folder",
"name": "TeamDriveTestFolder"
}
失败 (404):
{
"mimeType": "application/vnd.google-apps.folder",
"name": "TeamDriveTestFolder",
"parents": ["diPcXqabvgpX4tcZJZ-ms2Wg0bXf1Fem"]
}
感谢任何帮助破解此团队文件夹创建的帮助。
使用 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);
}
虽然上述方法创建(和 returns)一个文件夹,但它强制该文件夹成为第一个团队驱动器中的 root-level 文件夹。不过,这是一个非常脆弱的 "force",因为我们可以将新文件夹概括为任何给定文件夹的子文件夹:
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);
}
对于你的第3点"Nested in an existing team drive folder",你可以直接使用GAS。以下是代码片段,
var parentFolder=DriveApp.getFolderById('folderId');
var newFolder=parentFolder.createFolder('name'); //replace name with Folder name.
这里需要在folderId字段中传递父文件夹ID。您可以在 Team Drive 的 URL 中找到此 FolderID。在下图中,'0AFmY7fi4434bUk9PVA' 是 FolderID。
MakeSubfolder 是一个了不起的解决方案。
考虑将其更改为新根文件夹的全局替代方案。
如果没有指定的父文件夹,只需在 Root 上创建它即可。
此外,它与 Drive 和 SharedDrive 配合得很好。
非常感谢。
这是我第一次提交问题,所以如果我能问得更好,请帮助我。
我想:
- 创建文件夹
- 在 google 团队驾驶中
- 嵌套在现有团队云端硬盘文件夹“路径”中
- 来自 Google Apps 脚本,可以在脚本文件中或附加到 Google Sheet
我相信这些资源告诉我如何创建 Team Drive 文件夹:
- https://developers.google.com/drive/v3/reference/files/create
- https://developers.google.com/drive/v3/web/simple-upload
- https://developers.google.com/drive/v3/web/folder
方案A:使用GAS创建文件夹。经过一番研究,我的理解是普通GAS还不能在Team Drive中创建文件夹。
[18-03-15 14:55:23:674 EDT] Execution failed: Cannot use this operation on a Team Drive item. (line 139, file "utils") [1.618 seconds total runtime]
//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 在团队云端硬盘中创建文件夹。被限制的是文件夹移动。这有效:
//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 在团队驱动器中,我也只会在我的个人驱动器中得到一个无标题文件。我认为我的问题是获取 UrlFetchApp.fetch 的正确信息。就像请求 body 没有被读取。
参考文献:
- Google Apps Script: UrlFetchApp Post File
- https://developers.google.com/apps-script/reference/url-fetch/url-fetch-app
这是我使用 Google Apps 脚本和 Drive 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 > true
WORKS(在个人驱动器根目录中创建的 TeamDriveTestFolder 文件夹):
{
"mimeType": "application/vnd.google-apps.folder",
"name": "TeamDriveTestFolder"
}
失败 (404):
{
"mimeType": "application/vnd.google-apps.folder",
"name": "TeamDriveTestFolder",
"parents": ["diPcXqabvgpX4tcZJZ-ms2Wg0bXf1Fem"]
}
感谢任何帮助破解此团队文件夹创建的帮助。
使用 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);
}
虽然上述方法创建(和 returns)一个文件夹,但它强制该文件夹成为第一个团队驱动器中的 root-level 文件夹。不过,这是一个非常脆弱的 "force",因为我们可以将新文件夹概括为任何给定文件夹的子文件夹:
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);
}
对于你的第3点"Nested in an existing team drive folder",你可以直接使用GAS。以下是代码片段,
var parentFolder=DriveApp.getFolderById('folderId');
var newFolder=parentFolder.createFolder('name'); //replace name with Folder name.
这里需要在folderId字段中传递父文件夹ID。您可以在 Team Drive 的 URL 中找到此 FolderID。在下图中,'0AFmY7fi4434bUk9PVA' 是 FolderID。
MakeSubfolder 是一个了不起的解决方案。 考虑将其更改为新根文件夹的全局替代方案。
如果没有指定的父文件夹,只需在 Root 上创建它即可。
此外,它与 Drive 和 SharedDrive 配合得很好。
非常感谢。