如何在Google应用程序脚本中停止Foreach循环 [英] How to stop foreach loop in Google apps script

查看:17
本文介绍了如何在Google应用程序脚本中停止Foreach循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从RSS URL生成文档文件。

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

我在this article中得到了Tanaike的帮助。

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());
  });
}
但是,当检索RSS中的所有10个项目时,它将被复制,因为RSS仅创建5个新项目。 因此,我想在创建第一个项目的文档文件后停止。

请帮帮我!

推荐答案

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

这篇关于如何在Google应用程序脚本中停止Foreach循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆