问题
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);
}
回答1:
Try something like this perhaps?
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());
}
}
回答2:
To solve this problem make sure the data in the last column does not extend right into the next column. In this example the data is 3 and the range is 3 however since the data in the last column extends into the next column google sheets the data is 4 and the range is 3.
Illustration of error: https://i.stack.imgur.com/XjI7F.png
To fix this error extend the width of the last column to where all data in the last column fits inside the last column and does not overlap into the next column.Illustration of solution: https://i.stack.imgur.com/1uWr4.png
来源:https://stackoverflow.com/questions/58106515/columns-in-the-data-does-not-match-the-number-of-columns-in-the-range-the-data