0

我正在尝试使用 Google 电子表格中的 Google Apps 脚本解析以下 XML 文档。特别是我想解析lineItems其属性的值和值coaCode,例如 RTLR = 50211.000000:

<ReportFinancialStatements Major="1" Minor="0" Revision="1">
      <FinancialStatements>
      <COAMap></COAMap>
         <AnnualPeriods>
         <FiscalPeriod Type="Annual" EndDate="2009-06-30" FiscalYear="2009">
            <Statement Type="INC">
                <FPHeader>
                    <PeriodLength>12</PeriodLength>
                    <Source Date="2009-09-23">ARS</Source>
                </FPHeader>
                <lineItem coaCode="SREV">50211.000000</lineItem>
                <lineItem coaCode="RTLR">50211.000000</lineItem>
                <lineItem coaCode="SCOR">12001.000000</lineItem>
            </Statement>
        </FiscalPeriod>

这是我的代码片段:

function parse(txt) {

  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheets()[0];

  var document = XmlService.parse(txt);
  var root = document.getRootElement();
  var elements = root.getChild('FinancialStatements').
         getChild('AnnualPeriods').getChildren('FiscalPeriod');  

  var columnOffset = 2;
  var rowOffset = 5;
  for (var i = 0; i < elements.length; i++) {
    var column = elements.length - i + columnOffset;
    var endDate = elements[i].getAttribute('EndDate').getValue();
    var fiscalYear = elements[i].getAttribute('FiscalYear').getValue();
    var fiscalPeriodType = elements[i].getAttribute('Type').getValue();

    // ***** SCRIPT CRASHES HERE: *****
    var values = elements[i].getChildren('Statement').getChildren('lineItem');

   for (var j = 0; j < values.length; j++) {
     var row = j + rowOffset;
     var value = values[j].getValue();
     sheet.getRange(row,column).setValue(value);
   }
  }
}

当我运行代码时,它在该行崩溃

var values = elements[i].getChildren('Statement').getChildren('lineItem');

带有错误消息:

TypeError: Cannot find function getChildren in object [Element: <Statement/>],
   [Element: <Statement/>],[Element: <Statement/>].

我试图找出错误几个小时,但没有成功(我尝试了不同的.getChildren组合变体)。

您对我的代码可能有什么问题有任何想法吗?

4

2 回答 2

2

对于这样的 XML:

<ReportFinancialStatements Major="1" Minor="0" Revision="1">
  <FinancialStatements>
    <COAMap />
    <AnnualPeriods>
      <FiscalPeriod Type="Annual" EndDate="2009-06-30" FiscalYear="2009">
        <Statement Type="INC">
          <FPHeader>
            <PeriodLength>12</PeriodLength>
            <Source Date="2009-09-23">ARS</Source>
          </FPHeader>
          <lineItem coaCode="SREV">50211.000000</lineItem>
          <lineItem coaCode="RTLR">50211.000000</lineItem>
          <lineItem coaCode="SCOR">12001.000000</lineItem>
        </Statement>
      </FiscalPeriod>
    </AnnualPeriods>
  </FinancialStatements>
</ReportFinancialStatements>

改变:

...
//var values = elements[i].getChildren('Statement').getChildren('lineItem');
var values = elements[i].getChildren('Statement')[0].getChildren('lineItem');
...
于 2013-11-08T22:08:37.410 回答
0

或者,您可以进行以下更改:

//var values = elements[i].getChildren('Statement').getChildren('lineItem');
 var values = elements[i].getChild('Statement').getChild('lineItem');

当数据出现在第一个“语句”节点中时,这和@wchiquito 提到的代码更改将起作用。

于 2016-08-29T15:56:48.883 回答