0

I am using IMPORTFEED to push CNN news feed into Google Sheets. When an article matches todays date, it sends an email. Perfect.

However, I need to do this on multiple rows rather than just one. Which will involve some "for loops" perhaps which I am unsure about.

Here is the Google App Script

function sendEmails() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1");  // To only handle the "IMPORT & LOGIC" sheet
// Get values from formulas in Row 2
var rsstitle = sheet.getRange('A4')
var rsslink = sheet.getRange('B4')
var rssdate = sheet.getRange('C4')
var feeddesc = sheet.getRange('D4')
var convdate = sheet.getRange('E4')
var todaysdate = sheet.getRange('F2') // STATIC CELL
var myemail = sheet.getRange('G2') // STATIC CELL
// Pull formula values from Row 2
var title = rsstitle.getValue();
var link = rsslink.getValue();
var rsdate = rssdate.getValue();
var desc = feeddesc.getValue();
var feeddate = convdate.getValue();
var today = todaysdate.getValue();
var emailcell = myemail.getValue();

var sourceRange = sheet.getRange("A2:E21");
var sourceData = sourceRange.getValues();

    for (row in sourceData) {
        // If todays date and feed date do not match, go to next row 
        if (feeddate.valueOf() != today.valueOf()) { // A bit tricky matching dates using the above but this seems to work
            return;
        }
        var emailAddress = emailcell; 
        // Produce and output email title such as "Example page has chaged"
        var subject = title;
        var message = desc;
        MailApp.sendEmail(emailAddress, subject, message);
    }

}

The Google Sheet is here and can be copied using "Make a copy" https://docs.google.com/spreadsheets/d/1TF2zdA3DKnFd9YjRbfpyB6GnJwvFr1bhNKLVrd23zpc/edit?usp=sharing

So ideally, based on that feed with a App Script that loops. I should get 2 emails from that list of news stories. Rather than one.

HavanaG
  • 1
  • 2
  • Yes, you need `for` loops, or some other way of evaluating the news' dates in multiple rows. Please make an attempt to modify your code to do your desired task. Also, rather than link to a sheet just to say "I should get two results," provide a data mockup in your question, so that the context is readily available. – tehhowch Mar 27 '18 at 13:25
  • For loop attempt added, executes with no errors. But no emails :-( – HavanaG Mar 27 '18 at 13:56
  • Note: https://stackoverflow.com/questions/500504/why-is-using-for-in-with-array-iteration-a-bad-idea?s=6|231.0336 Also note, that `return` means "stop executing **everything**" - you probably want `continue;` https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Building_blocks/Looping_code – tehhowch Mar 27 '18 at 14:06

0 Answers0