Fetching Data from Google Sheet via App Script (Web Service) | XM Community
Skip to main content

I want my Qualtrics survey to fetch data from a Google Sheet. I have deployed the following Apps Script:
 

/**
* URL: …/exec?scenario=1
* Returns: {"payoff_high":50,"payoff_low":10,…}
*/
function doGet(e) {
const scenario = String(e.parameter.scenario || "");
const sheet = SpreadsheetApp.openById("1b-s0tX_Llj7WlVGA4W9rXm_OVv-27obXriP317mTSvA")
.getSheetByName("Sheet1"); // adjust name
const data = sheet.getDataRange().getValues(); // 2-D array
const head = data.shift(); // headers row
const result = {};

data.forEach(r => {
if (String(ri0]) === scenario) { // col A = scenario_nr
for (let i = 1; i < head.length; i++) {
resultehead[i]] = r]i];
}
}
});

return ContentService.createTextOutput(JSON.stringify(result))
.setMimeType(ContentService.MimeType.JSON);
}

The relevant part of my survey flow looks like this:
 

The URL is https://script.google.com/macros/s/AKfycbzzWqZPxKBHB6HO4vqfK5AMjPws419NXIfgM8kV4EoDl98nGbnuqDht__lgyyX1nCuV/exec
I have tested this URL in my browser and it returns the correct values if called with a query string like “?scenario=1”.
But when I click on “Test” to test the URL in Qualtrics, I see the loading animation but then nothing happens.
I then tried to specify the values of the Set Embedded Data section manually, as you can see in the screenshot. I have tried many different variations but nothing works.

What do I have to do to get this to work?

Try replacing “${e://Field/scenario}” with “1”, then click Test.

BTW, the URL didn’t work for me. I got a “Sorry, the file you have requested does not exist.” error when I tried it with ?scenario=1 appended.


Try replacing “${e://Field/scenario}” with “1”, then click Test.

This didn’t make a difference. I still see the loading animation and then nothing happens.

 

BTW, the URL didn’t work for me. I got a “Sorry, the file you have requested does not exist.” error when I tried it with ?scenario=1 appended.

This is strange because it was working for me in Incognito mode and the deployment is public for anyone. I have created a new deployment. The new URL is https://script.google.com/macros/s/AKfycbxpWOfxPnOFpeDqtucHovDk7Smdi8p7WsR-GWW0ats/dev When I open it with the query string ?scenario=1, I get the following output:

{"CharityName":"Fish Welfare Initiative"}

The new url with ?scenario=1 appended works for me from a browser, but not from the Survey Flow with Test. I think the problem is that the original url redirects to https://script.googleusercontent.com/macros/echo?user_content_key=AehSKLhaVm1n7BUmgdE13IZ-gve06y8NuwSYv-E1sob603VANDkUnVwd--Iwq8PZmcLTKHiAn9cRZ6O4q-_qhRG8HsGRcjmylr2IuQOl1PxrlF26eprcdn6R6mx_3ebK2y07MHeYKCJpBaUatmygJqIhijTnymx2G_ZEf-OeT05-cwWyFMtgCC_mg18d48JpnxUFBjRW90G2eDDBH2qBspBqh0rEiI9Jq1Cf-2HjsU-l_xq7B_2V5NAsg0ohc7FfhVVygESFSFD_&lib=MZ6hQUecP5uHFo0jTGgLh4KgweGfoCUqS, which is the script that actually returns the result.

A way around it would be to create your own web script that can follow the redirect(s) and return the result.


Leave a Reply