読者です 読者をやめる 読者になる 読者になる

Google Apps ScriptでRSS1.0・RSS2.0・ATOMを取得

Google Apps Script RSS
function getRSS10(feedURL) {

  if (feedURL) {

    var sheet = SpreadsheetApp.getActiveSheet();

    var row = sheet.getLastRow() + 1;

    var xml = UrlFetchApp.fetch(feedURL).getContentText();
    var document = XmlService.parse(xml);
    var root = document.getRootElement();
    var rss = XmlService.getNamespace('http://purl.org/rss/1.0/');
    var dc = XmlService.getNamespace('dc', 'http://purl.org/dc/elements/1.1/');

    var items = root.getChildren('item', rss);

    for (var i in items) {

      var title = items[i].getChild('title', rss).getText();
      var link = items[i].getChild('link', rss).getText();
      var pubdate = items[i].getChild('date', dc).getText();

      sheet.getRange(row, 1).setValue(title);
      sheet.getRange(row, 2).setValue(link);
      sheet.getRange(row, 3).setValue(pubdate);

      row++;
    }
  }
}

function getRSS20(feedURL) {

  if (feedURL) {

    var sheet = SpreadsheetApp.getActiveSheet();

    var row = sheet.getLastRow() + 1;

    var xml = UrlFetchApp.fetch(feedURL).getContentText();
    var document = XmlService.parse(xml);
    var root = document.getRootElement();

    var items = root.getChild('channel').getChildren('item');

    for (var i in items) {
      var title = items[i].getChild('title').getText();
      var link = items[i].getChild('link').getText();
      var pubdate = Utilities.formatDate(new Date(items[i].getChild('pubDate').getText()), 'JST', "yyyy-MM-dd'T'HH:mm:ssXXX");

      sheet.getRange(row, 1).setValue(title);
      sheet.getRange(row, 2).setValue(link);
      sheet.getRange(row, 3).setValue(pubdate);

      row++;
    }
  }
}

function getATOM(feedURL) {

  if (feedURL) {

    var sheet = SpreadsheetApp.getActiveSheet();

    var row = sheet.getLastRow() + 1;

    var xml = UrlFetchApp.fetch(feedURL).getContentText();
    var document = XmlService.parse(xml);
    var root = document.getRootElement();
    var atom = XmlService.getNamespace('http://www.w3.org/2005/Atom');

    var entries = root.getChildren('entry', atom);

    for (var i in entries) {
      var title = entries[i].getChild('title', atom).getText();
      var link = entries[i].getChild('link', atom).getAttribute('href').getValue();
      var updated = entries[i].getChild('updated', atom).getText();

      sheet.getRange(row, 1).setValue(title);
      sheet.getRange(row, 2).setValue(link);
      sheet.getRange(row, 3).setValue(updated);

      row++;
    }
  }
}

function readerRSS() {
  var sheet = SpreadsheetApp.getActiveSheet();
  sheet.clear();

  var rssLists = [
    ['RSS1', 'アドレス'],
    ['RSS2', 'アドレス'],
    ['ATOM', 'アドレス']

  ];

  for (var i in rssLists) {
    switch (rssLists[i][0]) {
      case 'RSS1':
        getRSS10(rssLists[i][1]);
        break;

      case 'RSS2':
        getRSS20(rssLists[i][1]);
        break;

      case 'ATOM':
        getATOM(rssLists[i][1]);
        break;
    }
  }
  sheet.sort(3, false);
}

imabari.hateblo.jp