メインコンテンツにスキップ

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

  1. In GAS Editor: Click "Libraries +" → Paste into "Script ID" field → Click "Look up"
  2. "FirestoreGoogleAppsScript" will appear in the search results
  3. Select the latest version (highest number) from "Version" dropdown
  4. 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

FunctionDescription
FirestoreApp.getFirestoreGet Firestore instance with creds
firestore.createDocumentCreate a new document
firestore.queryBuild 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;
}

Sample Code

Post a Sample

No sample codes for this library yet

Be the first to post a sample!

Post a Sample