如何通过Google Apps Script提取Google Sheets中@提及人员的邮箱地址?
解决Google Sheets@提及人员的邮箱提取问题
核心方案:利用RichValue对象解析@提及数据
Google Sheets中的@提及属于内嵌富文本数据,可通过Apps Script的RichValue API直接提取关联用户信息,无需依赖已弃用的ContactsApp。
具体实现代码
- 获取目标单元格的富文本对象
const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("你的标签页名称"); const targetCell = sheet.getRange("A1"); // 替换为包含@提及的单元格位置 const richValue = targetCell.getRichValue();
- 提取@提及用户的邮箱信息
const userMentions = richValue.getUserMentions(); if (userMentions.length > 0) { userMentions.forEach(mention => { const userEmail = mention.getUser().getEmail(); const userName = mention.getUser().getName(); console.log(`提及用户:${userName},邮箱:${userEmail}`); // 可将结果写入指定单元格,示例: // sheet.getRange("B1").setValue(userEmail); }); }
注意事项
- 该API仅支持Google Workspace账号,个人免费版账号无法使用(@提及功能依赖Workspace组织通讯录)。
- 运行脚本时需授权
https://www.googleapis.com/auth/spreadsheets权限,确保权限通过。
替代方案:基于团队信息表的匹配查询
如果无法使用RichValue API(比如非Workspace账号),可直接通过你已有的团队信息表做匹配:
const teamSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("团队信息"); const targetName = "Peter Pan"; // 从@提及单元格提取的姓名文本 const teamData = teamSheet.getRange("A:B").getValues(); // 假设A列是姓名,B列是邮箱 const matchedRow = teamData.find(row => row[0] === targetName); if (matchedRow) { console.log(`匹配到邮箱:${matchedRow[1]}`); }
内容的提问来源于stack exchange,提问作者user7592671
相关产品推荐
相关产品推荐

