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.
You are getting 'Sheet "Completion_Codes" not found'
because the active spreadsheet does not have a sheet named Completion_Codes
.
The sheet name parameter is case sensitive.
Something that might help you and others with the same problem is to log the sheet names using something like this:
function logSheetNames(){
SpreadsheetApp.getActiveSpreadsheet()
.getSheets()
.forEach(sheet => Logger.log(`"${sheet.getName()}"`);
}
The above code will add a log entry for each sheet. The sheet names will be quote enclosed.