vendredi 1 février 2019

YesNo box won't terminate code when selecting "No"

I'm trying to create a macro to have a box prompt to confirm the action like "Do you want to clear the sheet?" with a yes or no question. If YES, to delete the given cells If NO, do nothing.

The YES condition works. But I can't seem to make the NO work. Even if I click on NO, it still executes the clear command. What am I doing wrong?

I'm not well versed in coding. I simply rely in Google. I have tried to change the "else" to "End if" and also tried "then" but none works. I'm not sure how to stop the code from running.

function myFunction() {
var sh1=SpreadsheetApp.getActive();
  var sheet1=sh1.getSheetByName("Sheet1")

var sh=SpreadsheetApp.getUi();
  var response=sh.alert("Clear?",sh.ButtonSet.YES_NO)

  if(response.YES);{

  var sheet = SpreadsheetApp.getActive().getSheetByName('Sheet1');
  sheet.getRange('b1:b21').clearContent();
  sheet.getRange('C13:C18').clearContent();
  }
  Else {}
}

Even clicking NO from the prompt it still executes the clearContent command.

Aucun commentaire:

Enregistrer un commentaire