コメントを含むユーザーに通知する
このサンプルでは、同僚を含むセルにコメント @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);
}
}
}
GitHub で Microsoft と共同作業する
このコンテンツのソースは GitHub にあります。そこで、issue や pull request を作成および確認することもできます。 詳細については、共同作成者ガイドを参照してください。
Office Scripts