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

后端 未结 6 1698
天命终不由人
天命终不由人 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 06:55

    Direct Answer to Question

    This summary from https://developers.google.com/apps-script/advanced/drive sums things up pretty well:

    The advanced Drive service allows you to use the Google Drive web API in Apps Script. Much like Apps Script's built-in Drive service, this API allows scripts to create, find, and modify files and folders in Google Drive. In most cases, the built-in service is easier to use, but this advanced service provides a few extra features, including access to custom file properties as well as revisions for files and folders.

    Like all advanced services in Apps Script, the advanced Drive service uses the same objects, methods, and parameters as the public API.

    Essentially DriveApp is easier to use than Drive, but Drive gives you more functionality since it shares the same functionality of the public API. I was not able to see how to save a file to a Shared/Team drive using DriveApp, so I ended up using Drive. The pain came around lack of documentation for the Google Apps Script implementation of Drive.

    Explanation of My Solution and Code Sample:

    A specific implementation of saving a file to Google drive, but this will likely be useful for others. It took me a whole day to figure this out since the documentation and code examples for Google Apps scripts is severely lacking. My use case was for saving a JSON file to a shared Google Drive (Team Drive).

    There are three parameters that I did not have at first and my files were not uploading. I am not sure if all are necessary. One was the "kind": "drive#parentReference" part of the parents metadata. The next was "teamDriveId": teamDriveId which is also in the metadata. The last parameter was "supportsAllDrives": true which I passed in the optional parameter location of Drive.Files.insert().

    I found the API explorer on https://developers.google.com/drive/api/v2/reference/files/insert to be very useful in figuring out which parameters were needed and how they needed to be formatted. I basically edited values in the explorer till I got a network request that worked. I then pulled the parameters I used into my Google Apps script.

    /**
     *  Creates a JSON file in the designated Google Drive location
     *  @param {String} jsonString - A JS string from the result of a JSON.stringify(jsObject)
     *  @param {String} filename - The filename. Be sure to include the .json extension
     *  @param {String} folderId - The ID of the Google Drive folder where the file will be created
     *  @param {String} teamDriveId - The ID of the team drive
     *  @return {void}
     */
    function createJSONFileInDriveFolder(jsonString, filename, folderId, teamDriveId) {
      var metadata = {
        "title": filename,
        "mimeType": "application/json",
        "parents": [
          {
            "id": folderId,
            "kind": "drive#parentReference"
          }
        ],
        "teamDriveId": teamDriveId
      };
      var optionalParams = {
        "supportsAllDrives": true
      };
    
      try {
        var jsonBlob = Utilities.newBlob(jsonString, 'application/vnd.google-apps.script+json');
        Drive.Files.insert(metadata, jsonBlob, optionalParams);
      } catch (error) {
        Logger.log(error);
      }
    }
    

提交回复
热议问题