Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

104
Visualizações
compare two values of cell then send email
function myFunction() {

  const minimumLevel = [0,5,10,15,20,25,30];
  var sheet = SpreadsheetApp.getActive().getSheetByName("Sheet1");
  var data = sheet.getRange(2,1,data.getLastRow() - 1, data.getLastColumn()).getValues();
  data.forEach((r,i) => {
      if(r[9] < minimumLevel[1]){
        console.log("true");

        // Fetch the email address
       var emailRange = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1").getRange("B2");
       var emailAddress = emailRange.getValues();
       // Send Alert Email.
       var message = 'This is your Alert email!'; // Second column
       var subject = 'Your Google Spreadsheet Alert';
        MailApp.sendEmail(emailAddress, subject, message);




      }
  })

}

I am trying to compare to values of cell and then if the value of that cell goes below minimum level it will send an email to someone, however I think i am stuck? please can someone help?

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Try it this way:

function myFunction() {   
  const minimumLevel = [0,5,10,15,20,25,30];
  var sheet = SpreadsheetApp.getActive().getSheetByName("Sheet1");
  var data = sheet.getRange(2,1,sheet.getLastRow() - 1, sheet.getLastColumn()).getValues();
  data.forEach((r,i) => {
      if(r[9] < minimumLevel[1]){
        console.log("true");
        //send email code here
      }
  });
}

cell to the left is the current stock level and the right is minimum level, the main condition that needs to be met is if the current stock level is less that minimum level, it will send an email to someone.. i feel like I am overthinking everything and now I cant figure it out, help would really be apprciated

about 4 years ago · Juan Pablo Isaza Relatório

0

Based on your new comments I understand that you have two columns, one for the actual stock price and another for the desired minimum level. I also understand that your goal is to send an alert email when the stock price descends below the alert price. If my understanding is accurate, then you can reach your goals easily by making small modifications on your script.

The modified script below will first extract the data from the stock price and alert level columns. I am assuming that the stock price is on Column A and the alerts at Column B, but you can modify that easily. Then the script will run a for loop and if the stock price is lower than the alert level, then an email will be sent.

function myFunction() {
  var sheet = SpreadsheetApp.getActive().getSheetByName("Sheet1");
  var stockPrice = sheet.getRange(2, 1, sheet.getLastRow() - 1, 1).getValues()
    .flat(2);
  var minimumLevel = sheet.getRange(2, 2, sheet.getLastRow() - 1, 1).getValues()
    .flat(2);
  var emailAddress = sheet.getRange("B2").getValue();
  var emailMessage = "{ ALERT MESSAGE }";
  var emailsubject = "{ EMAIL SUBJECT }";
  for (let i = 0; i < stockPrice.length; i++) {
    if (stockPrice[i] < minimumLevel[i]) {
      MailApp.sendEmail(emailAddress, emailsubject, emailMessage);
    }
  }
}

Please test this approach and let me know if you find any difficulties.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda