Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

google apps script - Show Alert Popup when cell selection is changed (onSelectionChange)

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);
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...