Skip to content

Instantly share code, notes, and snippets.

@takvol
Last active September 20, 2016 12:59
Show Gist options
  • Save takvol/aba2d33d850e682958db805edf5bb74b to your computer and use it in GitHub Desktop.
Save takvol/aba2d33d850e682958db805edf5bb74b to your computer and use it in GitHub Desktop.
Google Sheets custom function that returns status of the shipment serviced by "Nova Poshta" express delivery.
//Keep in mind that Nova Poshta may change tracking API method to require phone number
/**
* Returns current status of cargo.
*
* @param {number} invoice Invoice number.
* @return {string} cargo status information.
* @customfunction
*/
function getCargoStatus(invoice) {
if(!invoice || isNaN(invoice)) {
throw new Error("Bad value");
}
var apiKey = "your_api_key_goes_here";//To perform API requests Nova Poshta API key needed
var url = "http://testapi.novaposhta.ua/v2.0/en/documentsTracking/json/";
var payload, options, response;
payload = JSON.stringify({
"apiKey": apiKey,
"modelName": "TrackingDocument",
"calledMethod": "getStatusDocuments",
"methodProperties": {
"Language": "UA",
"Documents": [
{
"DocumentNumber": invoice,
"Phone": "380ХХХХХХХХХ"
}
]
}
});
options = {
"method": "post",
"contentType": "application/json",
"payload": payload
};
response = JSON.parse(UrlFetchApp.fetch(url, options));
if(!response.success) {
throw new Error(response.errors.toString());
}
return (response.data[0].Status);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment