Tom Sawkins
Tom Sawkins

Reputation: 361

Add an ALERT onEdit - Google Sheets / Google Apps Script

The code below triggers when the checkbox gets checked. At that time I would like an alert to popup with a message "Are you sure ?" - BEFORE onEdit runs. So select YES - onEdit runs. Select NO onEdit doesnt run

here is onEdit

function JESSIEToUnassigned(e) {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var s = e.source.getActiveSheet();
  var r = e.source.getActiveRange();
  if (s.getName() == "JESSIE" && r.getColumn() == 30 && r.getValue() == true) {
    var row = r.getRow();
    var numColumns = s.getLastColumn();

    var targetSS = SpreadsheetApp.openById("");
    s = SpreadsheetApp.getActiveSheet();
    var tempSheet = s.copyTo(targetSS);
    var targetSheet = targetSS.getSheetByName("Current DHL Jobs - UNASSIGNED");
    var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 5);

    // row - whatever row was checked. 5 is 5th Column accross - 1 is 1 row - 3 copy 3 columns from the 
    // 5 th
    tempSheet.getRange(row, 1, 1, 30).copyTo(target);
    s.deleteRow(row)
    AddCheckBoxToA3_D3();
  }
}

and here is the alert

function showAlert() {
  var ui = SpreadsheetApp.getUi(); // Same variations.
  var result = ui.alert(
    'Are you sure you want to continue?',
    ui.ButtonSet.YES_NO);


  // Process the user's response.
  if (result == ui.Button.YES) {
    // User clicked "Yes".
    JESSIEToUnassigned(e);
  } else {
    // User clicked "No" or X in the title bar.
    ui.alert('Code Stopped.');
  }
}

I have played around with the alert in different parts of the onedit code but I am obviously missing something

Thanks in Advance

Upvotes: 1

Views: 1188

Answers (1)

Jeff Rush
Jeff Rush

Reputation: 912

What you could do is : if checkbox in cell A1 is checked, alert me "Are you sure?"

Here is a working snippet:

To reproduce it add Checkbox to A1

function onEdit(e) {

  const targetCell = "A1";

  if(e.range.getA1Notation() === targetCell){
    if(e.range.getValue() === true){
      SpreadsheetApp.getUi().alert("Are you sure?")
    } 
  }

}

Now you could adapt it to your project

Upvotes: 1

Related Questions