You use dereference notation. You have given your ‘call a script’ task a name, I always use “script” to make it easy, and you would have also named the returned values in the return values config section. So then in a subsequent task you’d use [script].[value]
In the example I provided, instead of returning an object - which allows you to include multiple data types, but it can be more technical in the implementation - I instead return an array**.**
If you return an array, all types have to be the same; this is why in the script example, you can see I’m converting things to a string
If you return an array, then your dot-notation in subsequent tasks to get returned values is:
[task_name].[output]
Same for any regular return value as well
Best option:
Copy the contents, go to ChatGPT, paste and ask it to help you. ?
This is the structure of the returned object from OpenAi API. I know how to set up this stuff in the code because the documentation for the API tells me how things will be structured; from this I can then build the dot-notation necessary to extract the specific element I want.
It should be mentioned that returning an object will be better in the long run.
But if you’re not comfortable with JSON objects yet, working with an array might be easier to get things started.
In the long run though… returning a JSON object will be preferable, as you can skip the type conversion to string and just return the native types (array, number, text, etc.).
And the next step where I use the outputs to set values of a new row in another table (Courses) than the one (Events) that triggered the bot (might be something useful… )
And here is the Script itself :
function getDistDur(xOrigin, xDestination, xarriveDateTime, apiKey) {
const xarriveDT = new Date(xarriveDateTime).toISOString();
try {
// Convert arrival time to a Unix timestamp in seconds
const arrivalTimestamp = Math.floor(new Date(xarriveDT).getTime() / 1000);
// Define the URL with parameters for Distance Matrix API
const url = `https://maps.googleapis.com/maps/api/distancematrix/json?units=metric&origins=${encodeURIComponent(xOrigin)}&destinations=${encodeURIComponent(xDestination)}&departure_time=${arrivalTimestamp}&mode=driving&traffic_model=best_guess&key=${apiKey}`;
// Fetch the response
const response = UrlFetchApp.fetch(url);
const data = JSON.parse(response.getContentText());
if (data.rows[0].elements[0].status === "OK") {
const element = data.rows[0].elements[0];
const distanceMetre = element.distance.value;
const distanceTime = element.duration_in_traffic.value;
// Return the output in the desired format
return {
"distanceTime": Number(distanceTime),
"distanceMetre": Number(distanceMetre)
};
} else {
throw new Error("No route found");
}
} catch (err) {
console.log(err.stack);
return {
"distanceTime": 0, // Adjusted default for error case
"distanceMetre": 0 // Adjusted default for error case
};
}
}
Looks like you’ve mixed up your variable names. You use xDistanceTime in the script but then rename it to distanceTime in the task config return value. The latter is how you should refer to it later, with [call DistDur retour].[distanceTime] which is not what you said you tried.