Guest User

Untitled

a guest
Jun 3rd, 2019
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.24 KB | None | 0 0
  1. /**
  2. * @OnlyCurrentDoc
  3. */
  4.  
  5. function getXmlData(url, user, pass) {
  6. var pass = (typeof pass !== 'undefined')? pass : "";
  7.  
  8. try {
  9. var resp = UrlFetchApp.fetch(url,
  10. (typeof user !== 'undefined')
  11. // if 'user' is set, pass 'headers' with auth stuff
  12. ?{'headers':
  13. {'Authorization': "Basic " + Utilities.base64Encode(user+':'+pass)},
  14. 'muteHttpExceptions': true
  15. }
  16. // else, don't pass any parameters
  17. :{}
  18. );
  19. } catch (e) {
  20. return [["#ERROR!", "could not open connection"],
  21. e.toString(),
  22. "response is: " + ((typeof resp === 'undefined')?'undefined':resp.toString())];
  23. }
  24.  
  25. var connectionError = [];
  26. var headers = resp.getAllHeaders();
  27.  
  28. var code; // report anything but success codes (HTTP 2XX):
  29. if ((code = resp.getResponseCode()) < 200 || code >= 300) {
  30. connectionError = [["#ERROR!"], ["server responded with code: ", code], "headers: "].concat(__formatHeaders(headers));
  31. }
  32.  
  33. if (code == 401) { // specific additional info for 401 Unauthorized
  34. var headersHasBasicAuth = 0;
  35. if (typeof headers['WWW-Authenticate'] === 'string') {
  36. headersHasBasicAuth = headers['WWW-Authenticate'].match(/^Basic/) !== undefined;
  37. } else {
  38. Object.keys(headers['WWW-Authenticate']).reduce(function(a, val){
  39. return val.match(/^Basic/) !== undefined;
  40. }, 0);
  41. }
  42. if (headersHasBasicAuth) {
  43. connectionError[0].push("Server does not advertise Basic auth!");
  44. }
  45. }
  46.  
  47. if (connectionError.length > 0) {
  48. return connectionError;
  49. }
  50.  
  51. //Otherwise, the request was successful. Begin actually parsing XML:
  52.  
  53. var xml = resp.getContentText();
  54.  
  55. var document = XmlService.parse(xml);
  56. var root = document.getRootElement();
  57. var atom = XmlService.getNamespace('http://www.w3.org/2005/Atom');
  58. var metadata = XmlService.getNamespace('http://schemas.microsoft.com/ado/2007/08/dataservices/metadata')
  59.  
  60. var entries = root.getChildren('entry', atom);
  61. var ret = []; // 2d array which we'll fill width-first; every dimension is a row
  62.  
  63. for (var i = 0; i < entries.length; i++) {
  64. var content = entries[i].getChild('content', atom);
  65. var contentProperties = content.getChild('properties', metadata).getChildren();
  66.  
  67. row = []
  68. for (var j = 0; j < contentProperties.length; j++) {
  69. /* wasteful way to populate an 'index' row, thus naming the columns.
  70. it requires you to change 'ret = []' to 'ret = [[]]' higher up.
  71. it'd be better (though maybe less readable) to do this in an
  72. earlier loop, but it's just here for debugging anyway. */
  73. // ret[0][j] = contentProperties[j].getName(); // wasteful, but easy to read
  74. row.push(contentProperties[j].getText());
  75. }
  76. ret.push(row);
  77. }
  78.  
  79. return ret;
  80. }
  81.  
  82. function __formatHeaders(rawHeaders) {
  83. /** Accepts a headers Object (from HTTPResponse.getAllHeaders()) and returns the
  84. headers, formatted as a 2d array that fits nicely in a spreadsheet. **/
  85. var names = Object.keys(rawHeaders);
  86. var headers2d = [];
  87.  
  88. for (var i = 0; i < names.length; i++) {
  89. var row = [];
  90. row.push(names[i]);
  91. var header = rawHeaders[names[i]];
  92.  
  93. if(typeof header === 'string') {
  94. row.push(header);
  95. } else {
  96. // header is an Object, not an Array, so we can't Array.prototype.concat()
  97. for (var j = 0; j < header.length; j++) {
  98. row.push(header[j]);
  99. }
  100. }
  101.  
  102. headers2d.push(row);
  103. }
  104.  
  105. // Transpose the array because it's sideways
  106.  
  107. // Every row has to be the same length, so we'll pad them with null:
  108. var maxlen = headers2d.reduce(function(a, row) { if (row.length > a) return row.length; return a; }, 0);
  109. for (var i = 0; i < headers2d.length; i++) {
  110. while (headers2d[i].length < maxlen) {
  111. headers2d[i].push(null);
  112. }
  113. }
  114.  
  115. // The actual transposition:
  116. headers2d = headers2d[0].map(function(col, i) { return headers2d.map(function(row) { return row[i]; } ); } );
  117.  
  118. return headers2d;
  119. }
Advertisement
Add Comment
Please, Sign In to add comment