【问题标题】:apps-script - Create new google doc in existing folder应用程序脚本 - 在现有文件夹中创建新的谷歌文档
【发布时间】:2020-01-21 06:36:53
【问题描述】:

我正在从应用程序脚本创建一个谷歌文档,如下所示:

var newDoc = DocumentApp.create(docName);

我希望在我的驱动器的现有文件夹中创建这个新文档。我尝试了以下方法:

var dir = DriveApp.getFolderById("folder-id");
dir.addFile(newDoc);

但我得到的错误是:

ReferenceError: "DocsList" is not defined.

有什么方法可以在我的现有文件夹中创建新文档或通过应用程序脚本将我的文件移动到现有文件夹中?任何帮助将不胜感激。

【问题讨论】:

    标签: google-apps-script google-docs


    【解决方案1】:

    Folder.addFile() 要求你传递一个File,但DocumentApp.create() 返回一个Document。您需要做的是使用newDoc.getId() 获取其唯一标识符,然后在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”文件夹中,则需要将其从默认创建的根文件夹中删除。

    【讨论】:

    • 哦,这很简单,非常感谢!!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多