在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):grahamearley/FirestoreGoogleAppsScript开源软件地址(OpenSource Url):https://github.com/grahamearley/FirestoreGoogleAppsScript开源编程语言(OpenSource Language):TypeScript 100.0%开源软件介绍(OpenSource Introduction):Firestore for Google Apps ScriptsA Google Apps Script library for accessing Google Cloud Firestore.This library allows a user (or service account) to authenticate with Firestore and edit their Firestore database within a Google Apps Script. Read how this project was started here. As of v27, this project has been updated to use the GAS V8 runtime with Typescript! This introduces a number of breaking changes. Scripts utilizing the old Rhino runtime must use v26. InstallationIn the Google online script editor, select the Quick startCreating a service accountThe easiest way to use this library is to create a Google Service Account for your application and give it read/write access to your datastore. Giving a service account access to your datastore is like giving access to a user's account, but this account is strictly used by your script, not by a person. If you don't already have a Firestore project you want to use, create one at the Firebase admin console. To make a service account,
Configurating Firestore instance from your scriptNow, with your service account client email address const firestore = FirestoreApp.getFirestore(email, key, projectId); Configuration TemplateHere's a quick template to get you started (by replacing const email = '[email protected]';
const key = '-----BEGIN PRIVATE KEY-----\nPrivateKeyLine1\nPrivateKeyLine2\nPrivateKeyLineN\n-----END PRIVATE KEY-----';
const projectId = 'projectname-12345'
const firestore = FirestoreApp.getFirestore(email, key, projectId); Alternatively, using Properties Service once data is already stored in the service with "client_email", "private_key", and "project_id" property names: const props = PropertiesService.getUserProperties(); // Or .getScriptProperties() if stored in Script Properties
const [email, key, projectId] = [props.getProperty('client_email'), props.getProperty('private_key'), props.getProperty('project_id')];
const firestore = FirestoreApp.getFirestore(email, key, projectId); Creating DocumentsUsing this Firestore instance, we will create a Firestore document with a field const data = {
"name": "test!"
} We can choose to create a document in collection called "FirstCollection" without a name (Firestore will generate one): firestore.createDocument("FirstCollection", data); Alternatively, we can create the document in the "FirstCollection" collection called "FirstDocument": firestore.createDocument("FirstCollection/FirstDocument", data); Updating DocumentsTo update (overwrite) the document at this location, we can use the firestore.updateDocument("FirstCollection/FirstDocument", data); To update only specific fields of a document at this location, we can set the firestore.updateDocument("FirstCollection/FirstDocument", data, true); Deleting DocumentsTo delete a document at this location, we can use the firestore.deleteDocument("FirstCollection/FirstDocument"); Note: This cannot handle deleting collections or subcollections, only individual documents. Getting DocumentsYou can retrieve documents by calling the const documentWithMetadata = firestore.getDocument("FirstCollection/FirstDocument"); You can also retrieve all documents within a collection by using the const allDocuments = firestore.getDocuments("FirstCollection"); You can also get specific documents by providing an array of document names const someDocuments = firestore.getDocuments("FirstCollection", ["Doc1", "Doc2", "Doc3"]); Getting Document PropertiesYou can access various properties of documents from Firestore: const doc = firestore.getDocument("My Collection/My Document");
const originalData = doc.obj // Original database object (your stored data)
const readTime = doc.read // Date Object of the Read time from database
const updateTime = doc.updated // Date Object of the Updated time from database
const createdTime = doc.created // Date Object of the Created time from database
const name = doc.name // Full document path (projects/projName/databases/(default)/documents/My Collection/My Document)
const path = doc.path // Local document path (My Collection/My Document) Getting Documents (Advanced method using Query)If more specific queries need to be performed, you can use the const allDocumentsWithTest = firestore.query("FirstCollection").Where("name", "==", "Test!").Execute(); The Queries looking for const allDocumentsNullNames = firestore.query("FirstCollection").Where("name", null).Execute(); Query results can be ordered: const allDocumentsNameAsc = firestore.query("FirstCollection").OrderBy("name").Execute();
const allDocumentsNameDesc = firestore.query("FirstCollection").OrderBy("name", "desc").Execute(); To limit, offset, or just select a range of results: const documents2_3_4_5 = firestore.query("FirstCollection").Limit(4).Offset(2).Execute();
const documents3_4_5_6 = firestore.query("FirstCollection").Range(3, 7).Execute(); See other library methods and details in the wiki. Frequently Asked Questions
Breaking Changes
ContributionsContributions are welcome — send a pull request! See here for more information on contributing. After cloning this repository, you can push it to your own private copy of this Google Apps Script project to test it yourself. See here for directions on using If you want to view the source code directly on Google Apps Script, where you can make a copy for yourself to edit, click here. |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论