0

I am running a script attached to a bouton in a sheet.

The current script is as follow.

function Ok() {

  var ui = SpreadsheetApp.getUi();
  var click = ui.alert("are you sure you want to send?",ui.ButtonSet.YES_NO)
  if (click == ui.Button.YES){
   send(); 
  }
}

Now I am trying to add a validation before running the Script "send". If cell Q1 = 1 ( I need an alert box) If cell Q1 = 0 ( I need to run the script as it is currently)

I know that this is probably very simple but I can find the information

1 Answer 1

0

I think that you are taking the wrong approach.

Rhetorical question

What about if instead of doing the validation "before running the script" you include that validation when the script is running? This could be done in the same way that you are are checking that the YES button was clicked.

If what you are looking for is how to read the value of Q1 use

var name = 'Sheet1'; // Put here the name of the sheet holding the Q1 cell of interested
var value = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(name).getRange('Q1').getValue();

NOTE: For brevity the above code include only two code lines, you might want to use a variable for each of the spreadsheet, sheet and range objects to make it easier to debug / troubleshoot.

Here is a quick & dirty way to apply the above:

function Ok() {

  var ui = SpreadsheetApp.getUi();

  var name = 'Sheet1'; // Put here the name of the sheet holding the Q1 cell of interested
  var value = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(name).getRange('Q1').getValue();
  if(value !== 0) {
    ui.alert('Q1 is different from 0');
    return;
  }
  var click = ui.alert("are you sure you want to send?",ui.ButtonSet.YES_NO)
  if (click == ui.Button.YES){
   send(); 
  }
}
3
  • now I have the following error. TypeError: Cannot find function getActiveSpredsheet in object SpreadsheetApp. O Jan 29, 2020 at 17:11
  • 1
    It is working. I change the code for a simple line var value = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getRange('Q1').getValue(); Jan 29, 2020 at 17:23
  • I'm glad that you figured by yourself that was an unintended typo. I understand that this answer help you, please accept it. Ref. What I should do when someone answer my question. Jan 29, 2020 at 19:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.