Advertisement
ZiGoomul

API Новой Почты с Google таблицами

Dec 23rd, 2020
2,352
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const apiKey = "ваш apiKey";
  2. const url = "https://api.novaposhta.ua/v2.0/json/";
  3. const ss = SpreadsheetApp.getActiveSpreadsheet();
  4. const sheetNP = ss.getSheetByName("НП");
  5. const sheetSities = ss.getSheetByName("Города");
  6.  
  7. function getSities() {
  8.  
  9.   let data = {
  10.     "modelName": "Address",
  11.     "calledMethod": "getCities"
  12.   };
  13.  
  14.   let options = {
  15.     "method": "POST",
  16.     "headers": {
  17.       "content-type": "application/json",
  18.       "apiKey": apiKey
  19.     },
  20.     "async": true,
  21.     "crossDomain": true,
  22.     "processData": false,
  23.     "payload": JSON.stringify(data)
  24.   }
  25.  
  26.   let response = UrlFetchApp.fetch(url, options);
  27.   let dataPars = JSON.parse(response.getContentText());
  28.   let maxSities = dataPars.data.length
  29.  
  30.   let sities = [];
  31.   let sitiesRef = [];
  32.  
  33.   for (let i = 0; i < maxSities; i++) {
  34.     sities.push([dataPars.data[i].DescriptionRu])
  35.     sitiesRef.push([dataPars.data[i].Ref])
  36.   }
  37.   sheetSities.getRange(2, 1, maxSities).setValues(sities)
  38.   sheetSities.getRange(2, 2, maxSities).setValues(sitiesRef)
  39.   // console.log([dataPars.data[50]])
  40.  
  41. }
  42.  
  43. function getPrice() {
  44.  
  45.   let CitySender = sheetNP.getRange("B2").getValue();
  46.   let CityRecipient = sheetNP.getRange("D2").getValue();
  47.   let time = sheetNP.getRange("F2").getDisplayValue();
  48.  
  49.   let data = {
  50.     "modelName": "InternetDocument",
  51.     "calledMethod": "getDocumentPrice",
  52.     "methodProperties": {
  53.       "CitySender": CitySender,
  54.       "CityRecipient": CityRecipient,
  55.       "Weight": "15",
  56.       "ServiceType": "DoorsDoors",
  57.       "Cost": "100",
  58.       "CargoType": "Cargo",
  59.       "SeatsAmount": "10",
  60.       "PackCalculate": {
  61.         "PackCount": "1",
  62.         "PackRef": "1499fa4a-d26e-11e1-95e4-0026b97ed48a"
  63.       },
  64.       "RedeliveryCalculate": {
  65.         "CargoType": "Money",
  66.         "Amount": "100"
  67.       }
  68.     }
  69.   };
  70.  
  71.   let options = {
  72.     "method": "POST",
  73.     "headers": {
  74.       "content-type": "application/json",
  75.       "apiKey": apiKey
  76.     },
  77.     "async": true,
  78.     "crossDomain": true,
  79.     "processData": false,
  80.     "payload": JSON.stringify(data)
  81.   }
  82.  
  83.   let response = UrlFetchApp.fetch(url, options);
  84.   let dataPars = JSON.parse(response.getContentText());
  85.  
  86.   sheetNP.getRange("H2").setValue(dataPars.data[0].Cost)
  87.   // console.log(dataPars.data[0].Cost)
  88.  
  89. }
  90. function getDay() {
  91.  
  92.   let CitySender = sheetNP.getRange("B2").getValue();
  93.   let CityRecipient = sheetNP.getRange("D2").getValue();
  94.   let time = sheetNP.getRange("F2").getDisplayValue();
  95.  
  96.   let data = {
  97.     "modelName": "InternetDocument",
  98.     "calledMethod": "getDocumentDeliveryDate",
  99.     "methodProperties": {
  100.       "DateTime": time,
  101.       "ServiceType": "WarehouseDoors",
  102.       "CitySender": CitySender,
  103.       "CityRecipient": CityRecipient
  104.     }
  105.   };
  106.  
  107.   let options = {
  108.     "method": "POST",
  109.     "headers": {
  110.       "content-type": "application/json",
  111.       "apiKey": apiKey
  112.     },
  113.     "async": true,
  114.     "crossDomain": true,
  115.     "processData": false,
  116.     "payload": JSON.stringify(data)
  117.   }
  118.  
  119.   let response = UrlFetchApp.fetch(url, options);
  120.   let dataPars = JSON.parse(response.getContentText());
  121.  
  122.   sheetNP.getRange("G2").setValue(dataPars.data[0].DeliveryDate.date)
  123.   // console.log(dataPars.data[0].DeliveryDate.date)
  124.  
  125. }
  126. function getStatus() {
  127.  
  128.   let ttn = sheetNP.getRange("E2").getValue();
  129.  
  130.   let data = {
  131.     "modelName": "TrackingDocument",
  132.     "calledMethod": "getStatusDocuments",
  133.     "methodProperties": {
  134.       "Documents": [
  135.         {
  136.           "DocumentNumber": ttn.toString(),
  137.           "Phone": ""
  138.         }
  139.       ]
  140.     }
  141.   };
  142.  
  143.   let options = {
  144.     "method": "POST",
  145.     "headers": {
  146.       "content-type": "application/json",
  147.       "apiKey": apiKey
  148.     },
  149.     "async": true,
  150.     "crossDomain": true,
  151.     "processData": false,
  152.     "payload": JSON.stringify(data)
  153.   }
  154.  
  155.   let response = UrlFetchApp.fetch(url, options);
  156.   let dataPars = JSON.parse(response.getContentText());
  157.  
  158.   sheetNP.getRange("I2").setValue(dataPars.data[0].Status)
  159.   // console.log(dataPars.data[0].Status)
  160.  
  161. }
  162.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement