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.
In the Google online script editor, select the Resources
menu item and choose Libraries...
. In the "Add a library" input box, enter MX2_NUfxVpaA1XPcZ_N-3wWb_Hp7BVbw3
and click "Add." Choose the most recent version number.
The 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.
Follow these instructions (just the "Creating a service account" section) to obtain a service account email address and private key. Ensure you have given the account full read/write access to the Datastore scope. You can do this in the "Create a service account" window by selecting "Datastore" in the "Role" dropdown and choosing "Cloud Datastore Owner."
After following these instructions, you'll have a JSON file with fields for private_key
and client_email
. Copy these into your Google Apps Script! You'll also need to get your project ID — you can find this in your Firebase project settings (under Project ID).
Now, with your service account client email address email
, private key key
, and project ID projectId
, we will authenticate with Firestore to get our Firestore
object. To do this, get the Firestore
object from the library:
var firestore = FirestoreApp.getFirestore(email, key, projectId);
Using this Firestore instance, we will create a Firestore document with a field name
with value test!
. Let's encode this as a JSON object:
const data = {
"name": "test!"
}
Now, we can create a document called FirstDocument
at a collection called FirstCollection
:
firestore.createDocumentWithId("FirstDocument", "FirstCollection", data)
To update the document at this location, we can use the updateDocument
function:
firestore.updateDocument("FirstCollection/FirstDocument", data)
Note: Although you can call updateDocument
without using createDocument
to create the document, any documents in your path will not be created and thus you can only access the document by using the path explicitly.
See other library methods in the wiki.
Contributions are welcome — send a pull request! This library is a work in progress. 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 clasp
to develop App Scripts locally.
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.