Google Apps Sctript multiple run onEdit event issue -
i need move 1 row sheet if specific value entered.
i wrote below function.
function movearchived(event) { var sheetnametowatch = "queue"; var columnnumbertowatch = 15; // column = 1, b = 2, o = 15, etc. var valuetowatch = "archive"; var sheetnametomovetherowto = "archive"; var ss = spreadsheetapp.getactivespreadsheet(); var sheet = event.source.getactivesheet(); var range = event.source.getactivecell(); if (sheet.getname() == sheetnametowatch && range.getcolumn() == columnnumbertowatch && range.getvalue() == valuetowatch) { var ui = spreadsheetapp.getui(); var response = ui.alert('do want move row archive sheet?', ui.buttonset.yes_no); if (response == ui.button.no) { return } var targetsheet = ss.getsheetbyname(sheetnametomovetherowto); var targetrange = targetsheet.getrange(targetsheet.getlastrow() + 1, 1); sheet.getrange(range.getrow(), 1, 1, sheet.getlastcolumn()).moveto(targetrange); sheet.deleterow(range.getrow()); } } function onedit(event) { movearchived(event); }
it works perfect, moving row if cell in column "o" (15) edited value "archive".
the problem happens when customer edits 2 rows, 1 one, fast, onedit triggered twice, 1 after another.
the first 1 move corresponding row, second onedit event move different row, rows numbers changed after previous onedit event.
here more details of issue:
customer edits row[1] , row[3].
the first onedit, remove row[1], row[3] become row[2] but, second onedit event range.getrow()
still returns 3, row[3] removed , not row[2] expected.
thank suggestion.
i thought using lockservice ideal solution use case since purpose prevent concurrency between 2 or more function executions i'm bit disappointed result get...
maybe find better way implement or suggest solution ;-)
in mean time, can give try code below. made few changes, 1 of them ability handle multiple cells @ once (if user paste many cells 'archive' in it).
i changed range definition using range coming event and, finally, used installable onedit instead of simple 1 because had feeling worked more reliably (although there no objective reason that... maybe dreamed ;-)) , changed onedit function name avoid double trigger.
so you'll have add new onedit trigger ressources menu in script editor.
after these warnings thing can works ... not ! , that's imho annoying situation when using scripts ... -as said - try before buy , cross fingers hoping improve shortly.
code below :
function movearchived(event){ var lock = lockservice.getpubliclock(); // wait 30 seconds other processes finish. lock.waitlock(30000); var sheetnametowatch = "queue"; var columnnumbertowatch = 15; // column = 1, b = 2, o = 15, etc. var valuetowatch = "archive"; var sheetnametomovetherowto = "archive"; var ss = spreadsheetapp.getactivespreadsheet(); var sheet = event.source.getactivesheet(); var range = event.range; if (sheet.getname() == sheetnametowatch && range.getcolumn() == columnnumbertowatch && range.getvalue().indexof(valuetowatch)>-1) { var ui = spreadsheetapp.getui(); var response = ui.alert('do want move this/these row(s) archive sheet?', ui.buttonset.yes_no); if (response == ui.button.no) { lock.releaselock(); return } var targetsheet = ss.getsheetbyname(sheetnametomovetherowto); var targetrange = targetsheet.getrange(targetsheet.getlastrow() + 1, 1); range.moveto(targetrange); for(var n = event.range.rowstart ; n < event.range.rowend ; n++){ sheet.deleterow(n); } } lock.releaselock(); } function installonedit(event){ logger.log(json.stringify(event)); movearchived(event); }
Comments
Post a Comment