如何从 Google Sheets 访问 Cloud Firestore? [英] How to access Cloud Firestore from Google Sheets?

查看:22
本文介绍了如何从 Google Sheets 访问 Cloud Firestore?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试借助 FirestoreGoogleAppsScript 示例从 Google 工作表访问 Cloud Firestore 数据库,但是如第 5 点所述,我已经下载了文件,但没有找到更新项目中 client_emailprivate_keyproject_id 的位置.所以请指导我继续前进.

I am trying to access Cloud Firestore database from Google sheet with the help of FirestoreGoogleAppsScript example, but as explained in the 5th point I have downloaded the file but not getting where to update the client_email, private_key and project_id in the project. So kindly guide me to move forward.

当您按下创建"时,您的浏览器将下载一个 .json 文件,其中包含您的私钥 (private_key)、服务帐户电子邮件 (client_email) 和项目 ID (private_key)>project_id).将这些值复制到您的 Google Apps 脚本中 - 您需要它们来通过 Firestore 进行身份验证.

When you press "Create," your browser will download a .json file with your private key (private_key), service account email (client_email), and project ID (project_id). Copy these values into your Google Apps Script — you'll need them to authenticate with Firestore.

带有演示代码.gs

function myFunction() {
  projectid: "xxx";
  key: "-----BEGIN PRIVATE KEY-----
PrivateKeyHere
-----END PRIVATE KEY-----
";
  email: "xxx@xxx.iam.gserviceaccount.com";
  
  var firestore = FirestoreApp.getFirestore(email, key, projectId);
}

错误

ReferenceError: "email" is not defined. (line 7, file "Code")

推荐答案

你必须做如下,例如在一个简单的函数中获取一个集合:

You have to do as follows, for example in a simple function that fetches a collection:

function fecthFirstCollectionDocs() {

  var email = "xxxxxx@yyyyyyy.iam.gserviceaccount.com";
  var key = "-----BEGIN PRIVATE KEY-----your key here
-----END PRIVATE KEY-----
";
  var projectId = "zzzzzzzzz";

  var firestore = FirestoreApp.getFirestore(email, key, projectId);

  const allDocuments = firestore.query("FirstCollection").execute();
  Logger.log(JSON.parse(allDocuments));

}

key 的值是通过创建服务帐户获得的,如库文档中所述:https://github.com/grahamearley/FirestoreGoogleAppsScript#creating-a-service-account.很简单,按照说明操作即可.

The value of key is obtained by creating a service account, as explained in the Library documentation: https://github.com/grahamearley/FirestoreGoogleAppsScript#creating-a-service-account. It's quite easy, just follow the instructions.

你只需要复制-----BEGIN PRIVATE KEY----------END PRIVATE KEY-----之间的部分----- 来自 .json 文件.

You have to copy only the part between -----BEGIN PRIVATE KEY----- and -----END PRIVATE KEY----- from the .json file.

根据您 4 月 9 日的评论进行

Edited following you comment of 9 April:

解决

ReferenceError: "email" is not defined. (line 7, file "Code") 

错误,您应该正确声明传递给 getFirestore() 方法的变量,如我回答的代码所示,如​​下所示:

error you should correctly declare the variables you pass to the getFirestore() method, as shown in the code of my answer, and as follows:

而不是做

  projectid: "fir-79c39";

你应该这样做

  var projectid = "fir-79c39";

您声明一个名为 projectid 的变量,您在 getFirestore() 方法中使用该变量.keyemail 相同.

You declare a variable named projectid that you use in the getFirestore() method. The same for key and email.

这篇关于如何从 Google Sheets 访问 Cloud Firestore?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆