Google Apps脚本中是否有一种方法可以根据挂起的所有者的电子邮件ID来获取其所有者的文件?我实际上是在尝试使用Google Apps脚本实现transfer drive files to a new owner,我正在使用this code查找所有已暂停用户的详细信息:
/**
* Fetches the suspended user details from the AdminDirectory.
*/
function fetchUser() {
// Set the constant options only once.
const options = {
domain: 'xyz.com',
orderBy: 'email',
query: 'isSuspended=true',
maxResults: 500,
fields: "nextPageToken,users"
};
// Could log the options here to ensure they are valid and in the right format.
const results = [];
do {
var search = AdminDirectory.Users.list(options);
// Update the page token in case we have more than 1 page of results.
options.pageToken = search.nextPageToken;
// Append this page of results to our collected results.
if(search.users && search.users.length)
Array.prototype.push.apply(results, search.users);
} while (options.pageToken);
//Logger.log(results);
for(var k = 0; k < results.length; k++){
var fullEmail = results[k].primaryEmail;
Logger.log(fullEmail);
fetchFiles(fullEmail);
}
}
/**
* Fetches the files of the suspended users based on their email.
*/
function fetchFiles(email){
var pageToken;
var filesList = Drive.Files.list({ // Invalid value error is thrown here, I am not sure if this the right way to use Drive API in google script
domain: 'xyz.com',
orderBy: 'email',
q: "'email' in owners",
maxResults: 500,
pageToken: pageToken
});
Logger.log('filesList:' +filesList);
}
我正在尝试实现Transfer ownership of a file to another user in Google Apps Script之类的东西,但是有什么方法可以使我从上面的代码中获得的详细信息中提取用户的文件,基本上返回了以下输出:
[
{
orgUnitPath = /,
ipWhitelisted = false,
gender = {type = other},
creationTime = 2017-06-13T14:38:44.000Z,
isMailboxSetup = true,
isEnrolledIn2Sv = false,
kind = admin#directory#user,
suspensionReason = ADMIN,
isAdmin = false,
suspended = true,
emails = [{
address = john.seb@xyz.com,
primary = true
}],
lastLoginTime = 2017-09-12T00:27:00.000Z,
isDelegatedAdmin = false,
isEnforcedIn2Sv = false,
changePasswordAtNextLogin = false,
customerId = v12idsa,
name = {
givenName = John,
familyName = Seb,
fullName = John Seb
},
etag = "npJcgeAc7Xbfksfwer22344/sfasdfaffUDsfdsjfhsfhsdfw-ec",
id = 1033392392142423424,
primaryEmail = john.seb@xyz.com,
agreedToTerms = true,
includeInGlobalAddressList = true
},
...
]
我正在尝试在Google Apps脚本中使用Drive API,以便使用其电子邮件访问已暂停用户的文件,但会抛出该电子邮件
“无效值”错误
对于行Drive.Files.list
,我不确定这是否是在Google脚本中使用DRIVE API
的正确方法,还有其他方法可以在Google脚本中使用此api吗?
答案 0 :(得分:1)
在函数fetchFiles
中,您永远不会使用输入参数email
。而是,您在Drive REST API中查询文本'email' in owners
。由于文本字符串'email'
不是有效的电子邮件地址,因此您可以正确收到错误消息“无效值”。
而不是此代码:
/**
* Fetches the files of the suspended users based on their email.
*/
function fetchFiles(email){
var pageToken;
var filesList = Drive.Files.list({
domain: 'jerseystem.org',
orderBy: 'email',
q: "'email' in owners",
maxResults: 500,
pageToken: pageToken
});
Logger.log('filesList:' +filesList);
}
您应该首先执行替换,以设置所有常量选项(正如我在其他问题中提到并演示的那样),然后重复查询Drive API以查找具有该电子邮件地址的用户所拥有的文件:
function fetchAllFilesOwnedByEmail(email) {
const searchParams = {
corpora: 'some corpora',
orderBy: 'some ordering criteria',
q: "'" + email + "' in owners",
fields: 'nextPageToken,items(id,title,mimeType,userPermission)'
};
const results = [];
do {
var search = Drive.Files.list(searchParams);
if (search.items)
Array.prototype.push.apply(results, search.items);
searchParams.pageToken = search.nextPageToken;
} while (searchParams.pageToken);
return results;
}
您需要查看Drive REST API文档,至少请参阅Drive.Files.list
和Search for Files。如果没有,请不要忘记enable the Drive
advanced service。
还请注意,尽管上述代码可以解决您遇到的"Invalid Value"
错误,但即使您以G-Suite管理员身份执行代码,也不一定会使用户的文件显示出来。您的研究至少应该提出以下两个相关问题: