Home > front end >  How can two cells with different var work?
How can two cells with different var work?

Time:09-27

Sorry for maybe stupid question ,but i'm a begginer in this and I'm trying to figure it out by myself :D So this is my function:

function CheckSales() {
// Fetch the monthly sales
var monthSalesRange = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("non inventory").getRange("C28");
var monthSales = monthSalesRange.getValue();
// Check totals sales
if (monthSales < 200 ){
// Fetch the email address
var emailRange = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("data").getRange("L5");
var emailAddress = "[email protected]";
// Send Alert Email.
var message = "Przygotuj zapotrzebowanie! Inwentarz spadł do wartości "   monthSales; // Second column
var subject = "Low Inventory Alert";
MailApp.sendEmail(emailAddress, subject, message);
}
}

and everything is as i want it, but what should i do if i want the same function, but different var, for example:

That's the part that i already have, but what should i do if i want to add for cell C29 if monthSales <250. And then both of them working at the same time?

var monthSalesRange = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("non inventory").getRange("C28");
var monthSales = monthSalesRange.getValue();
// Check totals sales
if (monthSales < 200 )

CodePudding user response:

I believe what Damian wants is a function where you can change the range reference and value to check against. I would do:

function checkSales(rangeRef,compareTo) {
  // Fetch the monthly sales
  var monthSalesRange = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("non inventory").getRange(rangeRef);
  var monthSales = monthSalesRange.getValue();
  // Check totals sales
  if (monthSales < compareTo ){
    // Fetch the email address
    var emailRange = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("data").getRange("L5");
    var emailAddress = "[email protected]";
    // Send Alert Email.
    var message = "Przygotuj zapotrzebowanie! Inwentarz spadł do wartości "   monthSales; // Second column
    var subject = "Low Inventory Alert";
    MailApp.sendEmail(emailAddress, subject, message);
  }
}

And then you can call the function twice with different arguments from the function that you actually run:

function checkAllSales(){
  checkSales("C28",200);
  checkSales("C29",250);
}
  • Related