apps-script - 在现有文件夹中创建新的 google 文档
apps-script - Create new google doc in existing folder
我正在从应用程序脚本创建一个 google 文档,如下所示:
var newDoc = DocumentApp.create(docName);
我希望在我驱动器的现有文件夹中创建这个新文档。我尝试了以下方式:
var dir = DriveApp.getFolderById("folder-id");
dir.addFile(newDoc);
但是我得到的错误是:
ReferenceError: "DocsList" is not defined.
有什么方法可以在我的现有文件夹中创建新文档或通过应用程序脚本将我的文件移动到现有文件夹吗?任何帮助将不胜感激。
Folder.addFile()
requires that you pass it a File, but DocumentApp.create()
returns a Document. What you need to do is use newDoc.getId()
to get its unique identifier, and then use it in DriveApp.getFileById()
正确移动文件。
var newDoc = DocumentApp.create(docName); // Create a Document
var docFile = DriveApp.getFileById(newDoc.getId()); // Get Document as File
var dir = DriveApp.getFolderById("folder-id"); // Get the folder
dir.addFile(docFile); // Add the file to the folder
DriveApp.getRootFolder().removeFile(docFile); // Optionally remove the file from root
另请注意,Google 驱动器文件可以存在于多个文件夹中。因此,如果您只想将文件列在 "folder-id" 文件夹中,则需要将其从默认创建它的根文件夹中删除。
我正在从应用程序脚本创建一个 google 文档,如下所示:
var newDoc = DocumentApp.create(docName);
我希望在我驱动器的现有文件夹中创建这个新文档。我尝试了以下方式:
var dir = DriveApp.getFolderById("folder-id");
dir.addFile(newDoc);
但是我得到的错误是:
ReferenceError: "DocsList" is not defined.
有什么方法可以在我的现有文件夹中创建新文档或通过应用程序脚本将我的文件移动到现有文件夹吗?任何帮助将不胜感激。
Folder.addFile()
requires that you pass it a File, but DocumentApp.create()
returns a Document. What you need to do is use newDoc.getId()
to get its unique identifier, and then use it in DriveApp.getFileById()
正确移动文件。
var newDoc = DocumentApp.create(docName); // Create a Document
var docFile = DriveApp.getFileById(newDoc.getId()); // Get Document as File
var dir = DriveApp.getFolderById("folder-id"); // Get the folder
dir.addFile(docFile); // Add the file to the folder
DriveApp.getRootFolder().removeFile(docFile); // Optionally remove the file from root
另请注意,Google 驱动器文件可以存在于多个文件夹中。因此,如果您只想将文件列在 "folder-id" 文件夹中,则需要将其从默认创建它的根文件夹中删除。