使用批注通知人员
此示例演示如何向单元格添加批注,包括 @mentioning 同事。
示例方案
团队主管负责维护轮班计划。 他们将员工 ID 分配给班次记录。 如果团队主管希望通知员工,他们添加该员工的注释 @mentions 。 员工收到来自工作表的自定义消息的电子邮件。 随后,员工可以查看工作簿,并在方便时回复评论。
解决方案
- 该脚本从员工工作表中提取员工信息。
- 然后,该脚本添加注释 (将相关员工电子邮件) 添加到轮班记录中的相应单元格。
- 在添加新批注之前,将删除单元格中的现有注释。
设置:示例 Excel 文件
此工作簿包含脚本所需的数据、对象和格式设置。
示例代码:添加注释
将以下脚本添加到示例工作簿,并亲自尝试该示例!
function main(workbook: ExcelScript.Workbook) {
// Get the list of employees.
const employees = workbook.getWorksheet('Employees').getUsedRange().getTexts();
// Get the schedule information from the schedule table.
const scheduleSheet = workbook.getWorksheet('Schedule');
const table = scheduleSheet.getTables()[0];
const range = table.getRangeBetweenHeaderAndTotal();
const scheduleData = range.getTexts();
// Find old comments, so we can delete them later.
const oldCommentAddresses = scheduleSheet.getComments().map(oldComment => oldComment.getLocation().getAddress());
// Look through the schedule for a matching employee.
for (let i = 0; i < scheduleData.length; i++) {
const employeeId = scheduleData[i][3];
// Compare the employee ID in the schedule against the employee information table.
const employeeInfo = employees.find(employeeRow => employeeRow[0] === employeeId);
if (employeeInfo) {
const adminNotes = scheduleData[i][4];
const commentCell = range.getCell(i, 5);
// Delete old comments, so we avoid conflicts.
if (oldCommentAddresses.find(oldCommentAddress => oldCommentAddress === commentCell.getAddress())) {
const comment = workbook.getCommentByCell(commentCell);
comment.delete();
}
// Add a comment using the admin notes as the text.
workbook.addComment(commentCell, {
mentions: [{
email: employeeInfo[1],
id: 0, // This ID maps this mention to the `id=0` text in the comment.
name: employeeInfo[2]
}],
richContent: `<at id=\"0\">${employeeInfo[2]}</at> ${adminNotes}`
}, ExcelScript.ContentType.mention);
} else {
console.log("No match for: " + employeeId);
}
}
}