Implementation of GoogleSheets Stock Price Email Alerts

若如初见. 提交于 2021-01-28 05:20:45

问题


I have a Googlesheets Stock Price Alert Script Running for a single stock in a GoogleSheet. It checks the current price against a target price and sends an email alert if it is lower. How would I expand my script to check a list of stocks in the spreadsheet? I think I need a loop or something to scroll down each row but I'm unsure of how to do this. My working code for single row check:

 // Fetch the stock price
   var stockNameRange = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1").getRange("A2");  
   var currentPriceRange = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1").getRange("C2"); 
   var targetPriceRange = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1").getRange("B2");
   var stockName = stockNameRange.getValue();
   var currentPrice = currentPriceRange.getValue();
   var targetPrice = targetPriceRange.getValue();   

 // Check stock prices
   if (currentPrice < targetPrice){

// Fetch the email address
   var emailRange = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet2").getRange("B2");
   var emailAddress = emailRange.getValues();

// Send Alert Email.
   var message = stockName +" "+ currentPrice +" is below your target price of "+ targetPrice;
   var subject = 'Low Price Alert';
   MailApp.sendEmail(emailAddress, subject, message);
   }
 }```

回答1:


Try this:

function sdk(){
  // Fetch the stock price
  var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("sheet1");
  var data = ss.getRange("A13:C23").getValues(); // gets all the data from the three cols and puts it into a 2D array.
  var emailRange = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet2").getRange("B2");
  var emailAddress = emailRange.getValues();
  var message = "";

  for (var i = 0; i < data.length; i++){ // will go over the rows that you stored in data with data[i][0] being the stock name, data[i][1] being the target and data[i][2] being the current value.
    // Check stock prices
    if (data[i][2] < data[i][1]){
      // Send Alert Email.
      message += data[i][0] +" "+ data[i][2] +" is below your target price of "+ data[i][1] + " \n";
    }
  }
   if (message != "")
      MailApp.sendEmail(emailAddress, 'Low Price Alert', message);
}

Keep in mind that I used the range A13:C23 because that's where I had my test data, you will have to use yours depending on how you have structured your sheet, but this code will loop and get you the information you want into the email. it basically keeps appending rows of "alerts" into a message and sends everything together at the end of the loop.

When working with Apps Scripts, try to limit the calls to the APIs to make the code more optimized, this is why I changed the way you stored the ranges into a single call when getting the sheet in var ss = ..., this is also why using getValues() is important, it's only one call to the API instead of getting individual cells' values.



来源:https://stackoverflow.com/questions/57602064/implementation-of-googlesheets-stock-price-email-alerts

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!