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

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

提交回复
热议问题