循环浏览Google Sheet数据,而不是在一行中使用App Script

时间:2018-03-27 13:18:13

标签: javascript google-apps-script google-sheets spreadsheet

我正在使用IMPORTFEED将CNN新闻Feed推送到Google表格中。当文章与今天的日期匹配时,它会发送一封电子邮件。完美。

但是,我需要在多行而不是一行上执行此操作。这将涉及一些" for循环"也许我不确定。

以下是Google App脚本

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);
    }

}

Google表格在此处,可以使用"制作副本" https://docs.google.com/spreadsheets/d/1TF2zdA3DKnFd9YjRbfpyB6GnJwvFr1bhNKLVrd23zpc/edit?usp=sharing

理想情况下,基于具有循环的App脚本的Feed。我应该从该新闻报道列表中收到2封电子邮件。而不是一个。

0 个答案:

没有答案