今回はGAS(Google Apps Script)に関して書いていきます。
GASでのNotionデータベースCRUD操作(登録/参照/更新/削除)をそれぞれ実装して記事にしたので、それらをまとめてみようと思います。
それぞれの記事をリンクしつつ、この記事でもプログラムを載せて登録→参照→更新→削除の流れを書いてみようと思います。
それでは本題へ。
前提情報
まずは前提情報を色々と載せておきます。
GASとは?
Google Apps Script(GAS)は、ひとことで言えばGoogleが提供する各種サービスの自動化/連携を行うためのローコード開発(※1)ツールです。GASを使うと、Gmailやカレンダー、Googleスプレッドシート、Googleドライブなど、Googleが提供する様々なサービス上で処理を自動化したり、複数のサービスを連携させたりできます。
※1:ローコード開発:可能な限りソースコードを書かずにアプリケーションを開発する手法出典:【入門】GASとは?できること&活用法を解説(サンプルコード付き) - マーケティングオートメーションツール SATORI
詳しくは以下のサイトを見て頂ければと。
satori.marketingwww.cocoe.co.jp
Notionとは?
Notionとはアメリカ・サンフランシスコを拠点とするスタートアップ企業Notion Labsが開発した多機能ドキュメントツールです。プロジェクト管理やスケジュール管理、社内wikiなど目的に応じて使い分けられるさまざまなドキュメントを集約できるため「オール・イン・ワンツール」と呼ばれています。
出典:【万能アプリ】Notionとは?何がスゴイ?使い方や活用事例、料金などについてわかりやすく解説 - 2023/12/08 [Schoo]
詳しくは以下のサイトを見て頂ければと。
schoo.jpwww.sbbit.jp
CRUDとは?
CRUDとは、データベース管理システム(DBRS)に必要とされる4つの主要な機能、「作成(Create)」「読み出し(Read)」「更新(Update)」「削除(Delete)」をそれぞれ頭文字で表したもののことである。
例えばSQLにおいては、「作成」に「INSERT」のコマンドが、「読み出し」に「SELECT」、「更新」に「UPDATE」、「削除」には「DELETE」のコマンドがそれぞれ対応している。CRUDのそれぞれの機能を網羅していることは、データベースシステムの完全性を備えるために必須の要素であるとされている。
詳しくは以下のサイトを見て頂ければと。
www.weblio.jpe-words.jp
プログラム事前準備
対象のNotionデータベース
これは準備というか前提ですが、今回の記事では日記Notionのデータを操作します。
詳細は以下記事にて。
miya-moto-memo.hatenablog.com
GASとNotionの連携
GASでNotionのデータを操作するためには、事前準備が必要です。
事前準備の手順は別記事にて詳しく書いています。
以下あたりの記事を見てもらえればと思います。
miya-moto-memo.hatenablog.commiya-moto-memo.hatenablog.com
Create(作成)
まずはCreate(作成)です。
SQLでいうところの「INSERT」です。
プログラム
// Notionインテグレーションキー const NOTION_API_KEY = PropertiesService.getScriptProperties().getProperty('NOTION_API_KEY'); // 日記NotionのデータベースID const DATABASE_ID = PropertiesService.getScriptProperties().getProperty('DATABASE_ID'); /** * メイン処理 */ function main() { // Notionページ登録URL const url = 'https://api.notion.com/v1/pages'; // Notionページ登録実行 UrlFetchApp.fetch(url, composeFetchParam()); } /** * データ登録に必要なパラメータを組み立てる */ function composeFetchParam() { const payload = { 'parent': {'database_id': DATABASE_ID}, 'properties': { 'タイトル': { 'title': [{ 'text': { 'content': 'サンプルタイトル' } }] }, '日付': { 'date': { 'start': '2024-06-12' } }, 'ステータス': { 'status': { 'name': '未着手' } }, } }; const params = { 'method': 'POST', 'headers': { 'Notion-Version': '2022-06-28', 'Authorization': 'Bearer ' + NOTION_API_KEY, 'Content-Type': 'application/json' }, 'payload': JSON.stringify(payload), }; return params; }
プログラム実行
上記のプログラムを実行すると、Notionのデータが登録されます。
記事リンク
Notionデータベースにページを作成する「Create a page」に関して詳しく書いている記事は以下です。
こちらも良ければぜひ。
Read(参照)
次はRead(参照)です。
SQLでいうところの「SELECT」です。
プログラム
// Notionインテグレーションキー const NOTION_API_KEY = PropertiesService.getScriptProperties().getProperty('NOTION_API_KEY'); // 日記NotionのデータベースID const DATABASE_ID = PropertiesService.getScriptProperties().getProperty('DATABASE_ID'); /** * メイン処理 */ function main() { // Notion検索URL const url = 'https://api.notion.com/v1/databases/' + DATABASE_ID + '/query'; // Notion検索実行 const result = JSON.parse(UrlFetchApp.fetch(url, composeFetchParam())); for (const data of result.results) { // 各プロパティ取得 const title = data.properties['タイトル'].title[0].plain_text; const date = data.properties['日付'].date.start; const status = data.properties['ステータス'].status.name; // ログ出力 console.log(title); console.log(date); console.log(status); } } /** * データ検索に必要なパラメータを組み立てる */ function composeFetchParam() { const payload = { 'filter': { 'property': 'タイトル', 'title': { 'contains': 'サンプル' } } }; const params = { 'method': 'POST', 'headers': { 'Notion-Version': '2022-06-28', 'Authorization': 'Bearer ' + NOTION_API_KEY, 'Content-Type': 'application/json' }, 'payload': JSON.stringify(payload), }; return params; }
プログラム実行
上記のプログラムを実行すると、Notionのデータを取得します。
取得データをログ出力しています。
Update(更新)
次はUpdate(更新)です。
SQLでいうところの「UPDATE」です。
プログラム
// Notionインテグレーションキー const NOTION_API_KEY = PropertiesService.getScriptProperties().getProperty('NOTION_API_KEY'); // 日記NotionのデータベースID const DATABASE_ID = PropertiesService.getScriptProperties().getProperty('DATABASE_ID'); /** * メイン処理 */ function main() { // Notion検索 const result = queryNotion(); for (const data of result.results) { // Notion更新 updateNotion(data.id); } } /** * Notion検索 */ function queryNotion() { // URL const url = 'https://api.notion.com/v1/databases/' + DATABASE_ID + '/query'; // パラメータ const params = { 'method': 'POST', 'headers': headers, 'payload': JSON.stringify(payloadForQuery), }; // 実行 return JSON.parse(UrlFetchApp.fetch(url, params)); } /** * Notion更新 */ function updateNotion(pageId) { // URL const url = 'https://api.notion.com/v1/pages/' + pageId; // パラメータ const params = { 'method': 'PATCH', 'headers': headers, 'payload': JSON.stringify(payloadForUpdate), }; // 実行 UrlFetchApp.fetch(url, params); } /** * headers */ const headers = { 'Notion-Version': '2022-06-28', 'Authorization': 'Bearer ' + NOTION_API_KEY, 'Content-Type': 'application/json' }; /** * payload(検索用) */ const payloadForQuery = { 'filter': { 'property': 'タイトル', 'title': { 'contains': 'サンプル' } } }; /** * payload(更新用) */ const payloadForUpdate = { 'properties': { 'ステータス': { 'status': { 'name': '進行中' } } } };
プログラム実行
上記のプログラムを実行すると、Notionのデータが更新されます。
記事リンク
Notionデータベースのページプロパティを更新する「Update page properties」に関して詳しく書いている記事は以下です。
こちらも良ければぜひ。
「データ検索」→「データ更新」の流れは以下記事で詳しく書いています。
こちらも良ければぜひ。
Delete(削除)
最後はDelete(削除)です。
SQLでいうところの「DELETE」です。
プログラム
// Notionインテグレーションキー const NOTION_API_KEY = PropertiesService.getScriptProperties().getProperty('NOTION_API_KEY'); // 日記NotionのデータベースID const DATABASE_ID = PropertiesService.getScriptProperties().getProperty('DATABASE_ID'); /** * メイン処理 */ function main() { // Notion検索 const result = queryNotion(); for (const data of result.results) { // Notion削除 deleteNotion(data.id); } } /** * Notion検索 */ function queryNotion() { // URL const url = 'https://api.notion.com/v1/databases/' + DATABASE_ID + '/query'; // パラメータ const params = { 'method': 'POST', 'headers': headers, 'payload': JSON.stringify(payloadForQuery), }; // 実行 return JSON.parse(UrlFetchApp.fetch(url, params)); } /** * Notion削除 */ function deleteNotion(pageId) { // URL const url = 'https://api.notion.com/v1/pages/' + pageId; // パラメータ const params = { 'method': 'PATCH', 'headers': headers, 'payload': JSON.stringify(payloadForDelete), }; // 実行 UrlFetchApp.fetch(url, params); } /** * headers */ const headers = { 'Notion-Version': '2022-06-28', 'Authorization': 'Bearer ' + NOTION_API_KEY, 'Content-Type': 'application/json' }; /** * payload(検索用) */ const payloadForQuery = { 'filter': { 'property': 'タイトル', 'title': { 'contains': 'サンプル' } } }; /** * payload(削除用) */ const payloadForDelete = { 'archived': true };
プログラム実行
上記のプログラムを実行すると、Notionのデータが削除されます。
記事リンク
Notionデータベースのページを削除する「Trash a page」に関して詳しく書いている記事は以下です。
こちらも良ければぜひ。
miya-moto-memo.hatenablog.com
おわりに
ということで、「GASでのNotionデータベースCRUD操作(登録/参照/更新/削除)をまとめてみる」に関してアレコレ書いてみました。
CRUD操作(登録/参照/更新/削除)それぞれの記事をリンクしつつ、プログラムを載せて登録→参照→更新→削除の流れを書いてみました。
この記事が参考になれば幸いです。
関連記事
GAS(Google Apps Script)に関してはいくつか記事にしています。
気になる記事があればぜひ。
GASの活用事例
GASでのNotionデータベースCRUD操作(登録/参照/更新/削除)