スプシの変更内容を一定期間毎にDiscordへ通知したい

実現したいこと

GAS(JS)初心者です。
以下サイト記載の構文をコピペにて実行したのですが、デバッグ時に幾つかの型がundefinedとなり通らない。

前提

件名記載要件を満たしそうなサイトが以下にあったので、こちらのソースコードをコピペ。
7行目にて当該エラー発生。

https://qiita.com/daijin/items/5de520af98702f15f432

発生している問題・エラーメッセージ

TypeError: Cannot read properties of undefined (reading 'source') recordToProperty @ task.gs:7

該当のソースコード

task.gs

1//////////////////////////////////////////////////////////////////////////////// 2// 編集時に発行されるイベント 3//////////////////////////////////////////////////////////////////////////////// 4function recordToProperty(e) { 5 // 変更されたのが指定したシートの場合のみ有効 6 // ★★★編集を監視したいシート名に置き換える 7 if (e.source.getSheetName() != 'シート1') { 8 return; 9 } 10 11 // 変更時刻 12 const time = new Date().getTime(); 13 14 // メッセージに変更された範囲、新しい値を入れる 15 const message = [e.range.getA1Notation(), e.value]; 16 17 // 変更があったことをスクリプトプロパティに追加。keyは変更時刻 18 const script_property = PropertiesService.getScriptProperties(); 19 script_property.setProperty(time, JSON.stringify(message)); 20} 21 22//////////////////////////////////////////////////////////////////////////////// 23// 定期実行されるイベント 24//////////////////////////////////////////////////////////////////////////////// 25function notifyToDiscord(e) { 26 // スクリプトプロパティの変更をチェック。 27 const script_property = PropertiesService.getScriptProperties(); 28 const properties = script_property.getProperties(); 29 const array = Object.entries(properties); 30 31 // スクリプトプロパティに1件もなければ、この期間に変更なしと見なす。 32 if (array.length == 0) { 33 return; 34 } 35 36 // プロパティがあったので変更あり。Webhookを送信する。 37 const WEBHOOK_URL = 'https://discord.com/api/webhooks/xxxxxx/xxxxxx'; // ★★★DiscordのWebhook URL 38 39 // メッセージを作成 40 let message_body = 'シートに変更があったよ!!\n'; 41 let embeds = { 42 fields: [], 43 }; 44 45 array.forEach((prop) => { 46 const key = prop[0]; // time 47 const val = JSON.parse(prop[1]); // message 48 embeds.fields.push({ 49 name: 'セル: ' + val[0], 50 value: '変更後の内容: ' + val[1], 51 }); 52 }); 53 54 const payload = { 55 'username': /* ★★★投稿時のユーザー名。指定しない(デフォのままで良い)場合は、行ごと削除する */, 56 'content': message_body, 57 'tts': false, 58 'embeds': [embeds], 59 } 60 61 const param = { 62 'method': 'POST', 63 'headers': { 64 'Content-type': 'application/json' 65 }, 66 'payload': JSON.stringify(payload) 67 } 68 UrlFetchApp.fetch(WEBHOOK_URL, param); 69 70 // 送信成功後、プロパティを全削除 71 script_property.deleteAllProperties(); 72} 73

試したこと

if (e.source.getSheetName() != 'シート1') { return; }

を削るなどでの動作確認。

補足情報(FW/ツールのバージョンなど)

ここにより詳細な情報を記載してください。

コメントを投稿

0 コメント