Share via


Notificar a las personas con comentarios

En este ejemplo se muestra cómo agregar comentarios a una celda, incluido @mentioning un compañero.

Escenario de ejemplo

El responsable del equipo mantiene la programación de turnos. Asignan un identificador de empleado al registro de turnos. Si el jefe del equipo desea notificar al empleado, agrega un comentario que @mentions el empleado. El empleado se envía por correo electrónico con un mensaje personalizado de la hoja de cálculo. Posteriormente, el empleado puede ver el libro y responder al comentario a su conveniencia.

Solución

  1. El script extrae información del empleado de la hoja de cálculo del empleado.
  2. A continuación, el script agrega un comentario (incluido el correo electrónico del empleado pertinente) a la celda adecuada del registro de turnos.
  3. Los comentarios existentes en la celda se quitan antes de agregar el nuevo comentario.

Configuración: Archivo de Excel de ejemplo

Este libro contiene los datos, los objetos y el formato esperados por el script.

Código de ejemplo: Agregar comentarios

Agregue el siguiente script al libro de ejemplo y pruebe el ejemplo usted mismo.

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);
    }
  }
}