Use Lotus script to flexibly operate rich text fields in Lotus Notes-Alibaba Cloud

Chen Bin, Senior Software Engineer, IBM Chen Yun, software engineer, IBM Introduction: This article describes how to use LotusScript for flexible operations. Lotus Notes Rich Text domain (Rich Text Field), and provides several examples Program . Users are required to have LotusScript. Programming Experience and proficiency in using Lotus Domino designer.

Introduction

For a long time, rich text fields of Lotus Notes are frequently used. Rich Text fields are used in almost any domino application. Rich Text fields of Lotus Notes are also very powerful. In addition to common text, they also support images, tables, embedded objects, HTTP links, notes links, attachments, and many other types. However, there is a problem that has long plagued LotusScript. it is difficult for developers to flexibly operate various types of content in the Notes Rich Text domain. In fact, many new Lotus scripts have been added during the constant upgrade of Domino. class to operate the rich text fields of notes, but many Lotus scripts. developers are not familiar with this. This article describes how to use these classes to flexibly operate rich text fields.

Lotus script. Class Related to rich text fields in notes

Lotus script. classes related to the operations on the rich text fields of notes include:

  • Notesrichtextnavigator: the navigator of rich text fields, used to access various elements in rich text fields;
  • Notesrichtextrange indicates a range of rich text fields, which can contain multiple elements;
  • Notesrichtextdoclink indicates the document link of Rich Text domain content;
  • Notesembeddedobject Embedded Object or file attachment;
  • Notesrichtextsection indicates a section in the rich text field;
  • Notesrichtexttable indicates a table in the rich text field;
  • Notesrichtextstyle. It indicates various attributes of rich text;
  • Notesrichtextparagraphstyle. It indicates the attributes of rich text paragraphs;
  • Notescolorobject indicates a color.

Example

Next we will use a program to analyze the usage of each class.

First, create a blank Domino application and create a form named "test", which only contains a rich text field named "body, create an action named "test" in the default view ",CodeAs follows. This program generates a document that contains a rich text field and various elements in the rich text field.

Listing 1. Generating various elements

Sub Click(Source As Button)
    Dim s As New NotesSession
    Dim db As NotesDatabase
    Dim doc As NotesDocument
    Set db = s.CurrentDatabase
    Set doc = New NotesDocument(db)
    doc.Form. = "test"
    Dim rtf As NotesRichTextItem
    Set rtf = doc.CreateRichTextItem ("Body")
    
    'Generate a text paragraph and set its font size, color and other attributes
    Dim style. As NotesRichTextStyle. Set style. = s.CreateRichTextStyle. Dim pstyle. As NotesRichTextParagraphStyle. Set pstyle. = s.CreateRichTextParagraphStyle. Dim color As NotesColorObject
    Set color = s.CreateColorObject
    style.FontSize = 20
    style.Bold = True
    pstyle.Alignment = ALIGN_LEFT
    pstyle.FirstLineLeftMargin = RULER_ONE_INCH
    Call color.SetRGB(123, 234, 123)
    style.NotesColor = color.NotesColor
    Call rtf.AppendStyle(style)
    Call rtf.AppendParagraphStyle(pstyle)
    Call rtf.AppendText("This is a text paragraph, aligned to the left.")
    Call rtf.AddNewline(1)
    
    'Generate a database link, link to the current database
    Call rtf.AppendDocLink(db, "Link to the current database", "Current database")
    
    'Generate a section containing a table
    Call rtf.AppendStyle(style)
    Call rtf.BeginSection("This is a section", style, color, True)
    Call rtf.AppendText("This is the beginning of the section")
    iRow% = 3
    iCol% = 3
    style.NotesColor = COLOR_BLUE
    Call rtf.AppendStyle(style)
    'Add a 3X3 form
    Call rtf.AppendTable(iRow%, iCol%)
    Call rtf.AppendText("This is the end of the section")
    Call rtf.EndSection
    Dim nav As NotesRichTextNavigator
    Set nav = rtf.CreateNavigator
    Call nav.FindFirstElement(RTELEM_TYPE_TABLECELL)
    style.FontSize=16
    style.Bold=False
    Call rtf.AppendStyle(style)
    For i% = 1 To iRow%
        For j% = 1 To iCol%
            Call rtf.BeginInsert(nav)
            Call rtf.AppendText("line" & i% & ", column "& j%)
            Call rtf.EndInsert
            Call nav.FindNextElement(RTELEM_TYPE_TABLECELL)
        Next
    Next
    
    'Add an attachment
    Call rtf.EmbedObject(EMBED_ATTACHMENT, "", "C:\Documents and Settings\All Users\
        Documents\My Pictures\Sample Pictures\Water lilies.jpg")
    
    Call doc.Save(True,True)
End Sub

The following describes some of the methods used in this program.

  • Notessession. createrichtextstyle: Creates a notesrichtextstyle. object.
  • Notessession. createrichtextparagraphstyle: Creates a notesrichtextparagraphstyle. object.
  • Notessession. createcolorobject: Creates a notescolorobject object.

    Note that notesrichtextstyle, notesrichtextparagraphstyle. And notescolorobject objects cannot be created using new, because these three classes do not have the new () method and can only be created using notessession.

  • Notesrichtextitem. appendstyle: Insert a format object in the current position. The format after this position is used until another format is inserted.
  • Notesrichtextitem. appendparagraphstyle: insert a paragraph format object in the current position. This format is used for paragraphs after this position until another paragraph format is inserted.
  • Notesrichtextitem. beginsection: Insert a section in the rich text domain.
  • Notesrichtextitem. endsection: the end of the section. It must be paired with beginsection.

    You can use various append methods to add various elements between the two methods. When inserting segments, the segments are always at the end of the rich text field. Note that you cannot create a section that contains the existing elements in the rich text domain. The Section created by using the beginsection method is always empty, and the section content must be added by the program.

  • Notesrichtextitem. createnavigator: Creates a Rich Text domain navigator object notesrichtextnavigator.

    Only this method can be used to create a Rich Text domain navigator. There is no new method for notesrichtextnavigator. You can also obtain a notesrichtextnavigator object by using the notesrichtextrange. Navigator attribute.

  • The notesrichtextnavigator class is the most important class for flexible access to the content in rich text fields. through some of its methods, you can easily access elements in rich text fields.

    The navigator object notesrichtextnavigator maintains a current location. Any get or find operation in the rich text domain may change this location. Navigation always occurs in the same type of elements. You can use the find and get methods to access the corresponding elements. After finding the required elements, you can use the get method to obtain the elements. The following table lists these methods.

  • Table 1. Method list

    Method NameDescription
    FindfirstelementMove the current position to the first element of the specified type
    FindnextelementMove the current position to the next element of the specified type
    FindlastelementMove the current position to the last element of the specified type
    FindnthelementMove the current position to the nth element of the specified type
    FindfirststringMove the current position to the beginning of the first specified string
    FindnextstringMove the current position to the beginning of the next specified string
    GetelementReturns the element at the current position.
    GetfirstelementReturns the first element of the specified type.
    GetlastelementReturns the last element of the specified type.
    GetnextelementReturns the next element of the specified type.
    GetnthelementReturns the nth element of the specified type.

    Notesrichtextitem. begininsert: Change the insert position from the end of the rich text field to the start or end of the specified element.

    Notesrichtextitem. endinsert: resets the insert position to the end of the Rich Text Field and needs to be paired with begininsert.

    You can use various append methods to add various elements between the two methods. Let's take a look at the specific usage of begininsert:

    Call notesrichtextitem. begininsert (element, [after])

    Parameter description

    Element: It can be vertex, notesrichtextdoclink, notesrichtextnavigator, notesrichtextrange, notesrichtextsection, or notesrichtexttable, indicating the location of the object. If it is vertex, it indicates the current location of the notesrichtextnavig.

    After: an optional Boolean parameter. "True" indicates that the insert position is at the end of the element. "false" indicates that the insert position starts with the element.

    Through the simple example above, we can see how to use LotusScript. to operate rich text fields. Next we will use another example to demonstrate how to use LotusScript. to implement a simple function similar to converting text and tables in Word. It mainly shows how to use the notesrichtextrange class to operate text paragraphs in rich text fields.

    First, create a form test, create a rich text field of the body in the form, and then create a form operation named text2table to convert the text into a table. The Code is as follows:

    Listing 2 converts text to a table

    Sub Click(Source As Button)
        Dim s As New NotesSession
        Dim ws As New NotesUIWorkspace
        Dim uidoc As NotesUIDocument
        Set uidoc = ws.CurrentDocument
        Dim doc As NotesDocument
        Set doc = uidoc.Document
        Dim rtf As NotesRichTextItem
        Set rtf = doc.GetFirstItem("Body")
        'Set the separator to a space
        delimiter$ = ""
        rowcount% = 0
        colcount% = 0
        Dim rtnav As NotesRichTextNavigator
        Set rtnav = rtf.CreateNavigator
        Dim rtrange As NotesRichTextRange
        Dim rows()
        Dim paraArray As Variant
        Dim paraStr As String
        Dim firstTime As Boolean
        firstTime = True
        
        If rtnav.FindFirstElement(RTELEM_TYPE_TABLECELL) Then
            Msgbox "Form already exists!"
            Exit Sub
        End If
        If rtnav.FindFirstElement(RTELEM_TYPE_TEXTPARAGRAPH) Then
            Set rtrange = rtf.CreateRange
            Do
                'Set the beginning of the text range to the position pointed to by rtnav
                Call rtrange.SetBegin(rtnav)
                'Get the text paragraph at that position
                paraStr = rtrange.TextParagraph
                paraArray = Split(paraStr)
                'Use firstTime to determine whether the format of the text paragraph can be converted into a table
                If firstTime Then
                    colcount% = Ubound(paraArray)
                    firstTime = False
                Else
                    If colcount% <> Ubound(paraArray) Then
                        Msgbox "Text cannot be converted into a table!"
                        Exit Sub
                    End If
                End If
                'Define a dynamic array to save all text paragraphs
                Redim Preserve rows(rowcount%)
                rows(rowcount%) = paraArray
                rowcount% = rowcount% + 1
            Loop While rtnav.FindNextElement(RTELEM_TYPE_TEXTPARAGRAPH)
        Else
            Messagebox "There is no text in the rich text field"
            Exit Sub
        End If
        'Clear the rich text field value
        rtf.Values = ""
        rowcount% = rowcount%-1
        'Insert the table, and insert the saved text into the corresponding table cell in turn
        Dim row As Variant
        Call rtf.AppendTable(rowcount%+1, colcount%+1)
        Call rtnav.FindFirstElement(RTELEM_TYPE_TABLECELL)
        For i% = 0 To rowcount%
            row = rows(i%)
            For j% = 0 To colcount%
                Call rtf.BeginInsert(rtnav)
                Call rtf.AppendText(row(j%))
                Call rtf.EndInsert
                Call rtnav.FindNextElement(RTELEM_TYPE_TABLECELL)
            Next
        Next
        'Save the document and reopen it to refresh
        Call doc.Save(True, True)
        Call uidoc.Close(True)
        Call ws.EditDocument(False,doc)
        
    End Sub
  • Create a form operation named table2text to convert the table to text. The Code is as follows:

    Listing 3 converts a table to text

    Sub Click(Source As Button)
        Dim s As New NotesSession
        Dim ws As New NotesUIWorkspace
        Dim uidoc As NotesUIDocument
        Set uidoc = ws.CurrentDocument
        Dim doc As NotesDocument
        Set doc = uidoc.Document
        Dim rtf As NotesRichTextItem
        Set rtf = doc.GetFirstItem("Body")
        
        delimiter$ = ""
        Dim rtnav As NotesRichTextNavigator
        Set rtnav = rtf.CreateNavigator
        Dim rtrange As NotesRichTextRange
        Set rtrange = rtf.CreateRange
        Dim tbl As NotesRichTextTable
        Dim rowcount As Integer
        Dim colcount As Integer
        
        If rtnav.FindFirstElement(RTELEM_TYPE_TABLE) Then
            Set tbl = rtnav.GetElement
            rowcount = tbl.RowCount-1
            colcount = tbl.ColumnCount-1
            Redim data(rowcount, colcount)
            For i% = 0 To rowcount
                For j% = 0 To colcount
                    Call rtnav.FindNextElement(RTELEM_TYPE_TABLECELL)
                    Call rtrange.SetBegin(rtnav)
                    If j% = colcount Then
                        Call rtf.AppendText(rtrange.TextParagraph)
                    Else
                        Call rtf.AppendText(rtrange.TextParagraph & delimiter$)
                    End If
                Next
                If i% <rowcount Then
                    'Generate a new paragraph
                    Call rtf.AddNewline(1)
                End If
            Next
        Else
            Messagebox "There are no tables in the rich text field"
            Exit Sub
        End If
        
        Call tbl.Remove
        Call doc.Save(True, True)
        Call uidoc.Close(True)
        Call ws.EditDocument(False,doc)
    End Sub
    Summary
  • Based on the introduction and examples and code analysis in this article, I believe that you have an understanding of how to operate the rich text fields in notes. We can see the LotusScript. by using appropriate classes flexibly, we can accomplish many functions that we think cannot be completed. I hope this article will be helpful to readers.
  • This article is an English version of an article which is originally in the Chinese language on aliyun.com and is provided for information purposes only. This website makes no representation or warranty of any kind, either expressed or implied, as to the accuracy, completeness ownership or reliability of the article or any translations thereof. If you have any concerns or complaints relating to the article, please send an email, providing a detailed description of the concern or complaint, to info-contact@alibabacloud.com. A staff member will contact you within 5 working days. Once verified, infringing content will be removed immediately.
  • 更多相关文章请浏览:How to Import Lotus Notes to Office 365 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值