You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

请求实现:通过学生输入Unique ID从Google Sheets检索展示对应信息

学生ID查询Google Sheets信息的简易实现方案

一、先整理你的Google Sheets表格

  • 确保表格有清晰的列结构:A列存Unique ID,B列firstName,C列lastName,D列Group
  • 给目标工作表命名为StudentData(脚本里会用到这个名称,也可以自行修改,但要和代码对应)

二、编写Google Apps Script

打开你的Google Sheet,点击「工具」→「脚本编辑器」,分别创建两个文件:

1. 后端逻辑文件(Code.gs)

// 加载前端交互页面
function doGet() {
  return HtmlService.createHtmlOutputFromFile('Index');
}

// 根据Unique ID查询学生信息
function getStudentData(uniqueId) {
  const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('StudentData');
  // 用文本查找器优化大表查询效率
  const matchCell = sheet.getRange('A:A').createTextFinder(uniqueId).matchEntireCell(true).findNext();
  
  if (matchCell) {
    const row = matchCell.getRow();
    return {
      firstName: sheet.getRange(row, 2).getValue(),
      lastName: sheet.getRange(row, 3).getValue(),
      group: sheet.getRange(row, 4).getValue()
    };
  }
  return null; // 无匹配结果时返回null
}

2. 前端交互页面(Index.html)

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
    <style>
      body { font-family: Arial, sans-serif; max-width: 400px; margin: 2rem auto; padding: 0 1rem; }
      .input-group { margin-bottom: 1rem; }
      label { display: block; margin-bottom: 0.5rem; }
      input { width: 100%; padding: 0.5rem; box-sizing: border-box; border: 1px solid #ddd; border-radius: 4px; }
      button { padding: 0.5rem 1rem; background: #4285F4; color: white; border: none; border-radius: 4px; cursor: pointer; }
      #result { margin-top: 1.5rem; padding: 1rem; border-radius: 4px; }
      .success { background: #E8F5E9; border: 1px solid #C8E6C9; }
      .error { background: #FFEBEE; border: 1px solid #FFCDD2; }
    </style>
  </head>
  <body>
    <h2>学生信息查询</h2>
    <div class="input-group">
      <label for="uniqueId">Unique ID:</label>
      <input type="text" id="uniqueId" placeholder="请输入你的ID">
    </div>
    <button onclick="searchStudent()">查询</button>
    <div id="result"></div>

    <script>
      function searchStudent() {
        const uniqueId = document.getElementById('uniqueId').value.trim();
        const resultDiv = document.getElementById('result');
        
        if (!uniqueId) {
          resultDiv.innerHTML = '<p>请输入有效的Unique ID</p>';
          resultDiv.className = 'error';
          return;
        }
        
        // 调用后端脚本查询数据
        google.script.run
          .withSuccessHandler(data => {
            if (data) {
              resultDiv.innerHTML = `
                <p><strong>姓名:</strong> ${data.firstName} ${data.lastName}</p>
                <p><strong>分组:</strong> ${data.group}</p>
              `;
              resultDiv.className = 'success';
            } else {
              resultDiv.innerHTML = '<p>未找到匹配的学生信息</p>';
              resultDiv.className = 'error';
            }
          })
          .withFailureHandler(error => {
            resultDiv.innerHTML = `<p>查询出错: ${error.message}</p>`;
            resultDiv.className = 'error';
          })
          .getStudentData(uniqueId);
      }
    </script>
  </body>
</html>

三、部署为可访问的Web应用

  1. 在脚本编辑器中,点击「部署」→「新部署」
  2. 部署类型选择「Web应用」
  3. 配置选项:
    • 执行:选择「我」
    • 谁可以访问:选择「任何人,甚至匿名」(如果需要限制校内访问,可选择「组织内任何人」)
  4. 点击「部署」,授权后复制生成的URL,学生就能通过这个网页输入ID查询信息

四、优化提示

  • 如果表格数据量很大(上千行),文本查找器的效率比遍历所有行高很多,推荐使用上述createTextFinder方案
  • 确保Sheet中的Unique ID是文本格式,避免数字类型ID出现匹配误差(比如前置0被自动去除)

内容的提问来源于stack exchange,提问作者NoClueScriptWriter

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.12 14:05:56