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

前端 未结 9 1045
说谎
说谎 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:26

    Creating a new spreadsheet in a file can be done using this link as a reference.

    createFile(name, content, mimeType)

    Therefore using the enum MimeType we can do:

    var folder = DriveApp.getFolderById("your-folder-id");
    folder.createFile("My File Name","",MimeType.GOOGLE_SHEETS)
    
    0 讨论(0)
  • 2020-12-09 10:28

    Since you can no longer create Google Docs (Docs or SpreadSheets) using DriveApp, nor use addToFolder because DocList is deprecated. There is only one way to create or "move" Google Docs or Google SpreadSheets..

      //"Move" file to folder-------------------------------//
      var fileID = '12123123213321'
      var folderID = '21321312312'
      var file = DriveApp.getFileById(fileID).getName()
      var folder = DriveApp.getFolderById(folderID)
      var newFile = file.makeCopy(file, folder)
    
      //Remove file from root folder--------------------------------//
      DriveApp.getFileById(fileID).setTrashed(true)
    

    As you can see this DOES NOT move the file, it makes a copy with the same name in the folder you want and then moves the original file to the trash. I'm pretty sure there is no other way to do this.

    0 讨论(0)
  • 2020-12-09 10:28

    In July 27, 2020 there have been these updates:

    The File class now has the following methods:

    • file.getTargetId(): Gets a shortcut's file ID.
    • file.getTargetMimeType(): Returns the mime type of the item a shortcut points to.
    • file.moveTo(destination): Moves a file to a specified destination folder.

    The Folder class now has the following methods:

    • folder.createShortcut(targetId): Creates a shortcut to the provided Drive item ID, and returns it.
    • folder.moveTo(destination): Moves an item to the provided destination folder.

    The following Folder class methods have been deprecated:

    • addFile(File)
    • addFolder(Folder)
    • removeFile(File)
    • removeFolder(Folder)

    https://developers.google.com/apps-script/releases/#july_27_2020

    So you can create a Spreadsheet file in a folder using file.moveTo(destination) method:

    function createSpreadSheetInFolder(ss_new_name, folder_dest_id) {
        var ss_new = SpreadsheetApp.create(ss_new_name);
        var ss_new_id = ss_new.getId();
        var newfile = DriveApp.getFileById(ss_new_id);
        newfile.moveTo(DriveApp.getFolderById(folder_dest_id))
        return ss_new_id;
    }
    
    var file_name = 'SPREADSHEET NAME';
    var folder_id = 'DESTINATION FOLDER ID';
    var new_ssId = createSpreadSheetInFolder(file_name, folder_id)
    
    0 讨论(0)
  • 2020-12-09 10:31

    What is not working? Use getRootFolder in the last line.

    0 讨论(0)
  • 2020-12-09 10:35

    I finally got the answer to my question. The following works

    var file = DocsList.getFileById(sheet.getId());
    var folder = DocsList.getFolder("MyFolder");
    file.addToFolder(folder);
    
    // remove document from the root folder
    folder = DocsList.getRootFolder();
    file.removeFromFolder(folder);
    
    0 讨论(0)
  • 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
    }
    
    0 讨论(0)
提交回复
热议问题