简体   繁体   English

使用Google Apps脚本解析XML元素中的所有值?

[英]Parse all values from a XML element using Google Apps Script?

I am trying to parse forex values (all of them) for http://indicador.eof.cl/rss XML feed into a Gooogle Sites trough Google Apps Script. 我正在尝试通过Google Apps脚本将http://indicador.eof.cl/rss XML提要的外汇值(全部)解析为Gooogle站点。

The script as follow> 脚本如下>

function doGet(){

  var response = UrlFetchApp.fetch("http://indicador.eof.cl/rss").getContentText();
  var parsedResponse = Xml.parse(response, false);
  var root = parsedResponse.getElement();
  var entries = root.getElement('channel').getElements("item");

  for (var i=0; i<entries.length; i++) {
      var e = entries[i];
      var title = e.getElement("title").getText();
      var description = e.getElement("description").getText();
  }

  var app = UiApp.createApplication();
  var TopVar = app.createHorizontalPanel();

  TopVar.add(app.createLabel(title).setStyleAttribute("fontSize","12px"));  
  TopVar.add(app.createLabel(description).setStyleAttribute("fontSize","12px"));

  app.add(TopVar);
  return app;
}

The issue is the code just bring me the first value no all of them, what i am forgetting? 问题是代码只是给我带来了第一个价值,而没有一个,我忘记了什么?

Best Regards, 最好的祝福,

Try to move TopVar.add(...); 尝试移动TopVar.add(...); lines inside for loop : for循环内的行:

var app = UiApp.createApplication();
var TopVar = app.createHorizontalPanel();

for (var i=0; i<entries.length; i++) {
  var e = entries[i];
  var title = e.getElement("title").getText();
  var description = e.getElement("description").getText();
  TopVar.add(app.createLabel(title).setStyleAttribute("fontSize","12px"));  
  TopVar.add(app.createLabel(description).setStyleAttribute("fontSize","12px"));
}

Actually, I know nothing about google-apps-script. 实际上,我对google-apps-script一无所知。 But your current code logic seems a bit off. 但是您当前的代码逻辑似乎有些偏离。 It doesn't make use of values of local variables declare inside for loop ( e , title , and description ). 它不使用for循环内部声明的局部变量的值( etitledescription )。 Value of those variables changed in every iteration without any code using it. 这些变量的值在每次迭代中都会更改,而无需使用任何代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从 Google Apps 脚本中的 XML 解析中获取元素 ID - How can I get the Element Id from XML parse in Google Apps Script 使用Google App脚本解析HTML元素中的值? - Parse values from HTML element using Google App Script? 我如何使用 Google Apps 脚本解析 XML 并遍历所有元素 - Ho do I Parse XML using Google Apps Script and loop through all elements Google Apps脚本:直接访问XML元素 - Google Apps Script: Directly accessing a XML element <a>使用Apps脚本向Google文档</a>添加<a>元素</a> - add an <a> element to Google Document using Apps Script 如何使用Google Apps脚本的XmlService设置XML元素的名称空间? - How do you set the namespace of an XML element using Google Apps Script's XmlService? 使用 Google Apps 脚本从 Google Drive 中的多个上传元素上传本地文件 - Upload Local Files from multiple upload element in Google Drive using Google Apps Script 用于 Google Apps 脚本的 DocumentApp 未返回元素的所有属性? - DocumentApp for Google Apps Script not returning all attributes of an element? 如何在不使用 XmlService 的情况下解析 Google Apps 脚本中的 HTML 字符串? - How to parse an HTML string in Google Apps Script without using XmlService? 将Parse的JavaScript框架与Google Apps脚本一起使用 - Using Parse's JavaScript framework with Google Apps Script
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM