Giter VIP home page Giter VIP logo

filesapp's Introduction

FilesApp

MIT License

Overview

FilesApp is a GAS library for retrieving file and folder list in Google Drive using Google Apps Script (GAS). Also this can create a tree from all files and folders in Google Drive.

Description

When I create some applications using Google Drive, there are often the case which is required to retrieve the file list and folder list. I had prepared the script each time for each case so far. But recently, I thought that if there is a library for retrieving the file and folder list (as a tree), it will be useful for me and other developers. So I created this. If this was useful for your situation, I'm glad.

Library's project key

1dBxqh6gpWqFv4h0M25ALp4bhcs6CoqhiIJ0fUeUjR57bAxIN2PqfkQns

How to install

In order to use this library, please install this as a library.

  1. Install FilesApp library.
    • Library's project key is 1dBxqh6gpWqFv4h0M25ALp4bhcs6CoqhiIJ0fUeUjR57bAxIN2PqfkQns.
  2. FilesApp uses Drive API. Please enable Drive API at API console.
    • On the Script Editor
      • Resources -> Cloud Platform Project
      • Click the lower part of "This script is currently associated with project:"
    • On "API APIs&services"
      • In "Getting Started", Click "Enable APIs and get credentials like keys".
      • Click Library at left side.
      • At "Search APIs and services", Input "drive api", Click it.
      • Enable "Google Drive API"

Recently, when it enabled APIs, I had an experience that I was made to wait for several minutes for enabling APIs. So when you enabled APIs at API console, if the error related to APIs occurs, please run again after several minutes.

Usage

There are 4 methods in this library.

Method Description
createTree(folderId, mimeType, fields) Create a file and folder tree. Retrieve all folders of all level under folderId. All files and folders are included in an array with the level.
getAllInFolder(folderId, mimeType, fields) Retrieve all files and folders of all level under folderId. All files and folders are included in an array without the level.
getAllFoldersInFolder(folderId) Retrieve all folders of all level under folderId.
getFilesAndFoldersInFolder(folderId, mimeType, fields) Retrieve files and folders just under a folder with folderId.

Arguments

  • folderId : Folder ID of top folder you want to retrieve. Please use as a string.
    • If you want to retrieve the values under "root", please use "root" for this.
  • mimeType : mimeType of files you want to retrieve. Please use as an array like ["application/vnd.google-apps.script", "image/png"]
    • If you use null for this, all kinds of files are retrieved.
  • fields : fields of files you want to retrieve. Please use as a string. This value is the same to fields which is used at files.list of Drive API.
    • If you want to retrieve the filename and ID of files, you can use "files(id,name)"

Samples

In order to explain the following sample script, it supposes that there is this structure in Google Drive. In this case, it doesn't need that "sampleFolder1" is root.

1. createTree(folderId, mimeType, fields)

folderId is the ID of "sampleFolder1". In this sample, the filenames of files with all mimeTypes are retrieved.

When "root" is used for "folderId", all files of Google Drive can be retrieved.

var res = FilesApp.createTree(folderId, null, "files(name)");
Result

The index of the array of folderTreeById and folderTreeByName is corresponding to the level of folders.

{
  "topFolderId": ["### FolderId of sampleFolder1 ###"],
  "topFolderName": ["sampleFolder1"],
  "totalFilesAndFolders": 11,
  "totalFiles": 4,
  "totalFolders": 7,
  "files": [
    {
      "folderTreeById": ["### FolderId of sampleFolder1 ###"],
      "folderTreeByName": ["sampleFolder1"],
      "filesInFolder": [
        {
          "name": "Spreadsheet1",
          "parents": ["### FolderId of sampleFolder1 ###"]
        },
        {
          "name": "sampleFolder_2b",
          "parents": ["### FolderId of sampleFolder1 ###"]
        },
        {
          "name": "sampleFolder_2a",
          "parents": ["### FolderId of sampleFolder1 ###"]
        }
      ]
    },
    {
      "folderTreeById": ["### FolderId of sampleFolder1 ###", "### FolderId of sampleFolder_2b ###"],
      "folderTreeByName": ["sampleFolder1", "sampleFolder_2b"],
      "filesInFolder": [
        {
          "name": "Spreadsheet4",
          "parents": ["### FolderId of sampleFolder_2b ###"]
        },
        {
          "name": "sampleFolder_2b_3b",
          "parents": ["### FolderId of sampleFolder_2b ###"]
        },
        {
          "name": "sampleFolder_2b_3a",
          "parents": ["### FolderId of sampleFolder_2b ###"]
        }
      ]
    },
    {
      "folderTreeById": ["### FolderId of sampleFolder1 ###", "### FolderId of sampleFolder_2b ###", "### FolderId of sampleFolder_2b_3b ###"],
      "folderTreeByName": ["sampleFolder1", "sampleFolder_2b", "sampleFolder_2b_3b"],
      "filesInFolder": [
        {
          "name": "sampleFolder_2b_3b_4a",
          "parents": ["### FolderId of sampleFolder_2b_3b ###"]
        }
      ]
    },
    {
      "folderTreeById": ["### FolderId of sampleFolder1 ###", "### FolderId of sampleFolder_2b ###", "### FolderId of sampleFolder_2b_3b ###", "### FolderId of sampleFolder_2b_3a ###"],
      "folderTreeByName": ["sampleFolder1", "sampleFolder_2b", "sampleFolder_2b_3b", "sampleFolder_2b_3b_4a"],
      "filesInFolder": [
        {
          "name": "Spreadsheet5",
          "parents": ["### FolderId of sampleFolder_2b_3a ###"]
        }
      ]
    },
    {
      "folderTreeById": [ "### FolderId of sampleFolder1 ###", "### FolderId of sampleFolder_2b ###", "### FolderId of sampleFolder_2b_3a ###"],
      "folderTreeByName": ["sampleFolder1", "sampleFolder_2b", "sampleFolder_2b_3a"],
      "filesInFolder": [
        {
          "name": "Spreadsheet3",
          "parents": ["### FolderId of sampleFolder_2b_3a ###"]
        }
      ]
    },
    {
      "folderTreeById": ["### FolderId of sampleFolder1 ###", "### FolderId of sampleFolder_2a ###"],
      "folderTreeByName": ["sampleFolder1", "sampleFolder_2a"],
      "filesInFolder": [
        {
          "name": "Spreadsheet2",
          "parents": ["### FolderId of sampleFolder_2a ###"]
        },
        {
          "name": "sampleFolder_2a_3a",
          "parents": ["### FolderId of sampleFolder_2a ###"]
        }
      ]
    },
    {
      "folderTreeById": ["### FolderId of sampleFolder1 ###", "### FolderId of sampleFolder_2a ###", "### FolderId of sampleFolder_2a_3a ###"],
      "folderTreeByName": ["sampleFolder1", "sampleFolder_2a", "sampleFolder_2a_3a"],
      "filesInFolder": []
    }
  ]
}

2. getAllInFolder(folderId, mimeType, fields)

var res = FilesApp.getAllInFolder(folderId, null, "files(name)");
Result

All files and folders in a folder you set are retrieved. But it is not as a tree. The speed is faster than createTree().

[
    {"name":"Spreadsheet5"},
    {"name":"Spreadsheet4"},
    {"name":"Spreadsheet3"},
    {"name":"Spreadsheet2"},
    {"name":"Spreadsheet1"},
    {"name":"sampleFolder_2b_3b_4a"},
    {"name":"sampleFolder_2a_3a"},
    {"name":"sampleFolder_2b"},
    {"name":"sampleFolder_2a"},
    {"name":"sampleFolder_2b_3b"},
    {"name":"sampleFolder_2b_3a"}
]

3. getAllFoldersInFolder(folderId)

var res = FilesApp.getAllFoldersInFolder(folderId);
Result

All folders in a folder you set are retrieved as a tree.

{
    "id":[
        ["### ID of sampleFolder1 ###"],
        ["### ID of sampleFolder1 ###","### ID of sampleFolder_2b ###"],
        ["### ID of sampleFolder1 ###","### ID of sampleFolder_2b ###","### ID of sampleFolder_2b_3b ###"],
        ["### ID of sampleFolder1 ###","### ID of sampleFolder_2b ###","### ID of sampleFolder_2b_3b ###","### ID of sampleFolder_2b_3b_4a ###"],
        ["### ID of sampleFolder1 ###","### ID of sampleFolder_2b ###","### ID of sampleFolder_2b_3a ###"],
        ["### ID of sampleFolder1 ###","### ID of sampleFolder_2a ###"],
        ["### ID of sampleFolder1 ###","### ID of sampleFolder_2a ###","### ID of sampleFolder_2a_3a ###"]
    ],
    "name":[
        ["sampleFolder1"],
        ["sampleFolder1","sampleFolder_2b"],
        ["sampleFolder1","sampleFolder_2b","sampleFolder_2b_3b"],
        ["sampleFolder1","sampleFolder_2b","sampleFolder_2b_3b","sampleFolder_2b_3b_4a"],
        ["sampleFolder1","sampleFolder_2b","sampleFolder_2b_3a"],
        ["sampleFolder1","sampleFolder_2a"],
        ["sampleFolder1","sampleFolder_2a","sampleFolder_2a_3a"]
    ]
}

4. getFilesAndFoldersInFolder(folderId, mimeType, fields)

var res = FilesApp.getFilesAndFoldersInFolder(folderId, null, "files(name)");
Result

All files and folders just under a folder you set are retrieved.

[
    {"name":"Spreadsheet1"},
    {"name":"sampleFolder_2b"},
    {"name":"sampleFolder_2a"}
]

Licence

MIT

Author

Tanaike

If you have any questions and commissions for me, feel free to tell me.

Update History

  • v1.0.0 (June 15, 2018)

    1. Initial release.
  • v1.0.1 (September 3, 2018)

    1. Removed a bug.
      • When there are files and folders without the parents, an error occurred. In this version, this issue was removed.

TOP

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.