如何在循环内等待Modal响应并持续处理联系人?
解决方案
1. 循环续跑逻辑修正
你的核心思路没问题,但续跑时的索引参数错误:当前resumeLeadProcessing调用processContacts时传入的是index,但该联系人已经完成编辑,应该从index + 1开始,否则会重复触发同一个联系人的编辑弹窗,陷入循环。
修改resumeLeadProcessing中的调用代码:
// 原代码 processContacts(leads, index); // 修改为 processContacts(leads, index + 1);
另外要确保utils_saveContactInfoAsProperty和utils_retrieveContactInfoForProcessing的可靠性:Apps Script的Properties服务有单条属性10KB、总存储50KB的限制,如果leads对象过大,建议将数据存在Spreadsheet的隐藏工作表或Drive JSON文件中,避免数据截断或丢失。
2. 调试google.script.run触发的服务端代码
可以在Apps Script控制台设置断点,但前端触发的异步调用无法直接触发断点,可通过以下方式调试:
- 在服务端函数(如
resumeLeadProcessing)中插入debugger;语句,保存后重新触发前端操作,函数执行到该语句时会自动打开调试面板。 - 手动硬编码参数,在脚本编辑器中直接运行服务端函数,先验证逻辑正确性,再联调前端。
3. 数据处理模型优化
Apps Script是无状态的,每次google.script.run调用都是全新的执行上下文,不能依赖内存对象,必须持久化状态:
- 避免存储完整的
leads对象,仅存储当前处理索引和必要的修改字段,原始leads数据从Spreadsheet或其他持久化源读取。 - 将处理进度存在Properties服务中,即使脚本意外中断,也能从上次位置恢复:
示例代码:
// 启动处理时读取进度 function startProcessing() { const userProps = PropertiesService.getUserProperties(); const currentIndex = parseInt(userProps.getProperty('processingIndex') || 0); const leads = getLeadsFromSpreadsheet(); // 从Spreadsheet读取原始数据 processContacts(leads, currentIndex); } // 恢复处理时更新进度 function resumeLeadProcessing(firstName, lastName) { try { var { contact, index, leads } = utils_retrieveContactInfoForProcessing(); // ... 联系人信息更新逻辑 ... // 更新进度 PropertiesService.getUserProperties().setProperty('processingIndex', index + 1); processContacts(leads, index + 1); } catch (e) { Logger.log('Error in resumeLeadProcessing: ' + e.message); } }
内容的提问来源于stack exchange,提问作者Assaf Yavnai
相关产品推荐
相关产品推荐

