首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >RSS新闻源收集到谷歌单张问题

RSS新闻源收集到谷歌单张问题
EN

Web Applications用户
提问于 2021-08-03 23:05:55
回答 1查看 196关注 0票数 0

所以我从之前的一篇文章中得到了一个很好的指导,但是有一些缺陷使得它停止了工作。

这是最初的目标:创建一个google页面,更新rss链接并收集标题、网址和时间。然后,我希望工作表在更新时保留以前的一组信息。

下面我尝试使用这个方法,但是它停止工作了,而且第二个页面也没有收集url。我试着添加这个,看看它是否会复制,但是没有运气。因此,如果有人有一个很好的方法来更新和保存RSS源系统,我想知道如何做到这一点。

代码语言:javascript
运行
复制
function rssfeed() {
  var ss = SpreadsheetApp.getActive();
  ss.getRange('Import RSS!A1').setFormula('=IMPORTFEED("https://retail-insider.com/feed/","items Title",False,100)');
  ss.getRange('Import RSS!B1:B').copyTo(ss.getRange('History RSS!A1:A'), SpreadsheetApp.CopyPasteType.PASTE_VALUES, false);
  ss.getRange('Import RSS!A1').clear({contentsOnly: true, skipFilteredRows: true}); 
  ss.getRange('Import RSS!B1').setFormula('=IMPORTFEED("https://retail-insider.com/feed/","items URL",False,100)');
  ss.getRange('Import RSS!B1:B').copyTo(ss.getRange('History RSS!A1:A'), SpreadsheetApp.CopyPasteType.PASTE_VALUES, false);
  ss.getRange('Import RSS!B1').clear({contentsOnly: true, skipFilteredRows: true}); 
}
}
EN

回答 1

Web Applications用户

回答已采纳

发布于 2021-08-09 05:08:10

考虑一下现有代码上的这种变化。

剧本分为三部分:

  • buildrssfeed():类似于您现有的脚本;从目标站点创建提要并保存到“导入RSS”
  • copyrssfeed():只将提要数据的所需列从“导入RSS”复制到“历史RSS”
  • removeduplicates():删除“历史RSS”中的重复条目

这些脚本是作为单独的单元编写的,以演示每个阶段所涉及的过程。不过,他们可以合并或雏菊链,如果愿意的话。removeduplicates()也可以根据需要从菜单中运行。

现有方法的缺点之一是,rssfeed可以/将返回现有数据和新数据。当它被复制到“历史RSS”时,可能会有许多重复的条目。为了解决这个问题,removeduplicates从“历史RSS”中删除重复的内容。

处理序列/触发器

  • buildrssfeed()的时间驱动触发器已经被提出了。
代码语言:javascript
运行
复制
/**
 * insert `IMPORTFEED` formula and update feed data.
 */
function buildrssfeed() {
  var ss = SpreadsheetApp.getActiveSpreadsheet()
  
  // build the sheet names
  var importname = "Import RSS1";
  var importsheet = ss.getSheetByName(importname);
  
  // clear any existing data
  importsheet.clear();

  // insert the IMPORTFEED formula
  importsheet.getRange("A1").setFormula('=IMPORTFEED("https://retail-insider.com/feed/","items",True,100)');

  // update the spreadsheet
  SpreadsheetApp.flush();

  copyrssfeed()
  removeDuplicates();

}


/**
 * copy feed data to History
 */
function copyrssfeed() {
  var ss = SpreadsheetApp.getActiveSpreadsheet()

  // build the sheet names
  var importname = "Import RSS1";
  var importsheet = ss.getSheetByName(importname);
  var historyname = "History RSS1";
  var historysheet = ss.getSheetByName(historyname);

  // get last row information 
  var importLR = importsheet.getLastRow();
  var historyLR = historysheet.getLastRow();
  
  // get the RSS data from import RSS
  var importdata = importsheet.getDataRange().getValues();

  // create an array to collect the data
  var historydata = [];

  for (var i = 0;i<importLR-1;i++){
    var newImportRow=[]; // temporary array

    // push the Title, URL and Date created onto a temporary array
    newImportRow.push(importdata[i+1][0],importdata[i+1][2],importdata[i+1][3])

    // push the results of this row onto the history data array
    historydata.push(newImportRow)
  
  }

  // update the feed data to the Hisry RSS sheet
  historysheet.getRange(historyLR + 1, 1, historydata.length,3).setValues(historydata);

  SpreadsheetApp.flush();

  return;
}

/**
 * Removes duplicate rows from the current sheet.
 * Sourced from Google Apps tutorial
 * https://developers.google.com/apps-script/articles/removing_duplicates
 */
function removeDuplicates() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();

  // build the sheet names
  var historyname = "History RSS1";
  var historysheet = ss.getSheetByName(historyname);

  // get the data
  var data = historysheet.getDataRange().getValues();

  // create an array to hold unique rows
  var newData = [];

  // loop through the data testing for duplicate values
  for (var i in data) {
    var row = data[i];
    var duplicate = false;
    for (var j in newData) {
      if (row.join() == newData[j].join()) {
        duplicate = true;
      }
    }
    if (!duplicate) {
      newData.push(row);
    }
  }

  // clear the entire History RSS sheet
  historysheet.clearContents();

  // paste the date from the array
  historysheet.getRange(1, 1, newData.length, newData[0].length).setValues(newData);

  SpreadsheetApp.flush();
}
票数 0
EN
页面原文内容由Web Applications提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://webapps.stackexchange.com/questions/158209

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档