Last active
April 7, 2025 14:59
-
-
Save kepoorz/2a213beb1d40efec97c4d1756daf6fef to your computer and use it in GitHub Desktop.
メモ追加スクリプト - iOSショートカットからGoogleドライブにメモを追加するGAS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * メモ追加スクリプト - iOSショートカットからGoogleドライブにthino式のメモを追加するGAS | |
| * | |
| * 【使い方】 | |
| * 1. このコードをGoogle Apps Script(https://script.google.com/)に貼り付ける | |
| * 2. FOLDER_IDを自分のGoogleドライブフォルダIDに変更する | |
| * (フォルダを開いた時のURLの「folders/」以降の部分) | |
| * 3. デプロイ→新しいデプロイを選択し、種類を「ウェブアプリ」にする | |
| * 4. 次の設定で公開: | |
| * - 説明: メモ追加アプリ | |
| * - 次のユーザーとして実行: 自分 | |
| * - アクセスできるユーザー: 全員 | |
| * 5. デプロイボタンをクリック | |
| * 6. 生成されたURLをコピーする | |
| * | |
| * 【iOSショートカットの設定方法】 | |
| * 1. ショートカットアプリを開き、新規ショートカットを作成 | |
| * 2. 「プロンプト」アクションでテキスト入力を設定 | |
| * 3. 「URLの内容を取得」アクションを追加し: | |
| * - URL: デプロイ時に生成されたURL | |
| * - 方法: POST | |
| * - ヘッダー: Content-Type = application/x-www-form-urlencoded | |
| * - 本文: text = [プロンプトの回答] | |
| * 4. ショートカットを保存し、実行 | |
| * | |
| * 【機能説明】 | |
| * テキストを受け取り、Googleドライブの指定フォルダ内の | |
| * 今日の日付のMDファイル(例:2025-04-06.md)に | |
| * 「- HH:MM テキスト内容」の形式で追記します。 | |
| * Thinoと同じです。 | |
| */ | |
| // フォルダIDを設定(「URLのfolders/」以降の部分) | |
| const FOLDER_ID = 'フォルダIDをここに入力'; | |
| // POSTリクエスト処理 | |
| function doPost(e) { | |
| // デバッグ用にパラメータをログに記録 | |
| console.log("受信したパラメータ:", e.parameter); | |
| const text = e.parameter.text; | |
| // パラメータチェック | |
| if (text === undefined) { | |
| return ContentService.createTextOutput('⚠️ パラメータ「text」が見つかりません。'); | |
| } | |
| if (!text || text.trim() === '') { | |
| return ContentService.createTextOutput('⚠️ テキストが入力されていません。'); | |
| } | |
| const result = appendToFile(text); | |
| return ContentService.createTextOutput(result); | |
| } | |
| // GETリクエスト処理(テスト用) | |
| function doGet(e) { | |
| return ContentService.createTextOutput('このスクリプトはPOSTリクエストのみ対応しています。'); | |
| } | |
| // テキストをファイルに追加する関数 | |
| function appendToFile(text) { | |
| try { | |
| // 今日の日付でファイル名を生成 | |
| const today = new Date(); | |
| const year = today.getFullYear(); | |
| const month = String(today.getMonth() + 1).padStart(2, '0'); | |
| const day = String(today.getDate()).padStart(2, '0'); | |
| const fileName = `${year}-${month}-${day}.md`; | |
| // 現在の時刻を取得 | |
| const hours = String(today.getHours()).padStart(2, '0'); | |
| const minutes = String(today.getMinutes()).padStart(2, '0'); | |
| const timeStamp = `${hours}:${minutes}`; | |
| // フォルダを取得 | |
| const folder = DriveApp.getFolderById(FOLDER_ID); | |
| // ファイルが存在するか確認 | |
| let file; | |
| const fileIterator = folder.getFilesByName(fileName); | |
| if (fileIterator.hasNext()) { | |
| // ファイルが存在する場合は取得 | |
| file = fileIterator.next(); | |
| } else { | |
| // ファイルが存在しない場合は空のファイルを作成 | |
| file = folder.createFile(fileName, ''); | |
| } | |
| // ファイルの既存の内容を取得 | |
| let content = file.getBlob().getDataAsString(); | |
| // 末尾に改行がなければ追加してから、テキストを追記(リスト形式で時間付き) | |
| if (content && !content.endsWith('\n')) { | |
| content += '\n'; | |
| } | |
| content += `- ${timeStamp} ${text}\n`; | |
| // ファイルを更新 | |
| file.setContent(content); | |
| return '✅ メモを追加しました!'; | |
| } catch (error) { | |
| console.error(error); | |
| return '⚠️ エラーが発生しました: ' + error.toString(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment