使用Google Golang Sheets API V4 / Drive API V3获取电子表格的ID?

huangapple go评论92阅读模式
英文:

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

问题

我正在使用Golang进行一个小项目,目前正在尝试根据文件系统路径(在Drive中)和电子表格/工作表名称获取电子表格ID。然而,在Golang的API库中查找时,我没有找到可以实现这一功能的函数。

我对这种编程还比较新手,所以如果这有一个简单的解决方案,我提前道歉。

谢谢!

英文:

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!

答案1

得分: 1

你可以在Google的Drive API中使用drive.files.listdrive.files.list可以从你的Google Drive中搜索带有文件夹信息的文件。

根据你的问题,我认为可以按照以下两个步骤进行操作:

  1. 使用drive.files.list搜索文件。可以同时检索到文件ID和父文件夹ID。在这种情况下,字段为id和parents。

  2. 使用drive.files.get根据文件夹ID检索文件夹名称。字段为name。

你可以使用从每个文件获取的文件夹信息来构建文件树。

关于示例脚本,它使用了Drive API的Go快速入门(https://developers.google.com/drive/v3/web/quickstart/go)。请将main()更改为以下脚本中的“Step 3: Set up the sample”的脚本。

脚本:

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)
	}
}

结果:

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

Google Drive上的文件可以有多个父文件夹。在此脚本中,假设每个文件只有一个父文件夹。如果你的文件有多个父文件夹,请从父数组中检索它们的文件夹。

参考资料:

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

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

Drive API的Go快速入门
https://developers.google.com/drive/v3/web/quickstart/go

英文:

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

huangapple
  • 本文由 发表于 2017年3月23日 05:56:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/42963406.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定