Download Important Documents of the Trainees (Code.gs in Sheet Appscript) (Search with Reg No)

PHOTO EMBED

Sat Aug 09 2025 15:57:28 GMT+0000 (Coordinated Universal Time)

Saved by @master00001

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());
}
content_copyCOPY

Insert in app script, then deploy as webapp. Execute as me, anyone has access. Copy the webapp url and insert it in html code.