Google-sheets – Google Spreadsheet Alert Scripts

google sheetsgoogle-apps-script

Wanting to add an alert so if the information entered here if not all filled in as required it send you an alert.

function Repair1() {
  var ss        = SpreadsheetApp.getActiveSpreadsheet();
  var r         = ss.getRange("Repair Input!C3:C12");
  var sheet     = ss.getSheetByName("Repair Input"); //Form Sheet
  var datasheet = ss.getSheetByName("Repair Jobs"); //Data Sheet

  var date =          sheet.getRange("C1").getValue();
  var mechName =      sheet.getRange("C3").getValue();
  var customerName =  sheet.getRange("c4").getValue();
  var vehicleName =   sheet.getRange("c5").getValue();
  var group =         sheet.getRange("c6").getValue();
  var jobType =       sheet.getRange("c7").getValue();
  var invoice =       sheet.getRange("c8").getValue();
  var bProfit =       sheet.getRange("c9").getValue();
  var eProfit =       sheet.getRange("c10").getValue();
  var comment =       sheet.getRange("c11").getValue();
  var paid =          sheet.getRange("c12").getValue();
  
  if(mechName != "" && customerName != "" && vehicleName != "" && group != "" && jobType != "" && paid !="") 

//If data is empty here if just wont send but I want it to create an error telling the user there missing data.... But adding .getUi().alert('#ERROR...Please fill out the report completely!'); There causes it to send data that is not all filled in or empty cells breaking the code
   {

  //save dat shit here.
  var newRow = datasheet.appendRow([date, mechName, customerName,vehicleName,group,jobType,invoice,bProfit,eProfit,paid, comment]);

  //Input clear
  var clear = [[ sheet.getRange("C3").clearContent(),
                 sheet.getRange("C4").clearContent(),
                 sheet.getRange("C5").clearContent(),
                 sheet.getRange("C6").clearContent(),
                 sheet.getRange("C7").clearContent(),
                 sheet.getRange("C11").clearContent(),
                 sheet.getRange("C12").clearContent()]];

}
}

Have tried adding something like this but am lost..

SpreadsheetApp.getUi().alert('#ERROR...Please fill out the report completely!');

Best Answer

There are many ways to do this. One of them is to use Array.prototype.some(). I.E. instead of

if(mechName != "" && customerName != "" && vehicleName != "" && group != "" && jobType != "" && paid !="") 
 //If data is empty here if just wont send but I want it to create an error telling the user there missing data.... But adding .getUi().alert('#ERROR...Please fill out the report completely!'); There causes it to send data that is not all filled in or empty cells breaking the code
 {
 ...
 }

use

if( [mechName, customerName, vehicleName, group, obType , paid].some(value ==> value !== '') {
  ...
} else {
  
 SpreadsheetApp.getUi().alert('#ERROR...Please fill out the report completely!');

}

Resources