数据中的列与范围内的列数不匹配(数据有12个,但范围有11个) [英] Columns in the data does not match the number of columns in the range (The data has 12 but the range has 11)

查看:68
本文介绍了数据中的列与范围内的列数不匹配(数据有12个,但范围有11个)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个函数,该函数应该接收XML响应的元素,并将元素的每次迭代存储在电子表格的自己的行中,直到考虑到所有元素为止.这将一直有效,直到要退回的帐户超过500个为止(请参阅

I am building a function that should take the elements of an XML response and store each iteration of an element in it's own row in a spreadsheet until all the elements have been accounted for. This works, until there are more than 500 accounts to be returned (please see here for API info). Then, I recieve The number of columns in the data does not match the number of columns in the range. The data has 12 but the range has 11. Please see my code below thank you for your time and assistance.

function testPOST(e) {

  var url = "https://api.webex.com/WBXService/XMLService";

  var payload = e;

  var options =
      {
        "method"  : "POST",
        "payload" : payload,   
        "followRedirects" : true,
        "muteHttpExceptions": true
      };

  var result = UrlFetchApp.fetch(url, options);

  if (result.getResponseCode() == 200) {
    //    var od = XmlService.parse(result),
    //        pm = XmlService.getPrettyFormat().format(od);
    //    Logger.log(pm);
    Logger.log(result.getResponseCode() + "\n\n");

    /* Preferred Approach */  
    var ss = SpreadsheetApp.openById("ID").getSheetByName("Output");

    var header = [],
        values = [],
        root = XmlService.parse(result).getRootElement(),
        c1 = root.getChildren();
    for(var i = 0; i < c1.length;i++){
      if (c1[i].getName() == "body") {
        var c2 = c1[i].getChildren()[0].getChildren();
        for (var j = 0; j < c2.length; j++) {
          if (c2[j].getName() == "user") {
            var c3 = c2[j].getChildren();
            var temp = [];
            for (var k = 0; k < c3.length; k++) {
              if (j == 0) header.push(c3[k].getName());
              temp.push(c3[k].getValue());
            }
            values.push(temp);
          }
        }
      }
    }
    values.unshift(header);

    Logger.log(values);

    ss.getRange(1, 1, values.length, values[0].length).setValues(values);

  }
  else
  {
    Logger.log("\nFAILED ERROR:\n" + result.getContentText());
  }

}


function myFuction() {

  var siteName = "SITE",
      webexID = "ADMIN",
      pwd = "PASSWORD",
      startFrom = 0;

  var xmlst = '<?xml version="1.0" encoding="UTF-8"?>';

  var xmlbdy = '<message xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">'
  +'<header><securityContext>'
  +'<siteName>'+siteName+'</siteName>'
  +'<webExID>'+webexID+'</webExID>'
  +'<password>'+pwd+'</password>'
  +'</securityContext></header>'
  +'<body><bodyContent xsi:type="java:com.webex.service.binding.user.LstsummaryUser">'
  +'<listControl><startFrom>1</startFrom><maximumNum>500</maximumNum><listMethod>AND</listMethod></listControl>'
  +'<order><orderBy>UID</orderBy><orderAD>ASC</orderAD></order>'
  +'<active>ACTIVATED</active>'
  +'<dataScope></dataScope>'
  +'</bodyContent></body></message>';

  xmlst += xmlbdy;

  var document = XmlService.parse(xmlst);
  var output = XmlService.getPrettyFormat().format(document);

  //  Logger.log("\n" + output + "\n\n\n");
  testPOST(output);
}

推荐答案

尝试类似的方法吗?

function testPOST(e) {
  var url = "https://api.webex.com/WBXService/XMLService";
  var payload = e;
  var options={"method"  : "POST","payload" : payload,"followRedirects" : true,"muteHttpExceptions": true};
  var result = UrlFetchApp.fetch(url, options);
  var lA=[];
  if (result.getResponseCode() == 200) {
    Logger.log(result.getResponseCode() + "\n\n");
    var ss=SpreadsheetApp.openById("ID").getSheetByName("Output");
    var header=[],values=[],root=XmlService.parse(result).getRootElement(),c1 = root.getChildren();
    for(var i=0;i<c1.length;i++){
      if (c1[i].getName() == "body") {
        var c2=c1[i].getChildren()[0].getChildren();
        for (var j=0;j<c2.length;j++) {
          if (c2[j].getName()=="user") {
            var c3 = c2[j].getChildren();
            var temp = [];
            for (var k=0;k<c3.length;k++) {
              if (j==0)header.push(c3[k].getName());
              temp.push(c3[k].getValue());
            }
            values.push(temp);
            lA.push(temp.length);
          }
        }
      }
    }
    values.unshift(header);
    Logger.log(values);
    var lnth=lA.sort(function(a,b){return b-a;})[0];
    ss.getRange(1,1,values.length,lnth).setValues(values);
  }
  else
  {
    Logger.log("\nFAILED ERROR:\n" + result.getContentText());
  }

}

这篇关于数据中的列与范围内的列数不匹配(数据有12个,但范围有11个)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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