Office 脚本示例方案:在 Teams 中安排面试
在此方案中,你是一名人力资源招聘人员,在 Teams 中安排与候选人的面试会议。 在 Excel 文件中管理候选人的面试日程。 你需要将 Teams 会议邀请发送给候选人和面试官。 然后,需要更新 Excel 文件,确认 Teams 会议已发送。
该解决方案有三个步骤,这些步骤合并到单个 Power Automate 流中。
- 脚本从表中提取数据,并将对象数组作为 JSON 数据返回。
- 然后,将数据发送到 Teams 创建 Teams 会议 操作以发送邀请。
- 相同的 JSON 数据将发送到另一个脚本,以更新邀请的状态。
有关使用 JSON 的详细信息,请阅读 使用 JSON 将数据传入 Office 脚本和从 Office 脚本传递数据。
涵盖的脚本技能
- Power Automate 流
- Teams 集成
- 表分析
设置说明
下载工作簿
将示例工作簿下载到 OneDrive。
在 Excel 中打开工作簿。
将至少一个电子邮件地址更改为你自己的电子邮件地址,以便接收邀请。
创建脚本
- 在“ 自动 ”选项卡下,选择“ 新建脚本 ”,并将以下脚本粘贴到编辑器中。 这将提取表数据以计划邀请。
function main(workbook: ExcelScript.Workbook): InterviewInvite[] {
const MEETING_DURATION = workbook.getWorksheet("Constants").getRange("B1").getValue() as number;
const MESSAGE_TEMPLATE = workbook.getWorksheet("Constants").getRange("B2").getValue() as string;
// Get the interview candidate information.
const sheet = workbook.getWorksheet("Interviews");
const table = sheet.getTables()[0];
const dataRows = table.getRangeBetweenHeaderAndTotal().getValues();
// Convert the table rows into InterviewInvite objects for the flow.
let invites: InterviewInvite[] = [];
dataRows.forEach((row) => {
const inviteSent = row[1] as boolean;
if (!inviteSent) {
const startTime = new Date(Math.round(((row[6] as number) - 25569) * 86400 * 1000));
const finishTime = new Date(startTime.getTime() + MEETING_DURATION * 60 * 1000);
const candidateName = row[2] as string;
const interviewerName = row[4] as string;
invites.push({
ID: row[0] as string,
Candidate: candidateName,
CandidateEmail: row[3] as string,
Interviewer: row[4] as string,
InterviewerEmail: row[5] as string,
StartTime: startTime.toISOString(),
FinishTime: finishTime.toISOString(),
Message: generateInviteMessage(MESSAGE_TEMPLATE, candidateName, interviewerName)
});
}
});
console.log(JSON.stringify(invites));
return invites;
}
function generateInviteMessage(
messageTemplate: string,
candidate: string,
interviewer: string) : string {
return messageTemplate.replace("_Candidate_", candidate).replace("_Interviewer_", interviewer);
}
// The interview invite information.
interface InterviewInvite {
ID: string
Candidate: string
CandidateEmail: string
Interviewer: string
InterviewerEmail: string
StartTime: string
FinishTime: string
Message: string
}
将脚本命名为流的 “计划采访 ”。
使用以下代码创建另一个新脚本。 这会将行标记为已邀请。
function main(workbook: ExcelScript.Workbook, invites: InterviewInvite[]) {
const table = workbook.getWorksheet("Interviews").getTables()[0];
// Get the ID and Invite Sent columns from the table.
const idColumn = table.getColumnByName("ID");
const idRange = idColumn.getRangeBetweenHeaderAndTotal().getValues();
const inviteSentColumn = table.getColumnByName("Invite Sent?");
const dataRowCount = idRange.length;
// Find matching IDs to mark the correct row.
for (let row = 0; row < dataRowCount; row++){
let inviteSent = invites.find((invite) => {
return invite.ID == idRange[row][0] as string;
});
if (inviteSent) {
inviteSentColumn.getRangeBetweenHeaderAndTotal().getCell(row, 0).setValue(true);
console.log(`Invite for ${inviteSent.Candidate} has been sent.`);
}
}
}
// The interview invite information.
interface InterviewInvite {
ID: string
Candidate: string
CandidateEmail: string
Interviewer: string
InterviewerEmail: string
StartTime: string
FinishTime: string
Message: string
}
- 将第二个脚本命名为流的 “记录已发送邀请 ”。
创建 Power Automate 流
此流运行面试计划脚本、发送 Teams 会议,并将活动记录回工作簿中。
创建新的 即时云流。
选择 “手动触发流 ”,然后选择“ 创建”。
在流生成器中 + ,选择按钮和 “添加操作”。 使用 Excel Online (Business) 连接器的 “运行脚本” 操作。 使用以下值完成操作。
- 位置:OneDrive for Business
- 文档库:OneDrive
- 文件:hr-interviews.xlsx (通过文件浏览器) 选择
- 脚本:计划面试
添加使用 Microsoft Teams 连接器的 “创建 Teams 会议 ”操作的操作。 从 Excel 连接器中选择动态内容时,将为流生成 For each 块。 使用以下值完成连接器。
- 主题:Contoso 采访
- 消息:运行脚本) 的消息 (动态内容
- 时区:太平洋标准时间
- 开始时间: StartTime (运行脚本) 的动态内容
- 结束时间: FinishTime (运行脚本) 中的动态内容
- 日历 ID:日历
- 所需与会者: CandidateEmail ; InterviewerEmail (运行脚本 中的动态内容 - 请注意“;”,将值分隔)
在同一个 For each 块中,添加另一个 “运行脚本” 操作。 使用以下值。
- 位置:OneDrive for Business
- 文档库:OneDrive
- 文件:hr-interviews.xlsx (通过文件浏览器) 选择
- 脚本:录制已发送的邀请
- 邀请:运行脚本) 的结果 (动态内容
- 首先按 “切换到整个数组的输入 ”。
保存流。 流设计器应如下图所示。
使用流编辑器页上的“ 测试 ”按钮,或通过“我的流”选项卡运行 流 。请务必在出现提示时允许访问。
培训视频:从 Excel 数据发送 Teams 会议
观看 Sudhi Ramamurthy 在 YouTube 上演练此示例的一个版本。 他的版本使用更可靠的脚本来处理更改列和过时的会议时间。