【问题标题】:Create a Google Doc file directly in a Google Drive folder直接在 Google Drive 文件夹中创建 Google Doc 文件
【发布时间】:2015-10-22 17:57:18
【问题描述】:

如何在 Google Drive 文件夹中创建 Google 文档?

我知道我可以在这样的文件夹中创建一个文件。

var folder = DriveApp.createFolder("A Folder")
folder.createFile("filename.file", "Some text")

但是如何使用 Google Apps 脚本在文件夹中创建 文档

【问题讨论】:

  • 对于因搜索结果而登陆这里但只想学习如何为此目的手动构建 URL 的任何人,https://docs.google.com/document/create?folder=1d4AX96kMR34J0iNGABrrNg8 适合我:webapps.stackexchange.com/questions/6437/…

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


【解决方案1】:

文件有点不同,你先创建它然后移动到文件夹:

var doc = DocumentApp.create('Document Name'),
      docFile = DriveApp.getFileById( doc.getId() );

DriveApp.getFolderById(foldId).addFile( docFile );

【讨论】:

    【解决方案2】:

    另一个答案是正确的,但该文件将存在于您的文件夹中并且在您的“驱动器”文件夹(根文件夹)中。

    为避免这种情况,只需将其从那里删除!

    代码:

      var doc = DocumentApp.create('Document Name'),
          docFile = DriveApp.getFileById( doc.getId() );
      DriveApp.getFolderById('0B3°°°°°°°1lXWkk').addFile( docFile );
      DriveApp.getRootFolder().removeFile(docFile);
    

    【讨论】:

      【解决方案3】:

      添加到@Kriggs 答案。

      从根文件夹中删除:

      DriveApp.getRootFolder().removeFile(file)

      否则,它将位于根级别新位置。

      【讨论】:

        【解决方案4】:

        这是在特定文件夹中创建新 Google 文档的方法。创建Doc时,必须设置mime类型。

        请注意,此代码不是首先创建一个 Doc 文件,移动它,然后删除根驱动器中的文件。此代码直接在文件夹中创建一个新的 Google Doc 类型文件。

        您需要使用 Advanced Drive API。它必须在两个地方启用。

        • 从代码编辑器中选择“资源”,然后选择“高级 Google 服务”
        • 点击“Drive API”按钮,使其处于开启状态。
        • 点击链接:Google Cloud Platform API Dashboard。
        • 搜索驱动器 API
        • 从搜索列表中选择 Google Drive API
        • 启用 Drive API

        代码:

        function myFunction(e) {
          var doc,fileResource,folder,ID_of_newFile,multipleFolders,newFile,
              newDocName,rng,sh,ss,ssFile,ssID;
        
          var name = e.values[1];
          var kantoor = e.values[2];
        
          newDocName = 'Sleuteloverdracht ' + name;//Set the name of the new file
        
          rng = e.range;//Get range in the spreadsheet that received the data
          sh = rng.getSheet();//Get sheet tab
          ss = sh.getParent();//Get spreadsheet
          ssID = ss.getSheetId();//Get file ID of spreadsheet - Integer
        
          ssFile = DriveApp.getFileById(ssID);//Get the spreadsheet file in order to get the
            //folder to put the new file in
          multipleFolders = ssFile.getParents();//Get all parent folders of the spreadsheet file
        
          folder = multipleFolders.next();//Get the folder of the spreadsheet
        
          fileResource = {
            'title': newDocName,
            "parents": [{'id':folder.getId()}],  //<--By setting this parent ID to the 
            //folder's ID, it creates this file in the correct folder
            'mimeType': 'application/vnd.google-apps.document'
          };
        
          newFile = Drive.Files.insert(fileResource);//Create the new document file
           // directly into the same folder as the spreadsheet
          ID_of_newFile = newFile.getId();
        
          //Logger.log(ID_of_newFile)
        
          doc = DocumentApp.openById(newFile.getId());//Open the new file as a Google document
        
          var body = doc.getBody();
          body.appendParagraph('Sleuteloverdracht ' + name);
          body.appendParagraph('Uw naam is '+ name + '\n' + 
            'U heeft de sleutel van kantoor '+ kantoor);
          doc.saveAndClose();
        
        }
        

        【讨论】:

          【解决方案5】:
          var folder = DriveApp.createFolder ("A Folder");
          var file = DriveApp.createFile ("filename.file", "Some text");
          file.moveTo (folder);
          

          【讨论】:

            【解决方案6】:

            Serge insas 的回答帮助了我,但这些方法现在已在 2021 年弃用。这对我有用。

            var folder = 'Your Folder Id'
            var title = 'Your File Name'    
            var doc = DocumentApp.create(title);
            docFile = DriveApp.getFileById(doc.getId());
            docFile.moveTo(folder);
            

            【讨论】:

            • 当您使用 '.moveTo' 而不是 '.makeCopy' 时,您可以省去删除根文件夹中文件的麻烦。 '.moveTo' 实际上移动到文件到您的目标文件夹:var targetFolder = DriveApp.getFolderById(FOLDER_ID); var docFile = DriveApp.getFileById(doc.getId()); docFile.moveTo(targetFolder);
            • 更新了上面的内容以包含 Oliver 的评论。
            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多