1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
function deleteTriggers() { writeLog("trigger削除"); let triggers = ScriptApp.getProjectTriggers(); for(let i = 0; i < triggers.length; i++){ ScriptApp.deleteTrigger(triggers[i]); } } function writeLog(x){ let ss = SpreadsheetApp.getActiveSheet(); let range = ss.getRange("B1:C100"); let values = range.getValues(); for(let r = 0; r < values.length; r++){ if (values[r][0] == ""){ range.getCell(r+1,1).setValue(x); range.getCell(r+1,2).setValue( new Date().toTimeString().slice(0, 8) ); break; } } } function main() { // クリア let ss = SpreadsheetApp.getActiveSheet(); ss.clear(); ss.getRange("A1").setValue(0); writeLog("main開始"); deleteTriggers(); ScriptApp.newTrigger('trigger').timeBased().after(1000).create(); writeLog("trigger登録"); writeLog("main終了"); } function trigger() { writeLog("trigger開始"); let startTime = new Date().getTime(); while (SpreadsheetApp.getActiveSheet().getRange("A1").getValue() <= 20) { Utilities.sleep(1000); // 1秒 SpreadsheetApp.getActiveSheet().getRange("A1").setValue( SpreadsheetApp.getActiveSheet().getRange("A1").getValue() + 1 ); writeLog(SpreadsheetApp.getActiveSheet().getRange("A1").getValue()); let elapsedTime = (new Date().getTime() - startTime) / 1000; if (elapsedTime > 5){ deleteTriggers(); ScriptApp.newTrigger('trigger').timeBased().after(1000).create(); writeLog("trigger登録"); return; } } deleteTriggers(); writeLog("trigger終了"); } |