// set timer 08:59 functionsetTrigger08() { var triggerDay = newDate(); triggerDay.setHours(08); triggerDay.setMinutes(59); ScriptApp.newTrigger("main").timeBased().at(triggerDay).create(); }
// set timer 17:59 functionsetTrigger17() { var triggerDay = newDate(); triggerDay.setHours(17); triggerDay.setMinutes(59); ScriptApp.newTrigger("main").timeBased().at(triggerDay).create(); }
// delete Trigger functiondeleteTrigger() { var triggers = ScriptApp.getProjectTriggers(); for(var i=0; i < triggers.length; i++) { if (triggers[i].getHandlerFunction() == "main") { ScriptApp.deleteTrigger(triggers[i]); } } }
// main method functionmain() { deleteTrigger(); core_function(); Logger.log('main'); }
functioncore_function() { // Get script sheet URL var spreadSheet = SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/xxxxxx'); // Get the first sheet var sheet = spreadSheet.getSheets()[0]; // Get Rows var lastrow = sheet.getLastRow(); // Get current date var currentDay = newDate(); // To determine if a current date is a workday or not functionisWeekDay(currentDate) { var weekday = currentDate.getDay(); if (weekday == 0 || weekday == 6) { returnfalse; } returntrue; } // To determine if a current date is a holiday or not functionisEventsForDay(currentDate) { var calendar = CalendarApp.getCalendarById('ja.japanese#holiday@group.v.calendar.google.com'); if (calendar.getEventsForDay(currentDate, {max: 1}).length > 0) { returntrue; } returnfalse; } // Get name ,date and metion name array var name_array = sheet.getSheetValues(2, 1, lastrow, 1); var date_array = sheet.getSheetValues(2, 2, lastrow, 1); var mention_array = sheet.getSheetValues(2, 3, lastrow, 1); // Set date format for today var today = Utilities.formatDate(newDate(), "Asia/Tokyo", "yyyy/MM/dd"); var date = newArray(); // Roop to get date_number var date_num = date_array.length; for(var i = 0; i < date_num - 1; i++) { // Set date format for date date.push(Utilities.formatDate(newDate(date_array[i]), "Asia/Tokyo", "yyyy/MM/dd")); // Get index if today = date var num = date.indexOf(today); } // Get name through index we got var member = name_array[num]; var mention = mention_array[num]; // if it is workday and not holiday then set message var message; if(member && isWeekDay(currentDay) && !isEventsForDay(currentDay)) { message = 'Hey〜!:sunny:\n 今日の当番は *' + member + '* です! <@' + mention + '>: \n 本日「加湿器ON/OFF」、「終礼」、「ゴミ捨て」をあなたに任せるよ !! \n ※都合が悪い時にみんなに言ってくださいね'; // Send message to slack Logger.log(message); postSlack(message) } functionpostSlack(message) { // Webhook URL you copied when you created a app var url = "https://hooks.slack.com/xxxxxxxx"; var options = { "method" : "POST", "headers": {"Content-type": "application/json"}, "payload" : '{"text":"' + message + '"}' }; // Run here UrlFetchApp.fetch(url, options); } }