Formatted document in 1C

Since version 8.2.11 a new object appeared in the platform – Formatted document.
The formatted document is intended to design the text. It can be made bold, underlined, with increased/decreased font, centered, etc. Also it is possible to add picture. That is, in 1С it is possible now to format the text approximately like in MS Word and other similar programs. This can be convenient when designing various contracts or, for example, when editing email. Thus, we have an opportunity to lead a text document to the form that we desire.

Publications: Formatted document in 1C
In the syntax assistant there is a description.
Object FormattedDocument has a homonym data type which provides the functioning of this object and has the following methods:

Add
Delete
FindText
GenerateItems
GetBeginBookmark
GetBookmarkPosition
GetEndBookmark
GetFormattedString
GetHTML
GetItems
GetPositionBookmark
GetText
Insert
SetFormattedString
SetHTML
Write

Available in all application types: Thin client, web-client, server, thick client, external connection.

There is one important feature of this type. It is impossible to specify a type «Formatted document» for the attribute of object (catalog, document, etc.). But there is an ability to specify it for the form attributes.

Publications: Formatted document in 1CPublications: Formatted document in 1C

The question arises, how can we save the changes in text formatting that the user did? TO do this, a special object «ValueStorage» is used which may contain any data type including «formatted document». Thus, the object itself will be stored in database as «Value storage», later on opening the form it will be required to extract an object from the repository, display it on the form using a type «FormattedDocument». If the user will decide to save the changes, then during writing the object it is necessary to take an object «formatted document» again and save it in repository.

Example of work

Let’s take as a basis 1C:Accounting Suite, create in it the catalog «Companies». Open properties of this catalog and create an attribute «DescriptionFD» with type ValueStorage.

Create item form of catalog «Companies» and add to it a new attribute which we will call «FormattedDoc», set a data type «FormattedDocument». Please not that attribute «DescriptionFD» with type «ValueRepository» cannot be placed on the form.
Create also a command panel (menu) which contains the necessary buttons to control the formatted document. To do this, it is required to add to the form «Group – Command panel» and specify in property «Command source» a form attribute «FormattedDoc» with type FormattedDocument.

Publications: Formatted document in 1C

Launch debugger, check what we have got …

All is good, but there are some problems:

First, if to enter text and format it properly, a modification sing for the form do not appear. Thus, accidentally clicking the button to close the form, the system without asking questions closes it without saving.
in order to prevent such troubles, it is required to set the flag for the property «Saved data» of the form attribute «FormattedDoc». As a result, in any change of data in attribute «FormattedDoc», a modification sign will be set for the form, and on its closing the system will ask: «Save changes?».

Second. Let’s try to close the form a save the entered data. Re-opening the saved contract, we see that the text entered in the contract form is absent. Why is this happening?
The problem is that we do the changes in the form attribute which is not associated with the object attribute. Let’s set up the relation between the form attribute «FormattedDoc» with date type «FormattedDocument» and the object attribute «DescriptionFD» with type «ValueStorage».

Define this relation in software in the form module. Describe an algorithm.

  1. When the form is opened, it is required to refer to the object attribute «DescriptionFD» in order to read data from it and save them in the formatted document «FormattedDoc».
  2. When writing an object, take the content of formatted document and place it in the object attribute «DescriptionFD» with type «ValueStorage».

Thus, we will produce an effect that all the changes made by the user will be saved in the database.

We will need the following event handlers:

- event OnReadAtServer(CurrentObject)
It is convenient that on erroneous introduction of the changes in the form data it is possible to click the button «Re-read» and return to the initial data.

&AtServer
Procedure OnReadAtServer(CurrentObject)
 FormattedDoc = CurrentObject.DescriptionFD.Get();
EndProcedure

- event BeforeWriteAtServer(Cancel, CurrentObject, WriteParameters)

&AtServer
Procedure BeforeWriteAtServer(Cancel, CurrentObject, WriteParameters)
 CurrentObject.DescriptionFD = New ValueStorage(FormattedDoc);
EndProcedure

Since an attribute DescriptionFD has a type «ValueStorage», it is required to take into account the nuances or working with it … In case of object reading a method Get() is used and in case of writing an object constructor New ValueStorage() is used.

Thank you for attention!

[Total: 0    Average: 0/5]

Leave a Reply

Your email address will not be published. Required fields are marked *