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 |
function onEdit(e) { // セルの編集イベントで発火させる場合 return; let r = e.range.getRow(); let c = e.range.getColumn(); if (r == 2 && (c == 2 || c == 3)) { setFilter(); // toast表示する場合 SpreadsheetApp.getActiveSpreadsheet().toast("フィルタ設定完了しました。",""); } } function clearFilter() { let s = SpreadsheetApp.getActiveSheet(); let f = s.getFilter(); if (f) f.remove(); } function setFilter() { let s = SpreadsheetApp.getActiveSheet(); let f = s.getFilter(); if (f) f.remove(); let v1 = s.getRange("B2").getValue(); let v2 = s.getRange("C2").getValue(); if (v1 == "" && v2 == "") return; f = s.getRange("B5:H" + s.getLastRow()).createFilter(); let lock = LockService.getDocumentLock(); try { lock.waitLock(10000) if (v1 != "") f.setColumnFilterCriteria(2, SpreadsheetApp.newFilterCriteria().whenTextContains(v1).build()); if (v2 != "") f.setColumnFilterCriteria(3, SpreadsheetApp.newFilterCriteria().whenTextContains(v2).build()); } finally { lock.releaseLock(); } } |