1
1
---
2
2
title : Add comments in Excel
3
3
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
5
5
ms.localizationpriority : medium
6
6
---
7
7
@@ -23,55 +23,52 @@ This sample shows how to add comments to a cell including [@mentioning](https://
23
23
24
24
## Sample Excel file
25
25
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!
27
27
28
28
## Sample code: Add comments
29
29
30
30
``` TypeScript
31
31
function main(workbook : ExcelScript .Workbook ) {
32
32
// Get the list of employees.
33
33
const employees = workbook .getWorksheet (' Employees' ).getUsedRange ().getTexts ();
34
- console .log (employees );
35
-
34
+
36
35
// Get the schedule information from the schedule table.
37
36
const scheduleSheet = workbook .getWorksheet (' Schedule' );
38
37
const table = scheduleSheet .getTables ()[0 ];
39
38
const range = table .getRangeBetweenHeaderAndTotal ();
40
39
const scheduleData = range .getTexts ();
41
40
41
+ // Find old comments, so we can delete them later.
42
+ const oldCommentAddresses = scheduleSheet .getComments ().map (oldComment => oldComment .getLocation ().getAddress ());
43
+
42
44
// Look through the schedule for a matching employee.
43
45
for (let i = 0 ; i < scheduleData .length ; i ++ ) {
44
- let employeeId = scheduleData [i ][3 ];
46
+ const employeeId = scheduleData [i ][3 ];
45
47
46
48
// 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 );
48
50
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 ) ;
51
53
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 );
55
57
comment .delete ();
56
58
}
57
59
58
60
// Add a comment using the admin notes as the text.
59
- workbook .addComment (range . getCell ( i , 5 ) , {
61
+ workbook .addComment (commentCell , {
60
62
mentions: [{
61
63
email: employeeInfo [1 ],
62
64
id: 0 , // This ID maps this mention to the `id=0` text in the comment.
63
65
name: employeeInfo [2 ]
64
66
}],
65
67
richContent: ` <at id=\" 0\" >${employeeInfo [2 ]}</at> ${adminNotes } `
66
- }, ExcelScript .ContentType .mention );
67
-
68
+ }, ExcelScript .ContentType .mention );
68
69
} else {
69
70
console .log (" No match for: " + employeeId );
70
71
}
71
72
}
72
73
}
73
74
```
74
-
75
- ## Training video: Add comments
76
-
77
- [ Watch Sudhi Ramamurthy walk through this sample on YouTube] ( https://youtu.be/CpR78nkaOFw ) .
0 commit comments