[英]DriveApp: searching for folders with non-Latin names
I ran into what seems to be an issue inside 'getFoldersByName()' method of the Folder class.我在Folder类的“getFoldersByName()”方法中遇到了似乎是一个问题。 If I pass a Cyrillic string as an argument, it returns the instance of FolderIterator as it should, but calling the 'next()' method always throws an error ('iterator has reached the end').
如果我传递一个 Cyrillic 字符串作为参数,它会按原样返回FolderIterator的实例,但调用'next()'方法总是抛出一个错误('迭代器已到达结尾')。
The function below finds the subfolder with a given name inside the parent folder and returns the subfolder.下面的函数在父文件夹中查找具有给定名称的子文件夹并返回子文件夹。 If the subfolder already exists, the function returns it instead of creating a new one.
如果子文件夹已经存在,函数会返回它而不是创建一个新文件夹。 Unfortunately, this only works with Latin subfolder names.
不幸的是,这只适用于拉丁文子文件夹名称。
function findSubfolder(){
var subfolderName = "Папка папка"; //string containing cyrillic characters
var parentFolder = DriveApp.getFolderById(parentFolderId);
var iterator = parentFolder.getFoldersByName(subfolderName);
var folder = (iterator.hasNext()) ? iterator.next() : parentFolder.createFolder(subfolderName);
return folder;
}
It's worth mentioning that when I check (existingSubfolder.getName() == subfolderName) it returns 'true'.值得一提的是,当我检查(existingSubfolder.getName() == subfolderName) 时,它返回“true”。
I'm not sure whether this is useful for your situation.我不确定这对您的情况是否有用。 So please think of this as one of several answers.
因此,请将此视为几个答案之一。 In my environment, I also had the situation like you.
在我的环境中,我也有你这样的情况。 In my case, it occurs for
getFilesByName()
.在我的情况下,它发生在
getFilesByName()
。 Ref I solved the situation by using searchFiles()
instead of getFilesByName()
. Ref我通过使用
searchFiles()
而不是getFilesByName()
解决了这个问题。 So I thought that this way might be able to be used for your situation.所以我认为这种方式可能可以用于您的情况。 The modified script is as follows.
修改后的脚本如下。 It uses
searchFolders()
instead of getFoldersByName()
as a workaround.它使用
getFoldersByName()
searchFolders()
而不是getFoldersByName()
作为解决方法。
function findSubfolder(){
var subfolderName = "Папка папка"; //string containing cyrillic characters
var parentFolder = DriveApp.getFolderById(parentFolderId);
var iterator = parentFolder.searchFolders("title contains '" + subfolderName + "'"); // Modified
var folder = (iterator.hasNext()) ? iterator.next() : parentFolder.createFolder(subfolderName);
return folder;
}
If I misunderstand your question and this is not useful for you, I'm sorry.如果我误解了你的问题并且这对你没有用,我很抱歉。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.