Do you need to create a 4D View Pro document using code, without any user interface? It’s possible! Because 4D View Pro documents are objects, it’s very easy to create off-screen documents. You can create a new document in a variable of object type and manipulate it through its properties. When your document is ready, you can register it in your database, in a file, or display it directly in a 4D View Pro area.
Creating a 4D View Pro document is quite simple.
First, create an object with the following structure:
$doc:=New object
$doc.version:=1 // version [mandatory]
$doc.dateCreation:=Timestamp // creation date
$doc.dateModified:=Timestamp // modification date
$doc.spreadJS:=New object // spreadJS [mandatory]
$doc.meta:=New object("comment";"4D - How do I") // optional metadata
This object contains:
- Internal information for 4D: document version
- Developer information: metadata, creation, and modification dates
- Document structure and data: data to be passed in the spreadJS property.
Once your structure is ready, complete the spreadJS property. It’s an object which contains, among other things, spreadsheet information, data, cell styles, formulas, etc.
starter example
In the example bellow we write “Hello 4D View Pro!” in the first cell of your document:
// Create the document
$doc:=New object("version";1;"dateCreation";Timestamp;"dateModified";Timestamp)
$doc.spreadJS:=New object("version";"11.0.0";"sheets";New object)
//
// Creation of the first sheet
$sheet:=New object("name";"Sheet1";"rowCount";100;"columnCount";20)
$doc.spreadJS.sheets[$sheet.name]:=$sheet
//
// Fill first sheet cells
$sheet.data:=New object("dataTable";New object)
$sheet.data.dataTable["0"]:=New object // first row
$sheet.data.dataTable["0"]["0"]:=New object("value";"Hello 4D View Pro!") // set A1
$sheet.data.dataTable["0"]["1"]:=New object("value";42) // set B1
//
VP IMPORT FROM OBJECT ("ViewProArea";$doc)
Advanced example
In the example database, you can see how to quickly and easily create a simple document off-screen.
Off-screen creation of a 4D View Pro doc
To assist you with the creation of more complicated documents, you can refer to the JSON schema of SpreadJS documents.