Home > Blockchain >  How to stop foreach loop in Google apps script
How to stop foreach loop in Google apps script

Time:10-08

I need to generate a docs file from an RSS URL.

https://cdn.feedcontrol.net/1830/2857-8L5Ntr0N5l5Xf.xml

I have been helped by Tanaike to accomplish that in this article.

function myFunction1() {
  const url = "https://cdn.feedcontrol.net/1830/2857-8L5Ntr0N5l5Xf.xml";
  const data = UrlFetchApp.fetch(url).getContentText();
  const root = XmlService.parse(data).getRootElement();
  const ns1 = root.getNamespace();
  const ns2 = XmlService.getNamespace("http://purl.org/rss/1.0/modules/content/");
  root.getChild("channel", ns1).getChildren("item", ns1).forEach(e => {
    Drive.Files.insert({title: e.getChild("title", ns1).getValue(), mimeType: MimeType.GOOGLE_DOCS}, HtmlService.createHtmlOutput(e.getChild("encoded", ns2).getValue()).getBlob());
  });
}

However, when retrieving all 10 items in RSS, it will be duplicated, because RSS only creates 5 new items. So I want to stop after creating the DOCs file on the first items.

Please help me!

CodePudding user response:

const items = root.getChild("channel", ns1).getChildren("item", ns1);
for (let i = 0; i < 5; i  ) {
  const e = items[i];
  Drive.Files.insert({title: e.getChild("title", ns1).getValue(), "parents": [{'id':folderlocid}], mimeType: MimeType.GOOGLE_DOCS}, HtmlService.createHtmlOutput(e.getChild("encoded", ns2).getValue()).getBlob());
}
  • Related