function doGet(e) { // Check if 'e' and 'e.parameter' exist to avoid errors if (!e || !e.parameter) { return ContentService .createTextOutput("No parameters received") .setMimeType(ContentService.MimeType.TEXT); } const regNo = (e.parameter.regNo || "").toString().trim(); const traineeName = (e.parameter.traineeName || "").toLowerCase().trim(); if (!regNo || !traineeName) { return ContentService .createTextOutput("Missing parameters: regNo and traineeName are required") .setMimeType(ContentService.MimeType.TEXT); } const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1"); const data = sheet.getDataRange().getValues(); const headers = data[0]; const rows = data.slice(1); const regIndex = headers.indexOf("Registration Number"); const nameIndex = headers.indexOf("Trainee Name"); if (regIndex === -1 || nameIndex === -1) { return ContentService .createTextOutput("Required columns (Registration Number, Trainee Name) missing in sheet") .setMimeType(ContentService.MimeType.TEXT); } const links = {}; rows.forEach(row => { if (row[regIndex].toString().trim() === regNo && row[nameIndex].toLowerCase().trim() === traineeName) { headers.forEach((header, i) => { if (header.toLowerCase().includes("link")) { links[header] = row[i]; } }); } }); if (Object.keys(links).length === 0) { return ContentService .createTextOutput("No matching record found") .setMimeType(ContentService.MimeType.TEXT); } return ContentService .createTextOutput(JSON.stringify(links)) .setMimeType(ContentService.MimeType.JSON); } // Test function to simulate a GET request inside the editor function testDoGet() { const e = { parameter: { regNo: "12345", traineeName: "John Doe" } }; const response = doGet(e); Logger.log(response.getContent()); }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter