Matching response to request using UrlFetchApp.fetchAll - google-apps-script

Im trying to validate bunch of urls that are stored in google spreadsheet using Google App Script. Since there are many urls to validate and UrlFetchApp.fetch does not execute http request in parallel, it becomes slow. So I would like to use UrlFetchApp.fetchAll, which works asynchronously.
But, there is problem:
var responses = UrlFetchApp.fetchAll(["http://www.bar.com", "http://www.foo.com"]);
responses[0] // Is this response from http://www.bar.com or http://www.foo.com
responses[1] // Is this response from http://www.bar.com or http://www.foo.com
Now responses is array of HTTPResponse objects. But to give user feedback, I would need to indicate responses that fail. But problem is that in response, there is no reference to request or URL. So problem is that I have array of responses but I'm unable to find URL this response came from. Responses array is not in same order as given to argument.

Related

Problem with UrlFetchApp.fetch in Google Apps Script

I am trying to use the ImportJSON.gs script to import data from my WooCommerce Web site directly into a Google Sheet. I have enabled the REST API in WooCommerce and am able to enter the URL directly into Chrome and get an accurate JSON response. The problem I am having is that when using UrlFetchApp.fetch, the data I get back suggests everything in the URL following a ? or & seems to be either truncated or reencoded to something else, but I haven't quite figured out what URL would give this response.
I have created this simple script to test it:
function myFunction() {
var response = UrlFetchApp.fetch("https://kidswonder.net/wp-json/wc-bookings/v1/products/slots?min_date=2023-01-30&max_date=2023-01-31&product_ids=16304");
Logger.log(response.getContentText());
}
The expected JSON response, which I get by entering the URL directly into a browser is:
{"records":[{"date":"2023-01-30T09:00","duration":30,"available":50,"booked":0,"product_id":16304},{"date":"2023-01-30T09:30","duration":30,"available":47,"booked":3,"product_id":16304},{"date":"2023-01-30T10:00","duration":30,"available":48,"booked":2,"product_id":16304},{"date":"2023-01-30T10:30","duration":30,"available":50,"booked":0,"product_id":16304},{"date":"2023-01-30T11:00","duration":30,"available":50,"booked":0,"product_id":16304},{"date":"2023-01-30T11:30","duration":30,"available":50,"booked":0,"product_id":16304},{"date":"2023-01-30T12:00","duration":30,"available":50,"booked":0,"product_id":16304},{"date":"2023-01-30T12:30","duration":30,"available":50,"booked":0,"product_id":16304},{"date":"2023-01-30T13:00","duration":30,"available":45,"booked":5,"product_id":16304},{"date":"2023-01-30T13:30","duration":30,"available":50,"booked":0,"product_id":16304},{"date":"2023-01-30T14:00","duration":30,"available":50,"booked":0,"product_id":16304},{"date":"2023-01-30T14:30","duration":30,"available":50,"booked":0,"product_id":16304},{"date":"2023-01-30T15:00","duration":30,"available":50,"booked":0,"product_id":16304},{"date":"2023-01-30T15:30","duration":30,"available":50,"booked":0,"product_id":16304}],"count":14}
But the actual response I get, which is the same as if I had is:
{"records":[{"date":"2023-02-02T09:00","duration":30,"available":1,"booked":0,"product_id":140},{"date":"2023-02-02T10:00","duration":30,"available":1,"booked":0,"product_id":140},{"date":"2023-02-02T14:00","duration":30,"available":1,"booked":0,"product_id":140},{"date":"2023-02-02T15:00","duration":30,"available":1,"booked":0,"product_id":140},{"date":"2023-02-05T17:00","duration":30,"available":1,"booked":0,"product_id":12314},{"date":"2023-02-05T17:00","duration":30,"available":1,"booked":0,"product_id":12312},{"date":"2023-02-06T10:00","duration":30,"available":1,"booked":0,"product_id":103024},{"date":"2023-02-06T10:00","duration":30,"available":1,"booked":0,"product_id":12312},{"date":"2023-02-06T11:00","duration":30,"available":1,"booked":0,"product_id":103024},{"date":"2023-02-06T11:00","duration":30,"available":1,"booked":0,"product_id":12312},{"date":"2023-02-06T12:00","duration":30,"available":1,"booked":0,"product_id":103024},{"date":"2023-02-06T12:00","duration":30,"available":1,"booked":0,"product_id":12312},{"date":"2023-02-06T13:00","duration":30,"available":1,"booked":0,"product_id":103024},{"date":"2023-02-06T13:00","duration":30,"available":1,"booked":0,"product_id":12312},{"date":"2023-02-06T14:00","duration":30,"available":1,"booked":0,"product_id":103024},{"date":"2023-02-06T14:00","duration":30,"available":1,"booked":0,"product_id":12312},{"date":"2023-02-06T15:00","duration":30,"available":1,"booked":0,"product_id":103024},{"date":"2023-02-06T15:00","duration":30,"available":1,"booked":0,"product_id":12312}],"count":18}

Unable to verify the challenge parameter for Slack API

I'm new to this and trying to figure out the basics. I want to use google apps script to receive requests from slack and send back information from a spreadsheet.
I am stuck at this step. EventsAPI - URL Verification. My approach was to have a doPost() function that returns the challenge. After I was able to confirm slack could connect to the app I would be able to build it out to send the required information from the spreadsheet.
function doPost(e){
return ContentService.createTextOutput(JSON.parse(e.postData.contents).challenge);
}
I expect the challenge to be successfully returned and the URL verified.
The error I receive in Slack is Request URL Your URL didn't respond with the value of the challenge parameter.

Google Apps Script: How to get number of active UrlFetchApp request (same as $.active in jQuery)?

I have a custom function which makes a UrlFetchApp request to an API. This custom function is called multiple times at once, so multiple UrlFetchApp request is made.
I want to know when all the request is completed or the number of active request.
Pseudo Code
function myFunction(param){
UrlFetchApp request
Check if any active request // I need this part of the code
do something
}
Thanks.

Retrieving JSON Data From a Private Google Spreadsheet

I have searched high and low, but I cannot find an answer to my question.
Is it possible to retrieve data from a private Google spreadsheet in JSON format using php?
I know how to authenticate to access the spreadsheet.
When I use this url: https://spreadsheets.google.com/feeds/list/$key/od6/private/full
I get results in xml, but when I try to add "?alt=json" no content is returned.
Is it possible to get the results returned as JSON?
One workaround is to use as a gateway between your GSheet and a custom JSON web API such as: https://apispark.com/docs/tutorials/google-spreadsheet

How to define headers in spreadsheet to get gsx$title attribute in JSON response from google spreadsheet API?

I am planning to use Google spreadsheet API to fetch data from a spreadsheet and render it in my mobile app using Sencha Touch. I have seen few blogs and tutorial which explain how to fetch and parse data but when I am trying the same I am not getting column wise data as attributes in entry object instead I am getting comma separated data in content tag.
To give you an example, here is sample spreadsheet in which I have change headers and data:
https://docs.google.com/spreadsheet/ccc?key=0AkEOCoTxRKgvdDRZUnViSFlOVUF0QUJybkZ6VlBwTkE#gid=0
and here is its feed:
https://spreadsheets.google.com/feeds/list/0AkEOCoTxRKgvdDRZUnViSFlOVUF0QUJybkZ6VlBwTkE/od6/public/values?alt=json
but when I created same spreadsheet by hand from scratch I am not getting gsx$name & gsx$processedlinkurl attribute in JSON. Here is that sheet:
https://docs.google.com/spreadsheet/ccc?key=0AgduEz3vmQUTdHZlSEI4bjN6VnZ5TVpRMFlyOVAtR0E#gid=0
Here is the JSON:
https://spreadsheets.google.com/feeds/list/0AgduEz3vmQUTdHZlSEI4bjN6VnZ5TVpRMFlyOVAtR0E/od6/public/basic?alt=json
To mark first row as header I have frozen first row in my sheet still JSON is not giving me header wise data in JSON.
OK I got the mistake, it is values instead of basic which is to be used in URL while accessing spreadsheet API to get all headers in result which means
Correct URL:
https://spreadsheets.google.com/feeds/list/0AgduEz3vmQUTdHZlSEI4bjN6VnZ5TVpRMFlyOVAtR0E/od6/public/values?alt=json
Wrong URL:
https://spreadsheets.google.com/feeds/list/0AgduEz3vmQUTdHZlSEI4bjN6VnZ5TVpRMFlyOVAtR0E/od6/public/basic?alt=json