Upgrade to Pro — share decks privately, control downloads, hide ads and more …

GASでSlackBot

takuaraki
August 22, 2018

 GASでSlackBot

Google App Scriptsを利用して勤怠管理のSlackBotを作った話です。

takuaraki

August 22, 2018
Tweet

More Decks by takuaraki

Other Decks in Programming

Transcript

  1. γεςϜߏ੒ ("4 ۈଵه࿥ (PPHMF 4QSFBE 4IFFU ("4
 ۈଵूܭ 0VUHPJOH
 8FC)PPLT

    ۈଵೖྗ ࠓ೔ͷۈଵ͸ʁ ूܭ݁Ռදࣔ 1045
 ϦΫΤετ σʔλΛ ه࿥ σʔλΛ औಘ 1045
 ϦΫΤετ ूܭ݁ՌΛ 4MBDLʹ౤ߘ
  2. γεςϜߏ੒ ("4 ۈଵه࿥ (PPHMF 4QSFBE 4IFFU ("4
 ۈଵूܭ 0VUHPJOH
 8FC)PPLT

    ۈଵೖྗ ࠓ೔ͷۈଵ͸ʁ ूܭ݁Ռදࣔ 1045
 ϦΫΤετ σʔλΛ ه࿥ σʔλΛ औಘ 1045
 ϦΫΤετ ूܭ݁ՌΛ 4MBDLʹ౤ߘ
  3. ۈଵه࿥ɿ("4 function doPost(e) { // ର৅ͷ೔෇Λநग़ var pattern = /(?:\d{4}\/|)([1-9]|0[1-9]|1[0-2])\/([1-9]|0[1-9]|[12][0-9]|3[01])/;

    var matchResult = e.parameter.text.match(pattern); var date = new Date().getFullYear() + "/" + matchResult[1] + "/" + matchResult[2]; // εϓϨουγʔτʹ೔෇ɺ໊લɺຊจΛه࿥ var sheet = getCurrentYearSpreadsheet().getSheets()[0]; var kintaiValues = sheet.getSheetValues(1, 1, maxRowCount, 3); var newLineRow = -1; for (var row = 0; row < 10000; row++) { var dateCell = kintaiValues[row][0]; if (dateCell == "") { newLineRow = row + 1; break; } } arrData = [ [date, e.parameter.user_name, e.parameter.text] ]; sheet.getRange(newLineRow, 1, 1, 3).setValues(arrData); }
  4. γεςϜߏ੒ ("4 ۈଵه࿥ (PPHMF 4QSFBE 4IFFU ("4
 ۈଵूܭ 0VUHPJOH
 8FC)PPLT

    ۈଵೖྗ ࠓ೔ͷۈଵ͸ʁ ूܭ݁Ռදࣔ 1045
 ϦΫΤετ σʔλΛ ه࿥ σʔλΛ औಘ 1045
 ϦΫΤετ ूܭ݁ՌΛ 4MBDLʹ౤ߘ
  5. γεςϜߏ੒ ("4 ۈଵه࿥ (PPHMF 4QSFBE 4IFFU ("4
 ۈଵूܭ 0VUHPJOH
 8FC)PPLT

    ۈଵೖྗ ࠓ೔ͷۈଵ͸ʁ ूܭ݁Ռදࣔ 1045
 ϦΫΤετ σʔλΛ ه࿥ σʔλΛ औಘ 1045
 ϦΫΤετ ूܭ݁ՌΛ 4MBDLʹ౤ߘ
  6. ۈଵूܭɿ("4 function doPost(e) { var now = new Date(); var

    slackMessage = "```\n”; var sheet = getCurrentYearSpreadsheet().getSheets()[0]; var kintaiValues = sheet.getSheetValues(1, 1, maxRowCount, 3); for (var row = 1; row < 10000; row++) { var date = kintaiValues[row][0]; if (date == "") { break; } // ݱࡏ೔෇ͱಉ͡σʔλ͚ͩநग़ͯ͠ɺ౤ߘ༻ϝοηʔδʹ௥Ճ if (date.getFullYear() == now.getFullYear() && date.getMonth() == now.getMonth() && date.getDate() == now.getDate()) { var name = kintaiValues[row][1]; var text = kintaiValues[row][2]; slackMessage += name + " " + text + "\n"; } } slackMessage += "```"; sendToSlack(slackMessage); }