基于姓氏与出生日期查询Google Sheet会员信息的代码修改求助
双条件(姓氏+出生日期)会员查询工具修改方案
一、修改后的 Code.gs 代码
function doGet() { return HtmlService.createHtmlOutputFromFile('Index'); } function getMemberInfo(lastName, dob) { const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('会员数据'); if (!sheet) return {headers: [], results: []}; const data = sheet.getDataRange().getValues(); const headers = data[0]; // 获取姓氏和出生日期对应的列索引 const lastNameColIndex = headers.indexOf('Last Name'); const dobColIndex = headers.indexOf('DoB'); // 统一日期格式为 YYYY-MM-DD,避免格式不匹配 const normalizedDob = new Date(dob).toISOString().split('T')[0]; // 双条件过滤:姓氏完全匹配 + 出生日期格式统一后匹配 const results = data.slice(1).filter(row => { const rowLastName = row[lastNameColIndex]; const rowDob = new Date(row[dobColIndex]).toISOString().split('T')[0]; return rowLastName === lastName && rowDob === normalizedDob; }); return {headers, results}; }
关键修改点
- 函数重命名为
getMemberInfo,明确功能,同时接收lastName和dob两个查询参数 - 添加工作表存在性检查,避免因表名错误抛出异常
- 新增日期格式统一逻辑,解决输入日期与表格内日期格式不一致导致的匹配失败问题
- 过滤逻辑改为同时校验姓氏和出生日期两个条件
二、修改后的 Index.html 代码
<!DOCTYPE html> <html> <head> <base target="_top"> <style> .search-form { margin: 20px; } .form-group { margin-bottom: 12px; } label { display: inline-block; width: 100px; font-size: 14px; } input { padding: 6px 8px; width: 220px; border: 1px solid #ddd; border-radius: 4px; } button { padding: 7px 16px; background: #2196F3; color: #fff; border: none; border-radius: 4px; cursor: pointer; } table { border-collapse: collapse; margin: 0 20px; } th, td { border: 1px solid #eee; padding: 8px 12px; text-align: left; } th { background-color: #f8f9fa; font-weight: 500; } .tip { color: #666; font-size: 13px; margin-top: 8px; } </style> </head> <body> <div class="search-form"> <div class="form-group"> <label for="lastName">姓氏:</label> <input type="text" id="lastName" placeholder="请输入会员姓氏" required> </div> <div class="form-group"> <label for="dob">出生日期:</label> <input type="date" id="dob" required> </div> <button onclick="searchMember()">查询会员</button> <p class="tip">请填写完整信息,系统将匹配对应会员数据</p> </div> <div id="results"></div> <script> function searchMember() { const lastName = document.getElementById('lastName').value.trim(); const dob = document.getElementById('dob').value; if (!lastName || !dob) { document.getElementById('results').innerHTML = '<p style="margin-left:20px;color:#dc3545;">请填写完整的姓氏和出生日期</p>'; return; } google.script.run.withSuccessHandler(displayResults).getMemberInfo(lastName, dob); } function displayResults(data) { const resultsDiv = document.getElementById('results'); if (data.results.length === 0) { resultsDiv.innerHTML = '<p style="margin-left:20px;">未找到匹配的会员信息</p>'; return; } let table = '<table><tr>'; data.headers.forEach(header => table += `<th>${header}</th>`); table += '</tr>'; data.results.forEach(row => { table += '<tr>'; row.forEach(cell => { // 格式化日期为本地可读格式 const isDate = !isNaN(new Date(cell)); table += `<td>${isDate ? new Date(cell).toLocaleDateString() : cell}</td>`; }); table += '</tr>'; }); table += '</table>'; resultsDiv.innerHTML = table; } </script> </body> </html>
关键修改点
- 新增出生日期输入框(使用
type="date",浏览器会自动提供日期选择器,简化输入) - 添加前端非空校验,避免无效查询
- 调用后端新的
getMemberInfo函数,传递两个查询参数 - 优化界面样式,提升易用性
- 结果展示时自动格式化日期,提升可读性
三、使用注意事项
- 确保Google Sheet的工作表名称为
会员数据,若表名不同,修改Code.gs中getSheetByName的参数 - 确保Sheet内列名准确对应
Last Name和DoB,若列名不同,修改Code.gs中indexOf的参数 - 测试时无需手动调整日期格式,前端输入的日期会自动转为标准格式,后端会统一表格内日期格式进行匹配
内容的提问来源于stack exchange,提问作者chandra
相关产品推荐
相关产品推荐

