wren Jago
wren Jago

Reputation: 65

Exception: Argument cannot be null: prompt Error in google Scripts

I have created a mail merge with a confirmation pop up in order to confirm the script has not been run recently, and also to double check the user intends to send emails. The script works perfectly, but annoyingly I receive the following error when run: enter image description here

Stating; Exception: Argument cannot be null: prompt

I would like to get rid of this if possible. I attach my code.

//Creates a functional log of how many emails are sent and to how many branches

function confirmationWindow(){
  var numberSent = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Validations').getRange('D3').getValue();
  var numberOfVios = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Validations').getRange('D2').getValue();
  var ui = SpreadsheetApp.getUi();
  var ui = SpreadsheetApp.getUi();
      ui.alert(
        'You have succesfully sent ' + numberOfVios + ' violations, to ' + numberSent + ' branches.',)

}

function saveData() {
  var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Log');
  var date = new Date()
  var user = Session.getActiveUser().getEmail();
  var range = ss.getRange("A:B")
  var numberSent = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Validations').getRange('D2').getValue();
  var branchNumber = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Validations').getRange('D3').getValue();
  ss.appendRow([date,user,numberSent,branchNumber]);
}

var EMAIL_SENT = 'True';

function sendEmails() {
  var sheet = SpreadsheetApp.getActive().getSheetByName('Output');
  var sheetI= SpreadsheetApp.getActive().getSheetByName('Input');
  var sheetC = SpreadsheetApp.getActive().getSheetByName('Control');
  var emailNum = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Validations').getRange('D3').getValue();
  var startRow = 2; 
  var numRows = emailNum; 
  var dataRange = sheet.getRange(startRow, 1, numRows, 6);
  sheetI.setTabColor("ff0000");
  saveData()
  
  var data = dataRange.getValues();
  for (var i in data) {
    var row = data[i];
    var emailAddress = row[3]; 
    var message = row[5] + "\n\nT\n\nT\n\nT"; 
    var message = message.replace(/\n/g, '<br>');
    var subject = row[4];
    MailApp.sendEmail(emailAddress, subject, message,{htmlBody:message});
    sheet.getRange(startRow + i - 18, 8).setValue(EMAIL_SENT);
    sheetC.getRange(startRow + i - 18, 3).setValue(EMAIL_SENT);
    SpreadsheetApp.flush();
  }
  confirmationWindow()
}


function recentSend() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Log');
  var lastR = sheet.getLastRow();
  var recentRun = sheet.getRange('A' + lastR).getValue();
  if(lastR=1){var recentlySent = 'False'}
  var today = new Date().valueOf()
  var recentDate =  recentRun
  var sec = 1000;
  var min = 60 * sec;
  var hour = 60 * min;
  var day = 24 * hour;
  var difference = today - recentDate
  var hourDifference =Math.floor(difference%day/hour);
  if (hourDifference > '12'){var recentlySent = 'False'}
  else {var recentlySent = 'True'}
  return(recentlySent)
}
function recentlySentTrue(){
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Log');
  var lastR = sheet.getLastRow();
  var recentUser = sheet.getRange('B' + lastR).getValue();
  var recentQuant = sheet.getRange('C' + lastR).getValue();
  var recentT = sheet.getRange('A' + lastR).getValue();
  var recentSendLog = recentT.toString();
  var recentTime = recentSendLog.slice(16,21) 
  var recentDate = recentSendLog.slice(0,11)
  var ui = SpreadsheetApp.getUi();
      var result = ui.alert(
        'Are you sure you wish to send?',
        'The user, ' + recentUser + ' ,recently sent ' + recentQuant + ' emails, at ' + recentTime + ' on ' + recentDate,
          ui.ButtonSet.YES_NO);
      if (result == ui.Button.YES) {

        ui.alert(recentlySentFalse());
    }   
    else {
    }
    }

function recentlySentFalse(){
  var ui = SpreadsheetApp.getUi();
  var ui = SpreadsheetApp.getUi();
      var result = ui.alert(
        'Are you Sure you wish to send?',
        'Are you sure you want to send these violations?',
          ui.ButtonSet.YES_NO);
      if (result == ui.Button.YES) {
        ui.alert(sendEmails());
    }   else {
    }
}



function confirm(){
  var recentlySent = recentSend();
  if (recentlySent == 'True'){
    recentlySentTrue()
  } 
  else{recentlySentFalse()
  }
}
    
  



 

Any help would be great.

Upvotes: 1

Views: 2081

Answers (1)

CMB
CMB

Reputation: 5163

Explanation:

Your ui.alert() calls that have function arguments such as recentlySentFalse() are not valid, since the functions do not have return statements, hence they return the default value of null. ui.alert() calls need at least a prompt message.

Solution 1:

Put the function call after the UI alert message:

      if (result == ui.Button.YES) {

        ui.alert('Sending message...');
        recentlySentFalse();
    }   

Solution 2:

If you do not want excessive alerts, just remove ui.alert and call the function immediately.

      if (result == ui.Button.YES) {

        recentlySentFalse();
    }   

References:

Class UI | Apps Script

Upvotes: 4

Related Questions