Advertisement
Guest User

Untitled

a guest
Oct 17th, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const keyoptions = {
  2.   method: "GET",
  3.   headers: {
  4.     "Accept": "application/json+v6",
  5.     "x-api-key": "XXXXXXXXXXXXXXXXXXXXXXXXX"
  6.   }
  7. };
  8.  
  9. function getVRMData(row, column) {
  10.   const vrm = SpreadsheetApp.getActiveSheet().getRange(row, column).getValue();
  11.   const url = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX' + vrm;
  12.   const response = UrlFetchApp.fetch(url, keyoptions);
  13.   const data = JSON.parse(response.getContentText())[0].motTests;
  14.   // extract info from reply
  15.  
  16.   const rows = data.map(function(d) { return d.expiryDate; });
  17.   const resultRows = data.map(function(d) { return d.testResult; });
  18.   const rfrAndCommentsRows = data.map(function(d) { return d.rfrAndComments; });
  19.   return {
  20.     "rows": rows,
  21.     "resultRows": resultRows,
  22.     "rfrAndCommentsRows": rfrAndCommentsRows
  23.   };
  24. }
  25.  
  26. function main() {
  27.   const ss = SpreadsheetApp.getActiveSpreadsheet();
  28.   const sheet = ss.getActiveSheet();
  29.   const destSheet = ss.getSheetByName("customfunction");
  30.   destSheet.getRange('A4:F').clearContent();
  31.   const lastRow = destSheet.getLastRow();
  32.  
  33.   for (var row = 2; row <= 10; row++) {
  34.     var data = getVRMData(row, 1);
  35.     const rows = data['rows'];
  36.     const resultRows = data['resultRows'];
  37.     const rfrAndCommentsRows = data['rfrAndCommentsRows'];
  38.     sheet.getRange(lastRow + row, 1, rows.length, 1).setValues(rows);
  39.     sheet.getRange(lastRow + row, 2, resultRows.length, 1).setValues(resultRows);
  40.     sheet.getRange(lastRow + row, 3, rfrAndCommentsRows.length, 1).setValues(rfrAndCommentsRows);
  41.   }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement