1
votes

code.gs

var runSelect = true
function onSelectionChange(e) {
  var sheet = SpreadsheetApp.getActiveSheet()
  var range = e.range;
  var column = range.getColumn();
  var lastRow = sheet.getLastRow();
  var ui = SpreadsheetApp.getUi();
  if (runSelect == true) {
    sheet.getRange(5, column, lastRow-4).activate();
    sheet.getRange(1, column).copyTo(sheet.getActiveRange());
    ui.alert("Finnish")

I have try to change my selection, but alert never show and not do copy

4
view>stackdriver Logs - TheMaster
Does onSelectionChange not work for you at all? It's a new feature that currently is sometimes a bit moody. - ziganotschka
I don't think alert would trigger from simple trigger. Can you add console.log('test') and see if it would appear in logs, as TheMaster suggested? - roma

4 Answers

3
votes

This works pretty good and it can also be used for people that have wanted to trigger an event when clicking on a cell in past questions.

function onSelectionChange(e) {
  var sh=e.range.getSheet();
  e.source.toast('Sheet: ' + sh.getName() + ' Range: ' + e.range.getA1Notation());  
}
1
votes

According to https://developers.google.com/apps-script/reference/base/ui.html alert(prompt) does not require authorization. Probably the onSelectionChange trigger is not executed and you are using the new V8 runtime. Use the legacy runtime, not the V8 runtime. With V8 runtime in my case sometimes it is triggered, sometimes not. There are general problems with trigger executions in V8, see https://issuetracker.google.com/issues/147016387

0
votes

Same issue happened to me. I run the code manually from the editor and it asked for authorization. I authorized it and viola! Now the onSelectionChange trigger works! I hope it may be helpful for someone passing by...

0
votes

I just faced the same issue on a new spreadsheet with a new project having only a very simple onSelectionChange:

function onSelectionChange(e) {
  console.log(JSON.stringify(e));
}

After reopening the spreadsheet (refreshing the web browser tab), the onSelectionChange worked fine.