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

后端 未结 6 1688
天命终不由人
天命终不由人 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条回答
  •  隐瞒了意图╮
    2021-02-04 07:10

    The documentation for INSERT for the Drive API is found at this link:

    Drive API for INSERT

    There is a section for Request body. One of the Optional Properties for Insert is parents[]. The brackets [] indicate that a list of parents can be designated. The documentation for parents[] states this:

    Collection of parent folders which contain this file. Setting this field will put the file in all of the provided folders. On insert, if no folders are provided, the file will be placed in the default root folder.

    So, . . . using Insert in Drive API, . . . . CAN write a new file directly to a subfolder. It's possible.

    Now, the nomenclature and syntax for the Google Drive SDK, HTTP request is different than what is inside of Apps Script.

    The syntax for invoking the Drive API HTTP Request inside of a .gs file is one of the following three:

    • Drive.Files.insert(FILE resource)
    • Drive.Files.insert(FILE resource, BLOB mediaData)
    • Drive.Files.insert(FILE resource, BLOB mediaData, OBJECT optionalArgs)

    The syntax shown in the list above is from the auto-complete drop down list inside the Apps Script code editor. If you type Drive.Files. a list of possible methods will appear. I can't find information about the syntax anywhere in the online documentation.

    So, where does the parents[] optional property go? Well, it's not a Blob, so we can rule that out. It's either FILE resource, or OBJECT optionalArgs. optionalArgs indicates that it's an object, but FILE resource is actually also an object.

    In the examples, the FILE resource is constructed as key:value pair object.

    Uploading Files - Advanced Drive Service - Google Documentation

提交回复
热议问题