问题描述
我想按其名称查找文本文件,然后获取其内容并将其放入var中.
I want to find text files by their name and then get their contents and make them into a var.
我试图按文件名查找文件,但是它似乎不起作用.我对如何找到文件内容一无所知.
I have tried to find the file by its name, but it doesn't seem to work. I'm clueless as to how to find the file contents though.
我找到文件的代码:
function testThing() {
var findquestions = DriveApp.getFilesByName('tempquestions.txt')
Logger.log(findquestions)
}
我希望它记录所发现的内容,但输出不过是:"FileIterator".我不知道那是什么意思.
I want it to log what it found, but the output is nothing but: "FileIterator". I don't know what that means.
推荐答案
您可以在文档中看到 .getFilesByName
返回 fileiterator
.什么是文件迭代器?该文档指出
As you can see in the documentation, .getFilesByName
return fileiterator
. What's a file iterator? The documentation states
可能存在大量同名文件.该迭代器提供对所有这些文件的访问.
There may be large amount of files with the same name. This iterator provides access to all those files.
哪些方法可以从fileIterator
访问file
? method
可以.
What methods provide access to file
from fileIterator
? This method
does.
如何获取此类文件的内容?从file
和< blob
来自blob
How to get contents of such file? Get blob
from file
and getDataAsString
from blob
Logger.log(DriveApp
.getFilesByName('tempquestions.txt') //fileIterator
.next() //file(first file with that name)
.getBlob() //blob
.getDataAsString() //string
)
这篇关于通过名称在Google云端硬盘上查找.txt文件并获取其内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!