使用Google应用程序脚本解析XML文件(存储在GoogleDrive中) [英] Parse XML file (which is stored on GoogleDrive) with Google app script

查看:101
本文介绍了使用Google应用程序脚本解析XML文件(存储在GoogleDrive中)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在GoogleDrive上存储了一些XML文件.我想使用Google Apps脚本将数据从XML文件传输到Google电子表格.

I have a few XML files stored on GoogleDrive. I would like to transfer data from XML file to Google spreadsheet with google apps script.

是否可以使用Google Apps脚本解析XML文件(存储在GoogleDrive中)?

Is it possible parse XML file (which is stored on GoogleDrive) with Google apps script?

推荐答案

首先,您必须了解如何解析XML数据,以及如何使用apps脚本获取文件.不幸的是,我们无法直接在Google驱动器中获取xml文件.它必须位于Google Drive外部或永恒的网站中. 在使用应用程序脚本解析xml时,请参考站点:

First you must understand how to parse XML data, to get file using apps script. Unfortunately we can't directly get xml files in the google drive. It must be located outside google drive or in an eternal website. Refer to this site in using apps script parsing xml:

// Log the title and labels for the first page of blog posts on the Google Apps Developer blog.
function parseXml() {
  var url = 'http://googleappsdeveloper.blogspot.com/atom.xml';
  var xml = UrlFetchApp.fetch(url).getContentText();
  var document = XmlService.parse(xml);
  var root = document.getRootElement();
  var atom = XmlService.getNamespace('http://www.w3.org/2005/Atom');

  var entries = document.getRootElement().getChildren('entry', atom);
  for (var i = 0; i < entries.length; i++) {
    var title = entries[i].getChild('title', atom).getText();
    var categoryElements = entries[i].getChildren('category', atom);
    var labels = [];
    for (var j = 0; j < categoryElements.length; j++) {
      labels.push(categoryElements[j].getAttribute('term').getValue());
    }
    Logger.log('%s (%s)', title, labels.join(', '));
  }
}

然后将值转发到将创建电子表格的函数.这是另一个有用的教程.

Then forward the values to the function that will create a spreadsheet. Here is another useful tutorial.

这篇关于使用Google应用程序脚本解析XML文件(存储在GoogleDrive中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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