今回はGAS(Google Apps Script)に関して書いていきます。
以前、「GASでNotionにデータ登録するWebページを作ってみる」記事を投稿しました(以下リンク)。
上記の記事に載せたプログラムでは「doGet」「doPost」といったWeb周りの処理と、「create」「composeFetchParam」といったNotion周りの処理が1つのプログラムにまとまっていました。
そのプログラムが長いな…と感じたため、プログラムを分割してみます。
今回の記事では、そのあたりに関して書いていきます。
プログラム
まずは変更前後でどう変わったか、プログラムを載せます。
変更前
コード.gs
// Notionインテグレーションキー const NOTION_API_KEY = PropertiesService.getScriptProperties().getProperty('NOTION_API_KEY'); // 雑多メモNotionのデータベースID const DATABASE_ID = PropertiesService.getScriptProperties().getProperty('DATABASE_ID'); /** * GET処理 */ function doGet() { // 表示処理 return index(); } /** * POST処理 */ function doPost(e) { // 作成処理 create(e.parameter); // 完了メッセージ const msg = "登録が完了しました"; // 表示処理 return index(msg); } /** * インクルード処理 */ function include(filename) { return HtmlService.createHtmlOutputFromFile(filename).getContent(); } /** * 表示処理 */ function index(msg = null) { // HTML側に渡す変数をセット const template = HtmlService.createTemplateFromFile('index'); template.deployURL = ScriptApp.getService().getUrl(); template.msg = msg; // タイトルやmetaタグをセット const htmlOutput = template.evaluate(); htmlOutput.setTitle('雑多メモ'); htmlOutput.addMetaTag('viewport', 'width=device-width, initial-scale=1'); return htmlOutput; } /** * 作成処理 */ function create(param) { // Notionページ登録URL const url = 'https://api.notion.com/v1/pages'; // Notionページ登録実行 UrlFetchApp.fetch(url, composeFetchParam(param)); } /** * データ登録に必要なパラメータを組み立てる */ function composeFetchParam(param) { const payload = { 'parent': {'database_id': DATABASE_ID}, 'properties': composeProperties(param), 'children': composeBlocks(param) }; 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; } /** * データ登録時のプロパティ */ function composeProperties(param) { return { 'タイトル': { 'title': [{ 'text': { 'content': param.title } }] } }; } /** * データ登録時のブロック */ function composeBlocks(param) { return [{ "type": "paragraph", "paragraph": { "rich_text": [{ "type": "text", "text": { "content": param.memo } }] } }] }
変更後
main.gs
/** * GET処理 */ function doGet() { // 表示処理 return index(); } /** * POST処理 */ function doPost(e) { // 作成処理 create(e.parameter); // 完了メッセージ const msg = "登録が完了しました"; // 表示処理 return index(msg); } /** * 表示処理 */ function index(msg = null) { // HTML側に渡す変数をセット const template = HtmlService.createTemplateFromFile('index'); template.deployURL = ScriptApp.getService().getUrl(); template.msg = msg; // タイトルやmetaタグをセット const htmlOutput = template.evaluate(); htmlOutput.setTitle('雑多メモ'); htmlOutput.addMetaTag('viewport', 'width=device-width, initial-scale=1'); return htmlOutput; } /** * インクルード処理 */ function include(filename) { return HtmlService.createHtmlOutputFromFile(filename).getContent(); }
notion.gs
// Notionインテグレーションキー const NOTION_API_KEY = PropertiesService.getScriptProperties().getProperty('NOTION_API_KEY'); // 雑多メモNotionのデータベースID const DATABASE_ID = PropertiesService.getScriptProperties().getProperty('DATABASE_ID'); /** * 作成処理 */ function create(param) { // Notionページ登録URL const url = 'https://api.notion.com/v1/pages'; // Notionページ登録実行 UrlFetchApp.fetch(url, composeFetchParam(param)); } /** * データ登録に必要なパラメータを組み立てる */ function composeFetchParam(param) { const payload = { 'parent': {'database_id': DATABASE_ID}, 'properties': composeProperties(param), 'children': composeBlocks(param) }; 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; } /** * データ登録時のプロパティ */ function composeProperties(param) { return { 'タイトル': { 'title': [{ 'text': { 'content': param.title } }] } }; } /** * データ登録時のブロック */ function composeBlocks(param) { return [{ "type": "paragraph", "paragraph": { "rich_text": [{ "type": "text", "text": { "content": param.memo } }] } }] }
変更前は「コード.gs」というファイルにWeb周りの処理、Notion周りの処理がまとまっていました。
変更後は「main.gs」「notion.gs」と2つのファイルに分割しました。
「main.gs」にWeb周りの処理、「notion.gs」にNotion周りの処理がまとまっています。
gsファイル分割手順
gsファイルの分割手順は以下の通りです。
※拡張子の「.gs」の入力は不要
※この辺はお好みで
※この辺はお好みで
おわりに
ということで、「【GAS】gsファイルを分割する」に関してアレコレ書いてみました。
スクリプトを追加して、処理を移動するだけでgsファイルを分割することができました。
function名を変える必要もなく、別ファイルからそのままfunctionを実行できました。
こうなってくると、function名をもう少し工夫したほうが分かりやすくなるかもしれません。
今回の記事が参考になれば幸いです。
関連記事
GAS(Google Apps Script)に関してはいくつか記事にしています。
気になる記事があればぜひ。
GASの活用事例
GASでNotionにデータ登録するWebページを作ってみる - 派生記事