如何在文件夹中创建文件?

问题描述:

我有一个Apps脚本,它可以创建多个文件并将它们移到一个文件夹中.有没有一种方法可以将文件直接放置在文件夹中,或者我首先必须获取文件,然后将其复制到适当的文件夹中,然后再从根文件夹中删除它,如下所示:

I have an Apps Script that creates multiple files and moves them into a folder. Is there a way to place the a file directly in a folder or do I first have to get the file then copy it to the appropriate folder and then removing it from the root folder like this:

folder=DocsList.createFolder("MyFolder");
var file=DocsList.createFile(blob);
file.addToFolder(folder);
file.removeFromFolder(DocsList.getRootFolder());

此问题是,如果您打开Drive,您会看到文件首先放在根文件夹中,然后移到MyFolder.因此,在执行removeFromFolder之前会有一段时间.

The problem with this is that if you open up Drive you see the file is first placed in the root folder then moved to MyFolder. So there is a bit of a lag until the execution of removeFromFolder.

只需测试以下代码

function Test() {
  DocsList.createFolder('Folder1').createFolder('Subfolder1').createFile('File1', 'Empty');
}

它按预期工作,即在新创建的文件夹My Drive\Folder1\Subfolder1中创建了一个新的File1文档.

It works as expected, i.e. created a new File1 document in newly created folder My Drive\Folder1\Subfolder1.