谷歌表单xml有问题

google sheets trouble with xml

本文关键字:有问题 xml 表单 谷歌      更新时间:2023-09-26

我正试图从UPS中提取数据以跟踪到电子表格中。XML提要的signedforbyname部分有时存在,有时不存在。当它不存在时,我会遇到一个问题:TypeError:无法从未定义中读取属性"Text"。(第60行,文件"代码")如果我在第60行注释掉了正在寻找签名的名字,那么在我也从应该生成的字符串中提取变量之后,我就没事了。

我对javascript不是很熟悉,所以我可能只是在做一件愚蠢的语法事情,这将是很棒的。我完全赞成简单的答案。如果有人能用这个狙击坑给我指明正确的方向,我将不胜感激

我想,如果有if-then来检查值是否存在,这是可行的,但这似乎也不起作用,因为无论是否使用if/then,同一个错误都会发生在同一个地方(即使我把语法搞砸了)。

我遗漏的代码:

catch(err) {
if (jsonText.TrackResponse.Shipment.Package.Activity.ActivityLocation.SignedForByName.Text !==null){
  var signedBy = jsonText.TrackResponse.Shipment.Package.Activity.ActivityLocation.SignedForByName.Text;}
  else {var signedBy = "No Signature";}
var dropLocation = jsonText.TrackResponse.Shipment.Package.Activity.ActivityLocation.Description.Text;
var estDate = 'Signed By: '+signedBy+' @ '+dropLocation;

日志中的XML:

[15-03-28 07:52:43:381 CST] Xml.parse([<?xml version="1.0"?>
<TrackResponse><Response><ResponseStatusCode>1</ResponseStatusCode><ResponseStatusDescription>Success</ResponseStatusDescription></Response><Shipment><Shipper><ShipperNumber>xxxx</ShipperNumber><Address><AddressLine1>xxxxx</AddressLine1><City>TRACY</City><StateProvinceCode>CA</StateProvinceCode><PostalCode>95304   9370</PostalCode><CountryCode>US</CountryCode></Address></Shipper><ShipTo><Address><City>ROUNDUP</City><StateProvinceCode>MT</StateProvinceCode><PostalCode>59072</PostalCode><CountryCode>US</CountryCode></Address></ShipTo><ShipmentWeight><UnitOfMeasurement><Code>LBS</Code></UnitOfMeasurement><Weight>4.90</Weight></ShipmentWeight><Service><Code>003</Code><Description>GROUND</Description></Service><ReferenceNumber><Code>01</Code><Value>xxxxxxxx</Value></ReferenceNumber><ShipmentIdentificationNumber>1ZA85Y090310073852</ShipmentIdentificationNumber><PickupDate>20150318</PickupDate><DeliveryDateUnavailable><Type>Scheduled Delivery</Type><Description>Scheduled Delivery Date is not currently available, please try back later</Description></DeliveryDateUnavailable><Package><TrackingNumber>1ZA85Y090310073852</TrackingNumber><Activity><ActivityLocation><Address><City>ROUNDUP</City><StateProvinceCode>MT</StateProvinceCode><PostalCode>59072</PostalCode><CountryCode>US</CountryCode></Address><Code>MP</Code><Description>GARAGE</Description></ActivityLocation><Status><StatusType><Code>D</Code><Description>DELIVERED</Description></StatusType><StatusCode><Code>FS</Code></StatusCode></Status><Date>20150323</Date><Time>131200</Time></Activity><PackageWeight><UnitOfMeasurement><Code>LBS</Code></UnitOfMeasurement><Weight>4.90</Weight></PackageWeight><ReferenceNumber><Code>01</Code><Value>FN1-2514196-2965923</Value></ReferenceNumber><ReferenceNumber><Code>01</Code><Value>D3M0KFD7K</Value></ReferenceNumber><ReferenceNumber><Code>01</Code><Value>NA</Value></ReferenceNumber><ReferenceNumber><Code>01</Code><Value>UPS-CAOAK-T00441361L  UPS842508  UP</Value></ReferenceNumber></Package></Shipment></TrackResponse>, true]) [0.067 seconds]
[15-03-28 07:52:43:450 CST] Logger.log([{TrackResponse={Response={ResponseStatusCode={Text=1}, ResponseStatusDescription={Text=Success}}, Shipment={ShipmentIdentificationNumber={Text=1ZA85Y090310073852}, Service={Description={Text=GROUND}, code={Text=003}}, ReferenceNumber={Value={Text=129821607}, code={Text=01}}, Shipper={address={PostalCode={Text=95304   9370}, StateProvinceCode={Text=CA}, CountryCode={Text=US}, AddressLine1={Text=xxxxx}, City={Text=TRACY}}, ShipperNumber={Text=xxxx}}, Package={Activity={Status={StatusCode={code={Text=FS}}, StatusType={Description={Text=DELIVERED}, code={Text=D}}}, Time={Text=131200}, Date={Text=20150323}, ActivityLocation={Description={Text=GARAGE}, address={PostalCode={Text=59072}, StateProvinceCode={Text=MT}, CountryCode={Text=US}, City={Text=ROUNDUP}}, code={Text=MP}}}, ReferenceNumber=[{Value={Text=FN1-2514196-2965923}, code={Text=01}}, {Value={Text=D3M0KFD7K}, code={Text=01}}, {Value={Text=NA}, code={Text=01}}, {Value={Text=UPS-CAOAK-T00441361L  UPS842508  UP}, code={Text=01}}], TrackingNumber={Text=1ZA85Y090310073852}, PackageWeight={UnitOfMeasurement={code={Text=LBS}}, Weight={Text=4.90}}}, ShipmentWeight={UnitOfMeasurement={code={Text=LBS}}, Weight={Text=4.90}}, DeliveryDateUnavailable={Description={Text=Scheduled Delivery Date is not currently available, please try back later}, Type={Text=Scheduled Delivery}}, PickupDate={Text=20150318}, ShipTo={address={PostalCode={Text=59072}, StateProvinceCode={Text=MT}, CountryCode={Text=US}, City={Text=ROUNDUP}}}}}}, []]) [0.001 seconds]
[15-03-28 07:52:43:451 CST] Logger.log([1, []]) [0 seconds]
[15-03-28 07:52:43:523 CST] Execution failed: TypeError: Cannot read property "Text" from undefined. (line 60, file "Code") [0.551 seconds total runtime]

编辑:尝试检查仅SignedForByName:的条件

if (jsonText.TrackResponse.Shipment.Package.Activity.ActivityLocation.SignedForByName) { ... }

如果存在SignedForByName,则将变量设置为...SignedForByName.Text

在测试您的JSON对象时,我在查找SignedForName.Text时复制了您的错误消息,但在尝试访问SignedForByName时得到了undefined