0

So I am getting leave data from Leave system via API and posting this to a google sheet and below is the sample response xml response.

<?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>

Below is the code that I use to get the data and set it to Google sheet. My challenge is, I am not sure on how to get the attribute ID and Status from the response and push it to the requestObjects array. each time I try to push the attribute using request.getAttribute("ID"), I get an error
SyntaxError: Unexpected identifier

       };
  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));
}

}

Results on G sheet

enter image description here

6
  • The ID and Status fields of the object don't have property names. Commented Feb 17, 2020 at 14:02
  • @Just I can't replicate this. It's probably, as Pointy wrote, that one of the <Request> elements doesn't have an ID or Status attribute. Commented Feb 17, 2020 at 15:13
  • Can I ask you about your result you expect? Commented Feb 18, 2020 at 0:59
  • @Tanaike, My intention is to get Status as a string, that is Approved or Cancelled, but what am getting is [Status='Approved'] and is not what I want Commented Feb 18, 2020 at 5:23
  • @Tanaike, Thanks a lot. I modified and it works. And by the way, you understood me correctly. Commented Feb 18, 2020 at 7:20

1 Answer 1

1

When you invoke the getAttribute() function on an Element instance, it returns an instance of the Attribute class. You then need to call getValue() on that Attribute instance. For example:

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

Note that the value returned is always of type String.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for looking at this. I have done this as indicated in my edited code, what I get in the G sheet seems to be odd. Please look at the editted question. Thanks
@Just Don't just copy-n-paste example code; try to understand the logic behind it and then apply it to your use case. Look at your code to see where you should be calling the getValue() method (hint...where else are you calling the getAttribute() function).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.