gpt4 book ai didi

javascript - 语法错误: Unexpected identifier while Parsin XML Data Using Google App Script

转载 作者:行者123 更新时间:2023-12-02 21:45:21 26 4
gpt4 key购买 nike

因此,我通过 API 从休假系统获取休假数据,并将其发布到 Google 表格,下面是示例响应 xml 响应。

<?xml version='1.0' encoding='ISO-8859-1'?>
<DataService>
<Request ID="1253" Status="Approved">
<TimeOffDate>2020-02-07</TimeOffDate>
<TimeOffDayOfWeek>Friday</TimeOffDayOfWeek>
<TimeStart></TimeStart>
<TimeEnd></TimeEnd>
<TimeOffHours>8.000</TimeOffHours>
<TimeOffTypeName>Annual Vacation</TimeOffTypeName>
<LoginID>testuser</LoginID>
<Firstname>test</Firstname>
<Lastname>user</Lastname>
<UserCategory></UserCategory>
<SubmittedDate>2019-10-03</SubmittedDate>
<Deducted>Yes</Deducted>
<Comment>
<![CDATA[* time-off request created by administrator]]>
</Comment>
</Request>

下面是我用来获取数据并将其设置到 Google 表格的代码。我的挑战是,我不确定如何从响应中获取属性 IDStatus 并将其推送到 requestObjects 数组。每次我尝试使用 request.getAttribute("ID") 推送属性时,都会收到错误
语法错误:意外的标识符

       };
var url = 'https://data.purelyhr.com/xml?ak=' + ak + '&sDate=' + start + '&eDate=' + end + '&TimeOffTypeName'+ '&page=' + pages;
var response = UrlFetchApp.fetch(url).getContentText();
var document = XmlService.parse(response);
var root = document.getRootElement();

//set variables to data from PurelyHR
var requestElements = root.getChildren('Request'); // Get all <Request> elements
var requestObjects = []; // Request objects for logging / eventual printing
for (var i = 0; i < requestElements.length; i++) {
var request = requestElements[i]; // A single <Request> element
var Status = request.getAttribute("Status").getValue();

// Add to requestObjects array
requestObjects.push({
Status:request.getAttribute("Status"),
TimeOffDate: request.getChild('TimeOffDate').getText(),
TimeOffDayOfWeek: request.getChild('TimeOffDayOfWeek').getText(),
TimeStart: request.getChild('TimeStart').getText(),
TimeEnd: request.getChild('TimeEnd').getText(),
TimeOffHours: request.getChild('TimeOffHours').getText(),
TimeOffTypeName: request.getChild('TimeOffTypeName').getText(),
LoginID: request.getChild('LoginID').getText(),
Firstname: request.getChild('Firstname').getText(),
Lastname: request.getChild('Lastname').getText(),
UserCategory: request.getChild('UserCategory').getText(),
SubmittedDate: request.getChild('SubmittedDate').getText(),
Deducted: request.getChild('Deducted').getText(),
Comment: request.getChild('Comment').getText()
});
}
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var range = sheet.getRange("A2:J").getValue();
for (var j = 0; j < requestObjects.length; j++) {
sheet.getRange(2 + j, 1).setValue(requestObjects[j].Status)
sheet.getRange(2 + j, 2).setValue(requestObjects[j].Firstname)
sheet.getRange(2 + j, 3).setValue(requestObjects[j].Lastname)
sheet.getRange(2 + j, 4).setValue(requestObjects[j].LoginID);
sheet.getRange(2 + j, 5).setValue(requestObjects[j].TimeOffTypeName);
sheet.getRange(2 + j, 6).setValue(requestObjects[j].TimeOffDayOfWeek);
sheet.getRange(2 + j, 7).setValue(requestObjects[j].SubmittedDate);
sheet.getRange(2 + j, 8).setValue(requestObjects[j].TimeOffHours)
sheet.getRange(2 + j, 9).setValue(requestObjects[j].TimeOffDate)
Logger.log(JSON.stringify(requestObjects));
}

}

G 工作表上的结果

enter image description here

最佳答案

当您在 Element 实例上调用 getAttribute() 函数时,它会返回 Attribute 类的实例。然后,您需要在该 Attribute 实例上调用 getValue()。例如:

var id = request.getAttribute("ID").getValue();

请注意,返回的值始终为String类型。

关于javascript - 语法错误: Unexpected identifier while Parsin XML Data Using Google App Script,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60264100/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com