Show Alert Popup when cell selection is changed (onSelectionChange)

不打扰是莪最后的温柔 提交于 2021-02-02 09:44:26

问题


I am trying to show an alert popup when cell selection is changed, using the new trigger onSelectionChange. For some reason it is not showing any alerts. Am i doing something wrong or alerts does not work with this trigger?

function onSelectionChange(e) {
      showAlert();
}
function showAlert() {
  var ui = SpreadsheetApp.getUi();
  var result = ui.alert(
                  'ALERT!',
                  'ALERT MESSAGE.',
               ui.ButtonSet.OK);
}

I also tried that way:

function onSelectionChange(e) {
  var ui = SpreadsheetApp.getUi();
  var result = ui.alert(
                  'ALERT!',
                  'ALERT MESSAGE.',
               ui.ButtonSet.OK);
}

回答1:


I created a script to test different ways to show a "pop up" in Google Apps Script. In both runtimes, only the one that use the HTML Service throw an error. The test was done using Chrome, a G Suite account, only signed in in one account.

Here is the code of the referred script:

function onSelectionChange(e) {
  var message = e.range.getA1Notation();
  switch(e.range.columnStart){
    case 1:
      alert(message);
      break;
    case 2:
      toast(message);
      break;
    case 3:
      msgBox(message);
      break;
    case 4:
      dialog(message);
      break;
    case 5:
      alertWithButton(message);
      break;
    default:
    console.info(message);
  }
}

function alert(message){
  SpreadsheetApp.getUi().alert(message);
}

function toast(message){
  SpreadsheetApp.getActiveSpreadsheet().toast(message);
}

function msgBox(message){
  Browser.msgBox(message);
}

function dialog(message){
  SpreadsheetApp.getUi().showModalDialog(
    HtmlService.createHtmlOutput(message), 
  'Alert'
  )
}

function alertWithButton(message){
  var ui = SpreadsheetApp.getUi();
  ui.alert(message, ui.ButtonSet.OK);
}


来源:https://stackoverflow.com/questions/63642589/show-alert-popup-when-cell-selection-is-changed-onselectionchange

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!