FirestoreGoogleAppsScript
A Google Apps Script library for accessing Google Cloud Firestore.
AI Summary
Firestore for Google Apps Scripts
Library to access Firestore from Google Apps Script.
Target Users
Beginner to intermediate GAS developers wanting to integrate and operate Firestore.
Problems Solved
GAS lacks native Firestore support requiring service account auth and REST API.
Script ID
- In GAS Editor: Click "Libraries +" → Paste into "Script ID" field → Click "Look up"
- "FirestoreGoogleAppsScript" will appear in the search results
- Select the latest version (highest number) from "Version" dropdown
- Click "Add"
Tags
Main Features
1
Easy Auth
Get Firestore instance using service account credentials stored in PropertiesService.
2
CRUD Ops
CRUD documents with create/update/delete/get; partial updates via mask array.
3
Queries
Advanced queries with Where, OrderBy, Limit, Offset on collections.
Examples
Main Functions
| Function | Description |
|---|---|
| FirestoreApp.getFirestore | Get Firestore instance with creds |
| firestore.createDocument | Create a new document |
| firestore.query | Build and execute queries |
Examples
Create Firestore Instance
/**
* Firestoreインスタンス作成
* ・PropertiesServiceから認証情報取得: props.getProperty('client_email')
* ・FirestoreApp.getFirestore呼び出し: FirestoreApp.getFirestore(email, key, projectId)
* ・インスタンスをfirestore変数に代入: const firestore = ...
*/
function getFirestoreInstance() {
const props = PropertiesService.getUserProperties();
const [email, key, projectId] = [props.getProperty('client_email'), props.getProperty('private_key'), props.getProperty('project_id')];
const firestore = FirestoreApp.getFirestore(email, key, projectId);
return firestore;
}Create Document
/**
* 文書作成
* ・データオブジェクト定義: const data = { "name": "test!" }
* ・コレクション指定で作成: firestore.createDocument("FirstCollection", data)
* ・パス/ID指定で作成: firestore.createDocument("FirstCollection/FirstDocument", data)
*/
function createDocumentExample(firestore) {
const data = {
"name": "test!"
};
firestore.createDocument("FirstCollection", data);
firestore.createDocument("FirstCollection/FirstDocument", data);
}Execute Query
/**
* クエリ実行
* ・queryでコレクション指定: firestore.query("FirstCollection")
* ・Where条件追加: .Where("name", "==", "Test!")
* ・Executeで結果取得: .Execute()
*/
function queryExample(firestore) {
const allDocumentsWithTest = firestore.query("FirstCollection").Where("name", "==", "Test!").Execute();
return allDocumentsWithTest;
}