【问题标题】:Create a Google Doc in a Specific Folder在特定文件夹中创建 Google Doc
【发布时间】:2013-03-02 23:56:55
【问题描述】:

我正在尝试在变量 chapterOne 中保存的特定文件夹中创建一个 google 文档。

我用谷歌搜索和搜索,我找到的最接近答案的是这里的代码:

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

但问题是它创建了一个文件,而不是谷歌文档。我正在寻找的东西会以某种方式调用 DocumentApp...

任何帮助将不胜感激!

~诺艾尔

【问题讨论】:

    标签: javascript google-apps-script google-docs google-drive-api


    【解决方案1】:

    DocsList 现已贬值,因此对于可能遇到此线程的任何人,这里是一个更新的答案:

    function createDoc() {
      // Creates doc in root directory
      var doc = DocumentApp.create('MyDocument');
      var docFile = DriveApp.getFileById(doc.getId());
    
      // Copy doc to the directory we want it to be in. Delete it from root.
      DriveApp.getFoldersByName('Subfolder1').next().addFile(docFile);
      DriveApp.getRootFolder().removeFile(docFile);
    
      // Returns the ID of the newly created Doc
      return DriveApp.getFilesByName('MyDocument').next().getId();
    }
    

    这是我找到此答案的帖子的链接:Create a Google Doc file directly in a Google Drive folder

    【讨论】:

      【解决方案2】:

      您可能希望使用 DocumentApp 创建文档,然后将其移至其他文件夹。

      var docName = "YOUR_DOC_NAME";
      var doc = DocumentApp.create(docName); //this creates the doc in the root folder
      var file = DocsList.getFileById(doc.getId());
      file.removeFromFolder(DocsList.getRootFolder());
      file.addToFolder(DocsList.getFolder("path/to/folder"));
      

      【讨论】:

        【解决方案3】:

        我无需在您的 Google Drive 根目录中创建 Google Doc,然后移至特定文件夹,而是能够直接在特定 Drive 文件夹中创建文件。 但为此,我在其他地方维护了一个空的 Google 文档。

        function createNewDocInDriveFolder(){
            var emptyDocId = "XXX-XXXXXvqadhYQiYoAno8aam2HJhRNxgO84qF9VXXXX";
            var destFolderId = "XXXXPsu8uKcmkhJ1-oiSRNzE1-XXXX_XX"; 
            var destFolder = DriveApp.getFolderById(destFolderId);
            var newDocFile = DriveApp.getFileById(emptyDocId).makeCopy("New Doc", destFolder);     
            return DocumentApp.openById(newDocFile.getId());
        }
        

        【讨论】:

          猜你喜欢
          • 2015-10-22
          • 1970-01-01
          • 2021-07-06
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-12-05
          相关资源
          最近更新 更多