【发布时间】:2017-02-04 18:58:50
【问题描述】:
我正在使用 Google Apps 脚本管理一些 Google Drive 文件。这个项目的一部分是审查文件的属性,所以我使用的是 Drive API 而不是 DriveApp。此外,Google Apps 脚本目前可以访问 Drive REST API v2 而不是 v3。
我已经成功设置了一个属性 (id),并且能够使用该属性集提取文件。
console = Logger;
function Files (folderId) {
var optionalArgs,response
;
optionalArgs = {
q:'"'+folderId+'" in parents',
spaces:"drive"
}
do {
response = Drive.Files.list(optionalArgs);
response.items.forEach(function (file) {
console.log(file.properties);
var id = file.properties.find(function (property) {
return property.key === 'id';
});
this[id.value] = file;
},this);
} while (optionalArgs.pageToken = response.nextPageToken);
}
运行此功能时,我可以在日志中看到文件属性
[{visibility=PUBLIC, kind=drive#property, etag="3GjDSTzy841RsmcBo4Ir-DLlp20/HGzJl78t8I2IehiAlaGXTkm2-C4", value=9e18766b-1cc9-4c1b-8003-b241f43db304, key=id}]
但得到
TypeError : 无法调用未定义的方法“find”。
我无法遍历这个结果数组。在其上使用 JSON.parse 会尝试将其转换为对象,这对于具有多个属性的文件是有问题的。在其上使用 JSON.parse(JSON.stringify()) 会导致
SyntaxError: Unexpected token: u
据我了解,这是由于值未定义造成的。如果我的日志没有告诉我,我可以使用它。
【问题讨论】:
标签: javascript google-apps-script google-drive-api