king_geedorah
king_geedorah

Reputation: 91

Fetch Spreadsheet ID using Google Golang Sheets API V4 / Drive API V3?

I'm using Golang for a small project of mine and am currently trying to pull a spreadsheet ID given the exactly filesystem path (in Drive) and spreadsheet/worksheet name. However, looking through the API library in Golang, I don't see a function that allows me to do this.

I'm pretty new to this kind of programming in general so sorry in advance if this has a trivial solution.

Thanks!

Upvotes: 0

Views: 1455

Answers (1)

Tanaike
Tanaike

Reputation: 201713

You can use drive.files.list in Drive API at Google. drive.files.list can search files with folder information from your Google Drive.

From your question, I thought following 2 steps.

  1. Search file using drive.files.list. File ID and parent folder id can be retrieved, simultaneously. In this case, the fields are id and parents.

  2. Retrieve folder name from folder id using drive.files.get. The field is name.

You can make file tree using folder information got from each file.

About sample script, it used Go Quickstart for Drive API (https://developers.google.com/drive/v3/web/quickstart/go) Please change main() for a script of "Step 3: Set up the sample" to following script.

Script :

func main() {
    ctx := context.Background()

    b, err := ioutil.ReadFile("client_secret.json")
    if err != nil {
        log.Fatalf("Unable to read client secret file: %v", err)
    }

    // If modifying these scopes, delete your previously saved credentials
    // at ~/.credentials/drive-go-quickstart.json
    config, err := google.ConfigFromJSON(b, drive.DriveMetadataReadonlyScope)
    if err != nil {
        log.Fatalf("Unable to parse client secret file to config: %v", err)
    }
    client := getClient(ctx, config)

    srv, err := drive.New(client)
    if err != nil {
        log.Fatalf("Unable to retrieve drive Client %v", err)
    }

    r, err := srv.Files.List().PageSize(10).
        Fields("nextPageToken, files(id, name)").Do()
    if err != nil {
        log.Fatalf("Unable to retrieve files: %v", err)
    }

    // From here, it's sample script.
    searchfile := "filename"
    r, err := srv.Files.List().
        Q("name=\"" + searchfile + "\" and trashed=false").Fields("files(id,parents)").Do() // "trashed=false" doesn't search in the trash box.
    if err != nil {
        log.Fatalf("Error: %v", err)
    }
    for _, i := range r.Files {
        r, err := srv.Files.Get(i.Parents[0]).Fields("name").Do()
        if err != nil {
            log.Fatalf("Error: %v", err)
        }
        fmt.Printf("FileID=%s, FolderID=%s, FolderName=%s\n", i.Id, i.Parents[0], r.Name)
    }
}

Result :

FileID=#####, FolderID=#####, FolderName=#####

Files on Google Drive can have several parent folders. At this script, it assumes that each file has one parent folder. If your files have several parent folders, please retrieve their folders from parent array.

References :

drive.files.list https://developers.google.com/drive/v3/reference/files/list

drive.files.get https://developers.google.com/drive/v3/reference/files/get

Go Quickstart for Drive API https://developers.google.com/drive/v3/web/quickstart/go

Upvotes: 1

Related Questions