如何为Google Sheets查询脚本添加符合条件记录的上下切换功能?
实现物料借还记录的多结果切换功能
解决方案概述
要解决单条记录限制、实现多记录切换,需要完成三个核心操作:
- 一次性查询并缓存所有符合条件的记录
- 跟踪当前显示的记录索引
- 编写切换函数处理上下翻页逻辑
完整实现代码
// 搜索所有匹配记录并显示第一条 function searchRecord() { const ss = SpreadsheetApp.getActiveSpreadsheet(); const formSheet = ss.getSheetByName("Teacher's Input Form"); const dbSheet = ss.getSheetByName("Borrow/Return Database"); const userName = formSheet.getRange("C7").getValue(); const itemName = formSheet.getRange("C9").getValue(); if (!userName || !itemName) { SpreadsheetApp.getUi().alert("请输入姓名和物品名称"); return; } const allRecords = dbSheet.getDataRange().getValues(); const matchedRecords = []; // 遍历数据库收集所有匹配记录 for (const row of allRecords) { if (row[1] === userName && row[2] === itemName) { matchedRecords.push(row); } } const props = PropertiesService.getScriptProperties(); if (matchedRecords.length === 0) { props.deleteProperty("matchedRecords"); props.deleteProperty("currentIndex"); SpreadsheetApp.getUi().alert("No record found!"); return; } // 缓存匹配记录与初始索引 props.setProperty("matchedRecords", JSON.stringify(matchedRecords)); props.setProperty("currentIndex", 0); // 渲染第一条记录到表单 displayRecord(matchedRecords[0]); } // 将指定记录渲染到输入表单 function displayRecord(record) { const formSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Teacher's Input Form"); formSheet.getRange("C4").setValue(record[0]); formSheet.getRange("C11").setValue(record[3]); formSheet.getRange("C13").setValue(record[4]); formSheet.getRange("C15").setValue(record[5]); formSheet.getRange("C17").setValue(record[6]); } // 切换到上一条记录 function prevRecord() { const props = PropertiesService.getScriptProperties(); const matchedRecordsStr = props.getProperty("matchedRecords"); const currentIndex = parseInt(props.getProperty("currentIndex")) || 0; if (!matchedRecordsStr) { SpreadsheetApp.getUi().alert("请先执行搜索"); return; } const matchedRecords = JSON.parse(matchedRecordsStr); if (currentIndex <= 0) { SpreadsheetApp.getUi().alert("已经是第一条记录"); return; } const newIndex = currentIndex - 1; props.setProperty("currentIndex", newIndex); displayRecord(matchedRecords[newIndex]); } // 切换到下一条记录 function nextRecord() { const props = PropertiesService.getScriptProperties(); const matchedRecordsStr = props.getProperty("matchedRecords"); const currentIndex = parseInt(props.getProperty("currentIndex")) || 0; if (!matchedRecordsStr) { SpreadsheetApp.getUi().alert("请先执行搜索"); return; } const matchedRecords = JSON.parse(matchedRecordsStr); if (currentIndex >= matchedRecords.length - 1) { SpreadsheetApp.getUi().alert("已经是最后一条记录"); return; } const newIndex = currentIndex + 1; props.setProperty("currentIndex", newIndex); displayRecord(matchedRecords[newIndex]); }
关键逻辑说明
- 记录缓存:使用
PropertiesService.getScriptProperties()存储匹配记录数组和当前索引,解决Google Apps Script函数执行无状态的问题。 - 搜索逻辑优化:遍历数据库所有行,收集所有匹配记录,而非找到第一条就终止。
- 渲染逻辑抽离:将记录渲染到表单的逻辑单独封装为
displayRecord,避免重复代码。 - 边界处理:切换函数中加入边界判断,防止索引越界,并给出对应提示。
按钮绑定操作
- 打开Google Sheets,点击「插入」→「绘图」,绘制上下箭头形状作为按钮。
- 点击绘制好的按钮,选择「分配脚本」,分别为上箭头绑定
prevRecord、下箭头绑定nextRecord。
内容的提问来源于stack exchange,提问作者user20403203
相关产品推荐
相关产品推荐

