Skip to content

Commit aa37f82

Browse files
authored
[excel] (Comments) Update comment sample to work when existing comments aren't present (#635)
* Update comment sample to work when existing comments aren't present * Replace let with const * Move statement getting existing comments outside the loop
1 parent 7362d1a commit aa37f82

File tree

3 files changed

+15
-18
lines changed

3 files changed

+15
-18
lines changed
Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: Add comments in Excel
33
description: Learn how to use Office Scripts to add comments in a worksheet.
4-
ms.date: 06/29/2021
4+
ms.date: 08/14/2023
55
ms.localizationpriority: medium
66
---
77

@@ -23,55 +23,52 @@ This sample shows how to add comments to a cell including [@mentioning](https://
2323

2424
## Sample Excel file
2525

26-
Download [excel-comments.xlsx](excel-comments.xlsx) for a ready-to-use workbook. Add the following script to try the sample yourself!
26+
Download [add-excel-comments.xlsx](add-excel-comments.xlsx) for a ready-to-use workbook. Add the following script to try the sample yourself!
2727

2828
## Sample code: Add comments
2929

3030
```TypeScript
3131
function main(workbook: ExcelScript.Workbook) {
3232
// Get the list of employees.
3333
const employees = workbook.getWorksheet('Employees').getUsedRange().getTexts();
34-
console.log(employees);
35-
34+
3635
// Get the schedule information from the schedule table.
3736
const scheduleSheet = workbook.getWorksheet('Schedule');
3837
const table = scheduleSheet.getTables()[0];
3938
const range = table.getRangeBetweenHeaderAndTotal();
4039
const scheduleData = range.getTexts();
4140

41+
// Find old comments, so we can delete them later.
42+
const oldCommentAddresses = scheduleSheet.getComments().map(oldComment => oldComment.getLocation().getAddress());
43+
4244
// Look through the schedule for a matching employee.
4345
for (let i = 0; i < scheduleData.length; i++) {
44-
let employeeId = scheduleData[i][3];
46+
const employeeId = scheduleData[i][3];
4547

4648
// Compare the employee ID in the schedule against the employee information table.
47-
let employeeInfo = employees.find(employeeRow => employeeRow[0] === employeeId);
49+
const employeeInfo = employees.find(employeeRow => employeeRow[0] === employeeId);
4850
if (employeeInfo) {
49-
console.log("Found a match " + employeeInfo);
50-
let adminNotes = scheduleData[i][4];
51+
const adminNotes = scheduleData[i][4];
52+
const commentCell = range.getCell(i, 5);
5153

52-
// Look for and delete old comments, so we avoid conflicts.
53-
let comment = workbook.getCommentByCell(range.getCell(i, 5));
54-
if (comment) {
54+
// Delete old comments, so we avoid conflicts.
55+
if (oldCommentAddresses.find(oldCommentAddress => oldCommentAddress === commentCell.getAddress())) {
56+
const comment = workbook.getCommentByCell(commentCell);
5557
comment.delete();
5658
}
5759

5860
// Add a comment using the admin notes as the text.
59-
workbook.addComment(range.getCell(i,5), {
61+
workbook.addComment(commentCell, {
6062
mentions: [{
6163
email: employeeInfo[1],
6264
id: 0, // This ID maps this mention to the `id=0` text in the comment.
6365
name: employeeInfo[2]
6466
}],
6567
richContent: `<at id=\"0\">${employeeInfo[2]}</at> ${adminNotes}`
66-
}, ExcelScript.ContentType.mention);
67-
68+
}, ExcelScript.ContentType.mention);
6869
} else {
6970
console.log("No match for: " + employeeId);
7071
}
7172
}
7273
}
7374
```
74-
75-
## Training video: Add comments
76-
77-
[Watch Sudhi Ramamurthy walk through this sample on YouTube](https://youtu.be/CpR78nkaOFw).
12.6 KB
Binary file not shown.
-11.7 KB
Binary file not shown.

0 commit comments

Comments
 (0)