ISmartTagExtension (Interfaz)

Representa una extensión de una etiqueta inteligente de una hoja de cálculo de Excel que se personaliza usando las herramientas de desarrollo de Office incluidas en Visual Studio. La extensión define un reconocedor personalizado para la etiqueta inteligente.

Espacio de nombres:  Microsoft.Office.Tools.Excel
Ensamblado:  Microsoft.Office.Tools.Excel (en Microsoft.Office.Tools.Excel.dll)

Sintaxis

'Declaración
<GuidAttribute("baa275c5-e25c-49ba-87be-dbe0712f7eb1")> _
Public Interface ISmartTagExtension _
    Inherits IExtension
[GuidAttribute("baa275c5-e25c-49ba-87be-dbe0712f7eb1")]
public interface ISmartTagExtension : IExtension

El tipo ISmartTagExtension expone los siguientes miembros.

Propiedades

  Nombre Descripción
Propiedad pública ExtensionBase Obtiene el objeto que esta interfaz IExtension está extendiendo. (Se hereda de IExtension).

Arriba

Métodos

  Nombre Descripción
Método público Recognize Busca texto en la celda de términos reconocidos.

Arriba

Comentarios

Implemente la interfaz ISmartTagExtension si desea controlar cómo Excel reconoce las etiquetas inteligentes en hojas de cálculo. Para obtener más información, vea Arquitectura de las etiquetas inteligentes y Cómo: Crear etiquetas inteligentes con reconocedores personalizados en Excel y .NET Framework 4.

Uso

Este tipo está pensado para su uso sólo en proyectos para Excel 2007. Las etiquetas inteligentes están desusadas en Excel 2010. Para obtener más información, vea Información general sobre etiquetas inteligentes.

Ejemplos

En el ejemplo de código siguiente se muestra cómo se implementa ISmartTagExtension para crear su propio reconocedor de etiquetas inteligentes. En el ejemplo se implementa el método Recognize para reconocer cada término de etiqueta inteligente encontrado en una celda. En este ejemplo se supone que se ha agregado una referencia a Microsoft.Office.Interop.SmartTag desde la pestaña .NET del cuadro de diálogo Agregar referencia.

Imports System
Imports System.Windows.Forms
Imports Microsoft.Office.Interop.SmartTag
Imports Microsoft.Office.Tools.Excel

Public Class CustomSmartTag
    Implements ISmartTagExtension

    ' Declare the smart tag.
    Private smartTagDemo As Microsoft.Office.Tools.Excel.SmartTag

    ' Declare actions for this smart tag.
    WithEvents Action1 As Microsoft.Office.Tools.Excel.Action
    WithEvents Action2 As Microsoft.Office.Tools.Excel.Action

    Public Sub New()
        Me.smartTagDemo = Globals.Factory.CreateSmartTag(
            "https://www.contoso.com/Demo#DemoSmartTag", "Custom Smart Tag", Me)

        Action1 = Globals.Factory.CreateAction("Display property value")
        Action2 = Globals.Factory.CreateAction("Display smart tag details")

        smartTagDemo.Terms.AddRange(New String() {"sales", "organization"})
        smartTagDemo.Actions = New Microsoft.Office.Tools.Excel.Action() {Action1, Action2}
    End Sub

    Private Sub Recognize(ByVal text As String, 
        ByVal site As ISmartTagRecognizerSite, ByVal tokenList As ISmartTagTokenList, 
        ByVal context As SmartTagRecognizeContext) Implements ISmartTagExtension.Recognize

        ' Determine whether each smart tag term exists in the document text.
        Dim Term As String
        For Each Term In smartTagDemo.Terms

            ' Search the cell text for the first instance of 
            ' the current smart tag term.
            Dim index As Integer = context.CellText.IndexOf(Term, 0)

            If (index >= 0) Then

                ' Create a smart tag token and a property bag for the 
                ' recognized term.
                Dim propertyBag As ISmartTagProperties = site.GetNewPropertyBag()

                ' Write a new property value.
                Dim key As String = "Key1"
                propertyBag.Write(key, DateTime.Now)

                ' Attach the smart tag to the term in the document.
                context.PersistTag(propertyBag)

                ' This implementation only finds the first instance
                ' of a smart tag term in the cell. 
                Exit For
            End If
        Next
    End Sub

    ' This action displays the property value for the term.
    Private Sub Action1_Click(ByVal sender As Object,
        ByVal e As Microsoft.Office.Tools.Excel.ActionEventArgs) Handles Action1.Click

        Dim propertyBag As ISmartTagProperties = e.Properties
        Dim key As String = "Key1"
        MessageBox.Show("The corresponding value of " & key & " is: " &
            propertyBag.Read(key))
    End Sub

    ' This action displays smart tag details.
    Private Sub Action2_Click(ByVal sender As Object,
        ByVal e As Microsoft.Office.Tools.Excel.ActionEventArgs) Handles Action2.Click
        MessageBox.Show("The current smart tag caption is '" &
        smartTagDemo.Caption & "'. The current smart tag type is '" &
        smartTagDemo.SmartTagType & "'.")
    End Sub

    Public ReadOnly Property Base() As Microsoft.Office.Tools.Excel.SmartTag
        Get
            Return smartTagDemo
        End Get
    End Property

    Public ReadOnly Property ExtensionBase() As Object Implements ISmartTagExtension.ExtensionBase
        Get
            Return smartTagDemo
        End Get
    End Property
End Class
using System;
using System.Windows.Forms;
using Microsoft.Office.Interop.SmartTag;
using Microsoft.Office.Tools.Excel;

namespace Trin_ExcelDerivedSmartTags4
{
    class CustomSmartTag : ISmartTagExtension
    {
        // Declare the smart tag.
        Microsoft.Office.Tools.Excel.SmartTag smartTagDemo;

        // Declare actions for this smart tag.
        private Microsoft.Office.Tools.Excel.Action Action1;
        private Microsoft.Office.Tools.Excel.Action Action2;

        public CustomSmartTag()
        {
            this.smartTagDemo = Globals.Factory.CreateSmartTag(
                "https://www.contoso.com/Demo#DemoSmartTag", "Custom Smart Tag", this);

            Action1 = Globals.Factory.CreateAction("Display property value");
            Action2 = Globals.Factory.CreateAction("Display smart tag details");

            smartTagDemo.Terms.AddRange(new string[] { "sales", "organization" });
            smartTagDemo.Actions = new Microsoft.Office.Tools.Excel.Action[] { 
                Action1, Action2 };

            Action1.Click += new ActionClickEventHandler(Action1_Click);
            Action2.Click += new ActionClickEventHandler(Action2_Click);
        }

        void ISmartTagExtension.Recognize(string text, ISmartTagRecognizerSite site, 
            ISmartTagTokenList tokenList, SmartTagRecognizeContext context)
        {

            // Determine whether each smart tag term exists in the document text.
            foreach (string term in smartTagDemo.Terms)
            {
                // Search the cell text for the first instance of the current smart tag term.
                int index = context.CellText.IndexOf(term, 0);

                if (index >= 0)
                {
                    // Create a smart tag token and a property bag for the recognized term.
                    ISmartTagProperties propertyBag = site.GetNewPropertyBag();

                    // Write a new property value.                 
                    string key = "Key1";
                    propertyBag.Write(key, DateTime.Now.ToString());

                    // Attach the smart tag to the term in the document
                    context.PersistTag(propertyBag);

                    // This implementation only finds the first instance of a 
                    // smart tag term in the cell. 
                    break;
                }
            }
        }

        // This action displays the property value for the term.
        private void Action1_Click(object sender,
            Microsoft.Office.Tools.Excel.ActionEventArgs e)
        {
            ISmartTagProperties propertyBag = e.Properties;
            string key = "Key1";
            MessageBox.Show("The corresponding value of " + key +
                " is: " + propertyBag.get_Read(key));
        }

        // This action displays smart tag details.
        private void Action2_Click(object sender,
            Microsoft.Office.Tools.Excel.ActionEventArgs e)
        {
            MessageBox.Show("The current smart tag caption is '" +
                smartTagDemo.Caption + "'. The current smart tag type is '" +
                smartTagDemo.SmartTagType + "'.");
        }

        public Microsoft.Office.Tools.Excel.SmartTag Base
        {
            get { return smartTagDemo; }
        }

        public object ExtensionBase
        {
            get { return smartTagDemo; }
        }
    }

Vea también

Referencia

Microsoft.Office.Tools.Excel (Espacio de nombres)

Otros recursos

Arquitectura de las etiquetas inteligentes

Cómo: Crear etiquetas inteligentes con reconocedores personalizados en Excel y .NET Framework 4