-1

My "if" statement in the script is ignored and all of the statements (options) and are run by the app. What am I doing wrong?

function ClearDataMacro() {
 var spreadsheet = SpreadsheetApp.getActive();
 // end if range (A3:E5)= "BOOKED"
 var READY=""
 var READY= spreadsheet.getRange('A3:E5').getValues();
 if (READY = "BOOKED")
 {
 spreadsheet.getRange('Y1:Z1').activate();
 spreadsheet.getCurrentCell().setValue("OPTION ONE");
 }
 spreadsheet.getRange('Y2:Z2').activate();
 spreadsheet.getCurrentCell().setValue("OPTION TWO")
 { 
spreadsheet.getRange('Y3:Z3').activate();
spreadsheet.getCurrentCell().setValue("OPTION THREE");
 }
spreadsheet.getRange('Y4:Z4').activate();
spreadsheet.getCurrentCell().setValue("OPTION FOUR");
}
TheMaster
  • 32,296
  • 6
  • 31
  • 56
T W
  • 9
  • 2
  • 4
    = is an assignment. == is a comparison. – Cooper Jun 07 '20 at 11:06
  • 1
    In addition, `READY` is a 2D array. Array is not equal to a string. In js two different objects are never strictly equal (`===`). You need to loop over. Look into for-loops, ``.includes``, `.forEach` – TheMaster Jun 07 '20 at 11:50
  • 1
    Consider taking a basic js course online. It'll save you a lot of time in the long run and probably now too. See [tag info page](https://stackoverflow.com/tags/google-apps-script/info) for more details. – TheMaster Jun 07 '20 at 11:52
  • Thank you very much Cooper. Tripped up by something so simple. Thank you both. – T W Jun 07 '20 at 11:53
  • The script I really want is "not equal to" <> , but my attempts to replicate this have failed. The script is not accepted. – T W Jun 07 '20 at 12:02
  • 1
    Show us your attempt at fixing it – Cooper Jun 07 '20 at 13:00
  • // end if range (A3:A5)= "BOOKED" var READY="" spreadsheet.getRange('A3:E5').getValues(); /// I don't know how to do a string array to find that READY does not equal to booking so I use this line instead. if (READY < "A BLOCK OF TEXT") { Program is here } {} }; – T W Jun 07 '20 at 13:16
  • Excuse the lack of formatting. My problem no is that READY is not a string array so I don't know how to compare it against text. I have used an archaic method. of just acknowledging that something is in the cell to trigger the program ending. – T W Jun 07 '20 at 13:21
  • Perhaps my example below will provide you with a framework you can work with. – Cooper Jun 07 '20 at 14:42

1 Answers1

0

Perhaps this example function will give you some help:

function examplefunc() {
  const ss=SpreadsheetApp.getActive();
  const sh=ss.getActiveSheet();
  const rg=sh.getDataRange();
  const vs=rg.getValues();
  let bookings=[];
  vs.forEach(function(r,i){
    r.forEach(function(c,j){
      if(c=="BOOKED") {
        bookings.push({row:i+1,col:j+1,value:"BOOKED"});
      }
    });
  });
  SpreadsheetApp.getUi().showModelessDialog(HtmlService.createHtmlOutput(bookings.join('<br />'), "Bookings")
}
halfer
  • 18,701
  • 13
  • 79
  • 158
Cooper
  • 36,005
  • 6
  • 17
  • 42