How to Create a Spreadsheet in a particular folder via App Script

前端 未结 9 1122
说谎
说谎 2020-12-09 10:00

Can anybody help me out,

I want to create a Spreadsheet through App Script in a particular folder. How to do that.

Presently I am doing as follow:

         


        
9条回答
  •  自闭症患者
    2020-12-09 10:37

    The other answer is a bit short (and not very explicit). While your approach is logic and should work if you replace

    file.removeFromFolder(file.getParents()[0]); 
    

    with

    file.removeFromFolder(DocsList.getRootFolder());

    there is a better way to do the same job using the new Drive app and the Folder Class, Folder has a method to create a file and you can specify the file type using the mimeType enum.

    Code goes like this :

    function myFunction() {
      var folders = DriveApp.getFoldersByName('YOUR FOLDER NAME'); // replace by the right folder name, assuming there is only one folder with this name
      while (folders.hasNext()) {
       var folder = folders.next();
      }
      folder.createFile('new Spreadsheet', '', MimeType.GOOGLE_SHEETS); // this creates the spreadsheet directly in the chosen folder
    }
    

提交回复
热议问题