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

查看:74
本文介绍了如何从Google表格访问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.

当您按创建"时,您的浏览器将下载包含私钥(private_key),服务帐户电子邮件(client_email)和项目ID(project_id)的.json文件.将这些值复制到您的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-----\nPrivateKeyHere\n-----END PRIVATE KEY-----\n";
  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\n-----END PRIVATE KEY-----\n";
  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.

您只需从.json文件中复制-----BEGIN PRIVATE KEY----------END PRIVATE KEY-----\n之间的部分.

You have to copy only the part between -----BEGIN PRIVATE KEY----- and -----END PRIVATE KEY-----\n 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";

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

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

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

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