在Google驱动器文件夹中,我有一个名为"TønsethandØyen"的文档。folder.getfilesbyname("TønsethandØyen")找不到它。如果我将它们的名字放在getfilesbyname中,则发现我拥有的所有其他文件。它与"Ø"有关吗?还有另一种方法吗?
是。getFilesByName()
找不到ø
和Ø
的此类字符。但是您可以使用驱动器API检索文件。通过使用此方法,还可以检索包含的文件名。请确认以下脚本。
脚本1:使用driveapp
我认为这是最简单的。
var files = DriveApp.searchFiles('title contains "Tønseth and Øyen" and trashed=false');
while (files.hasNext()) {
var file = files.next();
Logger.log("id=%s, name=%s", file.getId(), file.getName())
}
>>> id=#####, name=Tønseth and Øyen
如果"TønsethandØyen"的文件只是Google Drive上的文件,则可以使用"简单脚本"。这也可以替换您的问题的folder.getFilesByName("Tønseth and Øyen")
。
var file = DriveApp.searchFiles('title contains "Tønseth and Øyen" and trashed=false').next();
脚本2:使用高级Google服务(驱动器API V2)
为了使用此脚本,请在脚本2的高级Google Services上启用Drive API,并在API Console的脚本2和脚本3的API驱动API。
var params = {
q: "title contains 'Tønseth and Øyen' and trashed=false",
fields: "items(id, title)"
};
var res = Drive.Files.list(params);
Logger.log(res)
结果:
{
"items": [
{
"id": "#####",
"title": "Tønseth and Øyen"
}
]
}
如果您不想使用高级Google服务(驱动器API V2),也可以使用UrlFetchApp.fetch()
检索文件。在这种情况下,您可以使用Drive API V3。
脚本3:使用UrlFetchApp.fetch()
。(驱动API V3)
var filename = "Tønseth and Øyen";
var url = "https://www.googleapis.com/drive/v3/files?fields=files(id,name)&q=name+contains+'" + filename + "' and trashed=false";
var params = {
method: "GET",
headers: {"Authorization": "Bearer " + ScriptApp.getOAuthToken()},
muteHttpExceptions: true
};
var res = UrlFetchApp.fetch(url, params).getContentText();
Logger.log(res)
结果:
{
"files": [
{
"id": "#####",
"name": "Tønseth and Øyen"
}
]
}