问题描述
我正在尝试借助 FirestoreGoogleAppsScript 示例从 Google 工作表访问 Cloud Firestore 数据库,但是如第 5 点所述,我已经下载了文件,但没有找到更新项目中 client_email
、private_key
和 project_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: "[email protected]";
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 = "[email protected]";
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()
方法中使用该变量.key
和 email
相同.
You declare a variable named projectid
that you use in the getFirestore()
method. The same for key
and email
.
这篇关于如何从 Google Sheets 访问 Cloud Firestore?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!