我正在构建一个函数,该函数应该接收XML响应的元素,并将元素的每次迭代存储在电子表格的自己的行中,直到考虑到所有元素为止。请在下面查看我的代码,我能够将元素标题和值打印到记录器,但无法访问每个值以传输到工作表。请告知。
function testPOST(e) {
var url = "API_URL";
var payload = e;
var options =
{
"method" : "POST",
"payload" : payload,
"followRedirects" : true,
"muteHttpExceptions": true
};
var result = UrlFetchApp.fetch(url, options);
if (result.getResponseCode() == 200) {
// Logger.log(result);
Logger.log(result.getResponseCode() + "\n\n");
}
/*TESTING*/
var od = XmlService.parse(result);
pm = XmlService.getPrettyFormat().format(od);
Logger.log(pm);
/* Preferred Approach */
var ss = SpreadsheetApp.openById("SPREADSHEET_ID").getSheetByName("Output"),
temps = [],
root = XmlService.parse(result).getRootElement().getDescendants(),
temps = root;
for (var [key,val] in temps)
{
if(val == "")
{
continue;
}
else{
// Logger.log("Index Value:"+e.values[key]+"\n");
Logger.log(key+" Value:"+val+ "\n");
}
}
}
这是记录器输出的示例。
[19-07-17 17:05:39:717 EDT] 200
[19-07-17 17:05:39:838 EDT] 0 Value:[Element: <serv:header [Namespace: URL]/>]
[19-07-17 17:05:39:839 EDT] 1 Value:[Element: <serv:response [Namespace: URL]/>]
[19-07-17 17:05:39:840 EDT] 2 Value:[Element: <serv:result [Namespace: URL]/>]
[19-07-17 17:05:39:840 EDT] 3 Value:SUCCESS
[19-07-17 17:05:39:840 EDT] 4 Value:[Element: <serv:gsbStatus [Namespace: URL]/>]
[19-07-17 17:05:39:841 EDT] 5 Value:BACKUP
[19-07-17 17:05:39:841 EDT] 6 Value:[Element: <serv:body [Namespace: URL]/>]
[19-07-17 17:05:39:842 EDT] 7 Value:[Element: <serv:bodyContent [Namespace: URL]/>]
[19-07-17 17:05:39:842 EDT] 8 Value:[Element: <use:user [Namespace: URL]/>]
[19-07-17 17:05:39:842 EDT] 9 Value:[Element: <use:userID [Namespace: URL]/>]
[19-07-17 17:05:39:843 EDT] 10 Value:UID
[19-07-17 17:05:39:843 EDT] 11 Value:[Element: <use:Id [Namespace: URL]/>]
[19-07-17 17:05:39:844 EDT] 12 Value:ID
[19-07-17 17:05:39:844 EDT] 13 Value:[Element: <use:firstName [Namespace: URL]/>]
[19-07-17 17:05:39:844 EDT] 14 Value:NAME
[19-07-17 17:05:39:845 EDT] 15 Value:[Element: <use:lastName [Namespace: URL]/>]
[19-07-17 17:05:39:845 EDT] 16 Value:NAME
[19-07-17 17:05:39:846 EDT] 17 Value:[Element: <use:email [Namespace: URL]/>]
[19-07-17 17:05:39:846 EDT] 18 Value:EMAIL
[19-07-17 17:05:39:846 EDT] 19 Value:[Element: <use:userRoleType [Namespace: URL]/>]
[19-07-17 17:05:39:847 EDT] 20 Value:HOST
[19-07-17 17:05:39:848 EDT] 21 Value:[Element: <use:registrationDate [Namespace: URL]/>]
[19-07-17 17:05:39:848 EDT] 22 Value:DATE
[19-07-17 17:05:39:849 EDT] 23 Value:[Element: <use:active [Namespace: URL]/>]
[19-07-17 17:05:39:849 EDT] 24 Value:ACTIVATED
[19-07-17 17:05:39:850 EDT] 25 Value:[Element: <use:timeZoneID [Namespace: URL]/>]
[19-07-17 17:05:39:850 EDT] 26 Value:11
[19-07-17 17:05:39:851 EDT] 27 Value:[Element: <use:lastLoginTime [Namespace: URL]/>]
**Iterates**
[19-07-17 17:05:39:852 EDT] 28 Value:DATE
[19-07-17 17:05:39:842 EDT] 8 Value:[Element: <use:user [Namespace: URL]/>]
[19-07-17 17:05:39:842 EDT] 9 Value:[Element: <use:userID [Namespace: URL]/>]
[19-07-17 17:05:39:843 EDT] 10 Value:UID
[19-07-17 17:05:39:843 EDT] 11 Value:[Element: <use:Id [Namespace: URL]/>]
[19-07-17 17:05:39:844 EDT] 12 Value:ID
[19-07-17 17:05:39:844 EDT] 13 Value:[Element: <use:firstName [Namespace: URL]/>]
[19-07-17 17:05:39:844 EDT] 14 Value:NAME
[19-07-17 17:05:39:845 EDT] 15 Value:[Element: <use:lastName [Namespace: URL]/>]
[19-07-17 17:05:39:845 EDT] 16 Value:NAME
[19-07-17 17:05:39:846 EDT] 17 Value:[Element: <use:email [Namespace: URL]/>]
[19-07-17 17:05:39:846 EDT] 18 Value:EMAIL
[19-07-17 17:05:39:846 EDT] 19 Value:[Element: <use:userRoleType [Namespace: URL]/>]
[19-07-17 17:05:39:847 EDT] 20 Value:HOST
[19-07-17 17:05:39:848 EDT] 21 Value:[Element: <use:registrationDate [Namespace: URL]/>]
[19-07-17 17:05:39:848 EDT] 22 Value:DATE
[19-07-17 17:05:39:849 EDT] 23 Value:[Element: <use:active [Namespace: URL]/>]
[19-07-17 17:05:39:849 EDT] 24 Value:ACTIVATED
[19-07-17 17:05:39:850 EDT] 25 Value:[Element: <use:timeZoneID [Namespace: URL]/>]
[19-07-17 17:05:39:850 EDT] 26 Value:11
[19-07-17 17:05:39:851 EDT] 27 Value:[Element: <use:lastLoginTime [Namespace: URL]/>]
[19-07-17 17:05:39:852 EDT] 28 Value:DATE
URLFetchApp.fetch的值
<?xml version="1.0" encoding="UTF-8"?>
<serv:message xmlns:serv="http://www.webex.com/schemas/2002/06/service" xmlns:com="http://www.webex.com/schemas/2002/06/common" xmlns:use="http://www.webex.com/schemas/2002/06/service/user">
<serv:header>
<serv:response>
<serv:result>SUCCESS</serv:result>
<serv:gsbStatus>PRIMARY</serv:gsbStatus>
</serv:response>
</serv:header>
<serv:body>
<serv:bodyContent xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="use:lstsummaryUserResponse">
<use:user>
<use:userID>UID</use:userID>
<use:webExId>ID</use:webExId>
<use:firstName>NAME</use:firstName>
<use:lastName>NAME</use:lastName>
<use:email>EMAIL</use:email>
<use:userRoleType>HOST</use:userRoleType>
<use:registrationDate>DATE</use:registrationDate>
<use:active>ACTIVATED</use:active>
<use:timeZoneID>11</use:timeZoneID>
<use:lastLoginTime>DATE</use:lastLoginTime>
</use:user>
<use:user>
<use:userID>UID</use:userID>
<use:webExId>ID</use:webExId>
<use:firstName>NAME</use:firstName>
<use:lastName>NAME</use:lastName>
<use:email>EMAIL</use:email>
<use:userRoleType>SITEADMIN</use:userRoleType>
<use:registrationDate>DATE</use:registrationDate>
<use:active>ACTIVATED</use:active>
<use:timeZoneID>7</use:timeZoneID>
<use:lastLoginTime>DATE</use:lastLoginTime>
</use:user>
<use:matchingRecords>
<serv:total>2</serv:total>
<serv:returned>2</serv:returned>
<serv:startFrom>1</serv:startFrom>
</use:matchingRecords>
</serv:bodyContent>
</serv:body>
</serv:message>
答案 0 :(得分:0)
userID webExId firstName lastName email userRoleType registrationDate active timeZoneID lastLoginTime
UID ID NAME NAME EMAIL HOST DATE ACTIVATED 11 DATE
UID ID NAME NAME EMAIL SITEADMIN DATE ACTIVATED 7 DATE
我可以像上面那样理解。如果我的理解是正确的,那么该示例脚本如何?请认为这只是几个答案之一。
在运行脚本之前,请设置电子表格ID。
function myFunction() {
var id = "###"; // <--- Please set the Spreadsheet ID here.
// This is the sample XML data from your updated question.
var result = '<?xml version="1.0" encoding="UTF-8"?><serv:message xmlns:serv="http://www.webex.com/schemas/2002/06/service" xmlns:com="http://www.webex.com/schemas/2002/06/common" xmlns:use="http://www.webex.com/schemas/2002/06/service/user"><serv:header><serv:response><serv:result>SUCCESS</serv:result><serv:gsbStatus>PRIMARY</serv:gsbStatus></serv:response></serv:header><serv:body><serv:bodyContent xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="use:lstsummaryUserResponse"><use:user><use:userID>UID</use:userID><use:webExId>ID</use:webExId><use:firstName>NAME</use:firstName><use:lastName>NAME</use:lastName><use:email>EMAIL</use:email><use:userRoleType>HOST</use:userRoleType><use:registrationDate>DATE</use:registrationDate><use:active>ACTIVATED</use:active><use:timeZoneID>11</use:timeZoneID><use:lastLoginTime>DATE</use:lastLoginTime></use:user><use:user><use:userID>UID</use:userID><use:webExId>ID</use:webExId><use:firstName>NAME</use:firstName><use:lastName>NAME</use:lastName><use:email>EMAIL</use:email><use:userRoleType>SITEADMIN</use:userRoleType><use:registrationDate>DATE</use:registrationDate><use:active>ACTIVATED</use:active><use:timeZoneID>7</use:timeZoneID><use:lastLoginTime>DATE</use:lastLoginTime></use:user><use:matchingRecords><serv:total>2</serv:total><serv:returned>2</serv:returned><serv:startFrom>1</serv:startFrom></use:matchingRecords></serv:bodyContent></serv:body></serv:message>';
var header = [];
var values = [];
var root = XmlService.parse(result).getRootElement();
var 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);
var sheet = SpreadsheetApp.openById(id).getSheetByName("Output");
sheet.getRange(1, 1, values.length, values[0].length).setValues(values);
}