Created
March 18, 2019 15:14
-
-
Save AWtnb/02c5642024a3ee8a68edfccbfaa8d494 to your computer and use it in GitHub Desktop.
get data from google sheet with 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
| /* | |
| slack 経由で辞典のデータを参照 | |
| */ | |
| ///////////////////////////////////////////// | |
| // グローバル変数 | |
| ///////////////////////////////////////////// | |
| // シート | |
| var SHEET_ID = PropertiesService.getScriptProperties().getProperty("SHEET_ID"); | |
| var sht = SpreadsheetApp.openById(SHEET_ID).getSheets(); | |
| // token | |
| var WEBHOOK_URL = PropertiesService.getScriptProperties().getProperty("WEBHOOK_URL"); | |
| ///////////////////////////////////////////// | |
| // slack 関連 | |
| ///////////////////////////////////////////// | |
| // slackに投稿する関数 | |
| function send2slack(messageBlock, messageText) { | |
| var payload = { | |
| "channel": "#dict_item", | |
| "subtype": "bot_message", | |
| "text":messageText, | |
| "icon_emoji": ":whale2:", | |
| "username": "dictBot", | |
| "blocks": messageBlock | |
| } | |
| var options = { | |
| "method" : "POST", | |
| "payload": JSON.stringify(payload) | |
| } | |
| UrlFetchApp.fetch(WEBHOOK_URL, options); | |
| } | |
| // 投稿用の block を作成する関数 | |
| function makeSimpleTextBlock (text) { | |
| return { | |
| "type": "section", | |
| "text": { | |
| "type": "mrkdwn", | |
| "text": text | |
| } | |
| } | |
| } | |
| // context block を作成する関数 | |
| function makeContextBlock (elements) { | |
| return { | |
| "type": "context", | |
| "elements": elements | |
| } | |
| } | |
| // context block の element を作成する関数 | |
| function makeElement (text) { | |
| return { | |
| "type": "mrkdwn", | |
| "text": text | |
| } | |
| } | |
| ///////////////////////////////////////////// | |
| // データ検索用関数 | |
| ///////////////////////////////////////////// | |
| // 項目名で検索 | |
| function findbyItem(itemname) { | |
| var dataRange = sht[0].getRange(2,1,sht[0].getLastRow()-1,3); | |
| var reg = new RegExp(itemname, 'g'); | |
| var matched = []; | |
| for each(var record in dataRange.getValues()) { | |
| if ((record[0]).match(reg)) { | |
| matched.push("*" + record[0] + "* (" + record[1] + " " + record[2] + "G)"); | |
| } | |
| } | |
| return matched | |
| } | |
| // slack の OutGoingWebHook で指定チャンネルに投稿された文面を取得 | |
| function doPost (e) { | |
| if (e == null || e.postData == null || e.postData.contents == null || e.parameter.user_name == "slackbot") { | |
| return; | |
| } | |
| // 投稿内容取得 | |
| var txt = e.parameter.text; | |
| // 検索 | |
| var found = findbyItem(txt); | |
| // 件数確認 | |
| var nItem = found.length; | |
| if (nItem < 1) { | |
| var msg = "`" + txt + "` は見つかりませんでした"; | |
| var context = makeContextBlock([makeElement("no found")]); | |
| } | |
| else { | |
| var msg = "`" + txt + "` " + nItem + "件ヒット"; | |
| var context = makeContextBlock([makeElement(found.join("\r\n"))]); | |
| } | |
| // slack に投稿 | |
| send2slack( | |
| [ | |
| makeSimpleTextBlock(msg), | |
| context | |
| ], | |
| msg | |
| ); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment