I’m trying to connect a Google Sheet to Qualtrics. And keep running into this error: Sheet “Completion_Codes” not found.
Here’s the script:
function doGet() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Completion_Codes");
if (!sheet) {
Logger.log('Sheet "Completion_Codes" not found');
throw new Error('Spreadsheet not found');
}
var data = sheet.getDataRange().getValues();
var unusedCodes = [];
for (var i = 1; i < data.length; i++) {
if (data[i][1] !== 'Yes') {
unusedCodes.push(data[i][0]);
}
}
if (unusedCodes.length === 0) {
return ContentService.createTextOutput(JSON.stringify({ "completionCode": "No more codes available" })).setMimeType(ContentService.MimeType.JSON);
}
var randomIndex = Math.floor(Math.random() * unusedCodes.length);
var selectedCode = unusedCodes[randomIndex];
for (var i = 1; i < data.length; i++) {
if (data[i][0] === selectedCode) {
sheet.getRange(i + 1, 2).setValue('Yes');
break;
}
}
return ContentService.createTextOutput(JSON.stringify({ "completionCode": selectedCode })).setMimeType(ContentService.MimeType.JSON);
}
I have verified the name multiple times, even made sure I’m on the “active sheet” but nothing is working atm.
New contributor
RB39 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.