Programming Alerts in Google Apps Script

Following codes (JavaScript) show how to write various types of alerts in Google Apps Script (GAS). It also shows the usage of the alias for function in JavaScript.


 /**  
  * Alias of 'yesNoConfirm' function.  
  */  
 var alert_yesNo = yesNoConfirm;  
 /**  
  * Asks user for yes/no confirmation (dialog-box) and returns true for 'yes' and otherwise false.  
  *  
  * @version 1.1.0  
  *  
  * @param {String} title Title of the yes/no dialog-box.  
  * @param {String} message Message in the yes/no dialog-box.  
  * @return {Bollean|null} True if user selects 'yes', false is 'no', and null if 'cancel/close'.  
  */  
 function yesNoConfirm(title,message)  
 {  
  if(!message)  
  {  
   message = title;  
   title = "Confirmation";    
  }  
  var sheetUI = SpreadsheetApp.getUi();  
  var prompt = sheetUI.alert(title, message, sheetUI.ButtonSet.YES_NO_CANCEL);  
  if(prompt == "YES")  
   return true;  
  else if (prompt == "NO")  
   return false;  
  else  
   return null;  
 }
 /**  
  * Alias of 'info' function.  
  */  
 var alert_info = info;  
 /**  
  * Shows user an info dialog-box.  
  *  
  * @version 1.1.0  
  *  
  * @param {String} title Title of the yes/no dialog-box.  
  * @param {String} message Message in the yes/no dialog-box.  
  * @return {Bollean|null} True if user selects 'ok' else false 'close'.  
  */  
 function info(title,message)  
 {  
  if(!message)  
  {  
   message = title;  
   title = "Information";    
  }  
  var sheetUI = SpreadsheetApp.getUi();  
  var prompt = sheetUI.alert(title, message, sheetUI.ButtonSet.OK);  
  if(prompt == "OK")  
   return true;  
  else  
   return false;  
 }  
 /**  
  * Shows user an HTML info dialog box.  
  *  
  * @version 1.1.0  
  *  
  * @param {String} title Title of the yes/no dialog-box.  
  * @param {String} htmlMessage HTML message in the html dialog-box.  
  * @param {Number} width Dialog box width in pixel.  
  * @param {Number} height Dialog box height in pixel.   
  */  
 function alert_html(title,htmlMessage,width,height)  
 {   
  var htmlOutput = HtmlService.createHtmlOutput(htmlMessage)  
  .setWidth(width)  
  .setHeight(height);  
  SpreadsheetApp.getUi().showModalDialog(htmlOutput, title);   
 }  

Post a Comment