Create new file in a folder with Apps Script using Google Advanced Drive service

后端 未结 6 1763
天命终不由人
天命终不由人 2021-02-04 06:23

There are four ways to create a new file:

  • DocsList - Shown as DocsList in the Main List. Built in to Apps Script.
  • DriveApp - Shown as
6条回答
  •  萌比男神i
    2021-02-04 07:13

    Maybe this is a bit late, but by looking at the REST API docs, it shows that you can use Drive.Files.insert to insert into any folder. You simply have to add the folder's ID in the properties of the file you are inserting as such:

    var file = {
       title: 'myFile',
       "parents": [{'id':folder.getId()}],  //<--By setting this parent ID to the folder's ID, it creates this file in the correct folder.
       mimeType: 'image/png'
     };
    

    Folder ID can be obtained from the shareable link using the Google Drive GUI or as shown here. (e.g. Use the Execute function on the right.)

    Alternatively, you can access the folder by name by replacing the folder.getID() with Drive.getFoldersByName('name of folder').

    This is helpful because Drive.Files.insert() accepts arguments while Drive.createFile() and Drive.createFolder() do not.

提交回复
热议问题