Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google apps script 无法使用google apps脚本分析SOAP XML响应_Google Apps Script_Xml Parsing - Fatal编程技术网

Google apps script 无法使用google apps脚本分析SOAP XML响应

Google apps script 无法使用google apps脚本分析SOAP XML响应,google-apps-script,xml-parsing,Google Apps Script,Xml Parsing,我正在使用GoogleApps脚本调用SOAPXMLWeb服务,目的是将响应内容插入到GoogleSheet中。使请求返回具有有效数据值的预期响应,但是我在解析响应时遇到困难。下面是我的功能 function testFetch() { var response = UrlFetchApp.fetch(setScadaHost(), setOptions()); var doc = XmlService.parse(response.getContentText()); var ns

我正在使用GoogleApps脚本调用SOAPXMLWeb服务,目的是将响应内容插入到GoogleSheet中。使请求返回具有有效数据值的预期响应,但是我在解析响应时遇到困难。下面是我的功能

function testFetch() {
  var response = UrlFetchApp.fetch(setScadaHost(), setOptions());
  var doc = XmlService.parse(response.getContentText());
  var ns = XmlService.getNamespace(setNsScada());
  var root = doc.getRootElement().getChild('scada-response', ns);
  var entries = [];
  for(var i in root) {
    var id = root[i].getAttribute('node-id').getValue();
    var td = root[i].getAttribute('trading-date').getValue();
    var tp = root[i].getAttribute('trading-period').getValue();
    var mw = root[i].getAttribute('generation').getValue();
    entries.push(id, new Date(td), tp, mw);
  }
  shtSoap.getRange(shtSoap.getLastRow()+1,1,entries.length, 4).setValues(entries);
}
shtSoap在项目的其他地方定义,用于标识目标工作表。我返回的错误消息是异常:范围中的行数必须至少为1,并突出显示.setValues行

如果我输入Logger.logresponse;我得到的XML响应结构如下:

<?xml version="1.0" encoding="utf-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <soapenv:Body>
        <scada-response response-type="Scada Service" xmlns="[domainhost]/response/scada">
            <node node-id="1st node"><trading-date value="2020-09-21"><trading-period value="32"><time-stamp value="16:21:00.000Z"><generation>1000</generation></time-stamp></trading-period></trading-date></node>
            <node node-id="2nd node"><trading-date value="2020-09-21"><trading-period value="32"><time-stamp value="16:21:00.000Z"><generation>1200</generation></time-stamp></trading-period></trading-date></node>
            <node node-id="3rd node"><trading-date value="2020-09-21"><trading-period value="32"><time-stamp value="16:21:00.000Z"><generation>1200</generation></time-stamp></trading-period></trading-date></node>
            <node node-id="4th node"><trading-date value="2020-09-21"><trading-period value="32"><time-stamp value="16:21:00.000Z"><generation>800</generation></time-stamp></trading-period></trading-date></node>
        </scada-response>
    </soapenv:Body>
</soapenv:Envelope>
如果我登录Logger.log:

在for循环之后,我得到一个空数组。 doc在doc声明之后,我得到以下错误文档:没有DOCTYPE声明,根为[Element:] 我还尝试在根声明中将“scada响应”替换为“节点”,但得到了相同的结果


非常感谢您提供的任何帮助,以了解我的错误所在。

我相信您的目标如下

您希望使用Google Apps脚本从问题中的XML数据中检索节点id、交易日期、交易周期和生成的值。 您希望将检索到的值放入电子表格。 修改点: 在XML数据中, scada响应是soapenv:Body的子级。 节点是scada响应的子节点。 交易日期是节点的子节点。 交易期间是交易日期的子项。 时代是时间戳的产物。 在脚本中,var entries=[];是由entries.pushid、new Datetd、tp、mw组成的一维数组;消息灵通的 在这种情况下,我认为需要将id、new Datetd、tp、mw的值作为一维数组放入条目中。 当上述各点反映到脚本中时,它将变成如下所示

修改脚本: 确认上述脚本的脚本: 当您直接从XML数据测试上述脚本时,还可以使用以下示例脚本。在这种情况下,请将其复制并粘贴到脚本编辑器中,然后运行该函数

函数myFunction{ var响应=` 1000 1200 1200 800 `; var doc=XmlService.parseresponse; var ns1=XmlService.getNamespace'soapenv','http://schemas.xmlsoap.org/soap/envelope/'; var ns2=XmlService.getNamespace'[domainhost]/response/scada'; var nodeIds=doc.getRootElement.getChild'Body',ns1.getChild'scada-response',ns2.getChildren; var entries=nodeIds.mapc=>{ var tradingDate=c.getChild'trading-date',ns2; var tradingPeriod=tradingDate.getChild'trading-period',ns2; var id=c.getAttribute'node-id'.getValue; var td=tradingDate.getAttribute'value'.getValue; var tp=tradingPeriod.getAttribute'value'.getValue; var mw=tradingPeriod.getChild'time-stamp',ns2.getChild'generation',ns2.getValue; 返回[id,新日期TD,tp,mw]; }; console.logentries
} 我相信你的目标如下

您希望使用Google Apps脚本从问题中的XML数据中检索节点id、交易日期、交易周期和生成的值。 您希望将检索到的值放入电子表格。 修改点: 在XML数据中, scada响应是soapenv:Body的子级。 节点是scada响应的子节点。 交易日期是节点的子节点。 交易期间是交易日期的子项。 时代是时间戳的产物。 在脚本中,var entries=[];是由entries.pushid、new Datetd、tp、mw组成的一维数组;消息灵通的 在这种情况下,我认为需要将id、new Datetd、tp、mw的值作为一维数组放入条目中。 当上述各点反映到脚本中时,它将变成如下所示

修改脚本: 确认上述脚本的脚本: 当您直接从XML数据测试上述脚本时,还可以使用以下示例脚本。在这种情况下,请将其复制并粘贴到脚本编辑器中,然后运行该函数

函数myFunction{ var响应=` 1000 1200 1200 800 `; var doc=XmlService.parseresponse; var ns1=XmlService.getNamespace'soapenv','http://schemas.xmlsoap.org/soap/envelope/'; var ns2=XmlService.getNamespace'[domainhost]/response/scada'; var nodeIds=doc.getRootElement.getChild'Body',ns1.getChild'scada-response',ns2.getChildren; var entries=nodeIds.mapc=>{ var tradingDate=c.getChild'trading-date',ns2; var tradingPeriod=tradingDate.getChild'trading-period',ns2; var id=c.getAttribute'node-id'.getValue; var td=tradingDate.getAttribute'value'.getValue; var tp=tradingPeriod.getAttribute'value'.getValue; var mw=tradingPeriod.getChild'time-stamp',ns2.getChild'generation',ns2.getValue; 返回[id,新日期TD,tp,mw]; }; console.logentries }
function testFetch() {
  var response = UrlFetchApp.fetch(setScadaHost(), setOptions());
  var doc = XmlService.parse(response.getContentText());
  
  // --- I modified below script.
  var ns1 = XmlService.getNamespace('soapenv', 'http://schemas.xmlsoap.org/soap/envelope/');
  var ns2 = XmlService.getNamespace('[domainhost]/response/scada');
  var nodeIds = doc.getRootElement().getChild('Body', ns1).getChild('scada-response', ns2).getChildren();
  var entries = nodeIds.map(c => {
    var tradingDate = c.getChild('trading-date', ns2);
    var tradingPeriod = tradingDate.getChild('trading-period', ns2);
    var id = c.getAttribute('node-id').getValue();
    var td = tradingDate.getAttribute('value').getValue();
    var tp = tradingPeriod.getAttribute('value').getValue();
    var mw = tradingPeriod.getChild('time-stamp', ns2).getChild('generation', ns2).getValue();
    return [id, new Date(td), tp, mw];
  });
  // ---
  
  shtSoap.getRange(shtSoap.getLastRow()+1,1,entries.length, 4).setValues(entries);
}