eBay API 将 XML 解析为 Google 脚本时出现问题

Problem with eBay API Parsing XML to Google Script

我正在尝试调用 eBay API GetMyeBaySelling 我可以获得 Act、Timestamp、Version 和 Build 到 return 并将其写入 Google Sheet 但是无论我尝试什么,我都无法将 'Item' 列表添加到 return。响应如下,我需要帮助来获取任何其他字段。假设我想遍历 return 'BuyItNowPrice' 所有 returned 列表。我已经尝试了下面的代码,但我很快就无处可去。

我更习惯 Excel VBA 但正在尝试过渡到 Google Sheets 脚本。

API 响应:

[20-09-15 19:52:46:012 BST] Logging output too large. Truncating output. <?xml version="1.0" encoding="UTF-8"?>
<GetMyeBaySellingResponse xmlns="urn:ebay:apis:eBLBaseComponents"><Timestamp>2020-09-15T18:52:45.627Z</Timestamp><Ack>Success</Ack><Version>1163</Version><Build>E1163_CORE_APISELLING_19187371_R1</Build><ActiveList><ItemArray><Item><BuyItNowPrice currencyID="GBP">3.99</BuyItNowPrice><ItemID>##########</ItemID><ListingDetails><StartTime>2011-09-16T10:51:54.000Z</StartTime><ViewItemURL>https://www.ebay.co.uk/itm/???????????????????????????-/############</ViewItemURL><ViewItemURLForNaturalSearch>http://cgi.ebay.co.uk/????????item=############&amp;category=51169&amp;cmd=ViewItem</ViewItemURLForNaturalSearch></ListingDetails><ListingDuration>GTC</ListingDuration><ListingType>StoresFixedPrice</ListingType><Quantity>70</Quantity><SellingStatus><CurrentPrice currencyID="GBP">3.99</CurrentPrice></SellingStatus><ShippingDetails><ShippingServiceOptions><ShippingServiceCost currencyID="GBP">0.0</ShippingServiceCost></ShippingServiceOptions><ShippingType>Flat</ShippingType></ShippingDetails><TimeLeft>PT15H59M9S</TimeLeft><Title>????????????????????????????</Title><WatchCount>3</WatchCount><QuestionCount>3</QuestionCount><QuantityAvailable>0</QuantityAvailable><SKU>???????</SKU><PictureDetails><GalleryURL>http://thumbs.ebaystatic.com/pict/???????????_5.jpg</GalleryURL></PictureDetails><NewLeadCount>3</NewLeadCount><ClassifiedAdPayPerLeadFee currencyID="GBP">0.0</ClassifiedAdPayPerLeadFee><SellerProfiles><SellerShippingProfile><ShippingProfileID>??????????</ShippingProfileID><ShippingProfileName>Flat:Royal Mail 2nd(Free),Royal Mail Tra,Same#79</ShippingProfileName></SellerShippingProfile><SellerReturnProfile><ReturnProfileID>????????????</ReturnProfileID><ReturnProfileName>Returns Accepted,Buyer,30 days#2</ReturnProfileName></SellerReturnProfile><SellerPaymentProfile><PaymentProfileID>124284320022</PaymentProfileID><PaymentProfileName>PayPal#0</PaymentProfileName></SellerPaymentProfile></SellerProfiles><HideFromSearch>true</HideFromSearch><ReasonHideFromSearch>OutOfStock</ReasonHideFromSearch><OutOfStockControl>true</OutOfStockControl></Item><Item>

我的代码:

    var response = UrlFetchApp.fetch(site, options);

  
   var document = XmlService.parse(response);
  Logger.log(response);
  var root = document.getRootElement();
  var NS = XmlService.getNamespace("urn:ebay:apis:eBLBaseComponents");
   
  var ack = root.getChildText('Ack', NS);
  var time = root.getChildText('Timestamp', NS);
  var version = root.getChildText('Version', NS);
  var build = root.getChildText('Build', NS);
  var shortmessage = root.getChildText('ShortMessage', NS);
   //Logger.log(shortmessage);  
  var longmessage = root.getChildText('LongMessage', NS);
   //Logger.log(longmessage);
  
  var itms = root.getChild('ActiveList', NS).getChild('ItemArray', NS).getChild('BuyItNowPrice', NS);
        Logger.log(itms + '1st Log');
  for (var i = 0; i < itms.length; i++) {
    var item = itms[i].getChild('Item', NS).getText();
      Logger.log(item + '2nd Log');
  }

    //sheet.getRange('A2').setValue(xml);
   var LstSheet = sheet.getLastRow()+1
   sheet.getRange(LstSheet, 1).setValue([ack]);
   sheet.getRange(LstSheet, 2).setValue([time]);
   sheet.getRange(LstSheet, 3).setValue([version]);
   sheet.getRange(LstSheet, 4).setValue([build]);
   sheet.getRange(LstSheet, 5).setValue([shortmessage]);
   sheet.getRange(LstSheet, 6).setValue([longmessage]);


提前致谢

更新:16-09-2020 @ 17:08hrs 英国:好的,我现在有了工作代码,它可以将数据写入 sheet,但是在 4 秒内写入 200 条记录大约需要 82 秒列。将数据写入数组然后将其添加到 sheet 会更快吗?如果它会更快,你能帮忙举个例子吗?事实上,无论如何,最好看看如何将它写入数组,然后写入 sheet,这样我可以学到更多。

我的工作代码:

  var itms = root.getChild('ActiveList', NS).getChild('ItemArray', NS).getChildren();
  for (var i = 0; i < itms.length; i++) {
    
    var item = itms[i].getChild('ItemID', NS).getText();
    var title = itms[i].getChild('Title', NS).getText()
    var url = itms[i].getChild('ListingDetails', NS).getChild('ViewItemURLForNaturalSearch', NS).getText()
    var imgurl = itms[i].getChild('PictureDetails', NS).getChild('GalleryURL', NS).getText()
    
    
    sheet.appendRow([item, title, url, imgurl]);

我相信你的目标如下。

  • 您想使用 Google Apps 脚本从 ItemArray 中检索 BuyItNowPrice 的值。
  • response of var response = UrlFetchApp.fetch(site, options); 在“API Response:”.

修改点:

  • 我认为在你问题的XML数据中,root.getChild('ActiveList', NS).getChild('ItemArray', NS).getChild('BuyItNowPrice', NS)returnsnull。在本例中,要求为var itms = root.getChild('ActiveList', NS).getChild('ItemArray', NS).getChildren();.
  • for循环中,要求为var item = itms[i].getChild('BuyItNowPrice', NS).getText();.

当以上几点反映到你的脚本中,就会变成下面这样。

修改后的脚本:

从:
var itms = root.getChild('ActiveList', NS).getChild('ItemArray', NS).getChild('BuyItNowPrice', NS);
      Logger.log(itms + '1st Log');
for (var i = 0; i < itms.length; i++) {
  var item = itms[i].getChild('Item', NS).getText();
    Logger.log(item + '2nd Log');
}
到:
var itms = root.getChild('ActiveList', NS).getChild('ItemArray', NS).getChildren();
for (var i = 0; i < itms.length; i++) {
  var item = itms[i].getChild('BuyItNowPrice', NS).getText();
  Logger.log(item);
}

当你在“API Response:”中的XML数据被使用时,示例脚本变成如下。

function myFunction() {
  var response = `<?xml version="1.0" encoding="UTF-8"?>
<GetMyeBaySellingResponse
    xmlns="urn:ebay:apis:eBLBaseComponents">
    <Timestamp>2020-09-15T18:52:45.627Z</Timestamp>
    <Ack>Success</Ack>
    <Version>1163</Version>
    <Build>E1163_CORE_APISELLING_19187371_R1</Build>
    <ActiveList>
        <ItemArray>
            <Item>
                <BuyItNowPrice currencyID="GBP">3.99</BuyItNowPrice>
                <ItemID>##########</ItemID>
                <ListingDetails>
                    <StartTime>2011-09-16T10:51:54.000Z</StartTime>
                    <ViewItemURL>https://www.ebay.co.uk/itm/???????????????????????????-/############</ViewItemURL>
                    <ViewItemURLForNaturalSearch>http://cgi.ebay.co.uk/????????item=############&amp;category=51169&amp;cmd=ViewItem</ViewItemURLForNaturalSearch>
                </ListingDetails>
                <ListingDuration>GTC</ListingDuration>
                <ListingType>StoresFixedPrice</ListingType>
                <Quantity>70</Quantity>
                <SellingStatus>
                    <CurrentPrice currencyID="GBP">3.99</CurrentPrice>
                </SellingStatus>
                <ShippingDetails>
                    <ShippingServiceOptions>
                        <ShippingServiceCost currencyID="GBP">0.0</ShippingServiceCost>
                    </ShippingServiceOptions>
                    <ShippingType>Flat</ShippingType>
                </ShippingDetails>
                <TimeLeft>PT15H59M9S</TimeLeft>
                <Title>????????????????????????????</Title>
                <WatchCount>3</WatchCount>
                <QuestionCount>3</QuestionCount>
                <QuantityAvailable>0</QuantityAvailable>
                <SKU>???????</SKU>
                <PictureDetails>
                    <GalleryURL>http://thumbs.ebaystatic.com/pict/???????????_5.jpg</GalleryURL>
                </PictureDetails>
                <NewLeadCount>3</NewLeadCount>
                <ClassifiedAdPayPerLeadFee currencyID="GBP">0.0</ClassifiedAdPayPerLeadFee>
                <SellerProfiles>
                    <SellerShippingProfile>
                        <ShippingProfileID>??????????</ShippingProfileID>
                        <ShippingProfileName>Flat:Royal Mail 2nd(Free),Royal Mail Tra,Same#79</ShippingProfileName>
                    </SellerShippingProfile>
                    <SellerReturnProfile>
                        <ReturnProfileID>????????????</ReturnProfileID>
                        <ReturnProfileName>Returns Accepted,Buyer,30 days#2</ReturnProfileName>
                    </SellerReturnProfile>
                    <SellerPaymentProfile>
                        <PaymentProfileID>124284320022</PaymentProfileID>
                        <PaymentProfileName>PayPal#0</PaymentProfileName>
                    </SellerPaymentProfile>
                </SellerProfiles>
                <HideFromSearch>true</HideFromSearch>
                <ReasonHideFromSearch>OutOfStock</ReasonHideFromSearch>
                <OutOfStockControl>true</OutOfStockControl>
            </Item>

        </ItemArray>
    </ActiveList>
</GetMyeBaySellingResponse>`;

  var document = XmlService.parse(response);
  var root = document.getRootElement();
  var NS = XmlService.getNamespace("urn:ebay:apis:eBLBaseComponents");
  var itms = root.getChild('ActiveList', NS).getChild('ItemArray', NS).getChildren();
  for (var i = 0; i < itms.length; i++) {
    var item = itms[i].getChild('BuyItNowPrice', NS).getText();
    Logger.log(item);
  }
}

参考:

已添加:

在您最初的问题中,您想要检索 BuyItNowPrice 的值。在您的新问题中,您想要检索 ItemIDTitleListingDetails.ViewItemURLForNaturalSearchPictureDetails.GalleryURL 的值。从你最初的问题来看,我没有注意到你的新问题。

在您的脚本中,appendRow 用于循环。在这种情况下,工艺成本会很高。所以我想建议把值放在一个数组里,然后把数组放到sheet.

修改后的脚本:

var itms = root.getChild('ActiveList', NS).getChild('ItemArray', NS).getChildren();
var values = itms.map(e => {
  var item = e.getChild('ItemID', NS);
  var title = e.getChild('Title', NS);
  var url = e.getChild('ListingDetails', NS).getChild('ViewItemURLForNaturalSearch', NS);
  var imgurl = e.getChild('PictureDetails', NS).getChild('GalleryURL', NS);
  return [item ? item.getText() : "", title ? title.getText() : "", url ? url.getText() : "", imgurl ? imgurl.getText() : ""];
});
sheet.getRange(sheet.getLastRow() + 1, 1, values.length, values[0].length).setValues(values);