Home > database >  How do I get an alert prompt when an "if" statement returns no 'finds' in Google
How do I get an alert prompt when an "if" statement returns no 'finds' in Google

Time:06-01

//Invoice find and transfer to Warehouse Sheet
function searchInvoiceWhSh() {
var ss = SpreadsheetApp.getActiveSpreadsheet()
var shUserForm = ss.getSheetByName("Warehouse Form")
var shSalesSheet = ss.getSheetByName("Sales")
var sValue = shUserForm.getRange("G5").getValue();
var sData = shSalesSheet.getDataRange().getValues();
var currentRow = 9

for (var i=0; i<sData.length; i  ) {
  var row = sData[i];
  if (row[0] == sValue) { //do something}

  currentRow  = 2
}}

I've used this to search for an "Invoice number" from the "Sales" worksheet and when found to transfer the data back to the user form.

If, for example, the invoice number is typed incorrectly into the "sValue" cell, then no data will be transferred.

How do I code a prompt message to ask the user to check the invoice number as no records were found?

CodePudding user response:

Try:

function searchInvoiceWhSh() {

  const ui = SpreadsheetApp.getUi()
  const ss = SpreadsheetApp.getActiveSpreadsheet()

  const shUserForm = ss.getSheetByName("Warehouse Form")
  const shSalesSheet = ss.getSheetByName("Sales")

  const sValue = shUserForm.getRange("G5").getValue()
  const sData = shSalesSheet.getDataRange().getValues()

  const hasValue = sData.find(row => row[0] === sValue)

  if (hasValue) {
    // Value found
    const rowData = [...hasValue]

  } else {

    ui.alert(`No match found.`)

  }

}

This will search for the sValue provided as in your code, but will store the row in a variable once found. If it's not found, it will create an alert pop-up with your specified message.

Alternatively, you can check out UI Class for other pop-up options.

CodePudding user response:

Try it like this:

function myfunk() {
  var ss = SpreadsheetApp.getActive()
  var fsh = ss.getSheetByName("Warehouse Form")
  var ssh = ss.getSheetByName("Sales")
  var fv = fsh.getRange("G5").getValue();
  var svs = ssh.getDataRange().getValues();
  let m = 0;
  svs.forEach((r, i) => {
    if (r[0] == fv) {
      m  ;
    }
    SpreadsheetApp.getUi().alert(`${m} matches found`)
  });
}

Always provides a result

  • Related