4D View Pro: Merge and unmerge cells

Automatically translated from English

Often when you create a table, you might need to combine two or more cells to create a centered title on a particular section of your table. As of 4D v18 R4, a new set of commands is at your disposal to help you merge and unmerge a group of cells by programming. Let’s take a look at some examples.

Merged cells

To create a merged cell, simply define a range with all of the cells you want to combine. Pass the range as parameter to the VP  ADD SPAN command and you’ll be good to go!

For example, let’s use the document below:

If you want to merge the First quarter and Second quarter cells into the adjacent cells and merge the South area cell into the two rows below it, simply use this code:

// First quarter range
$q1:=VP Cells ("ViewProArea";2;3;3;1)
 
// Second quarter range
$q2:=VP Cells ("ViewProArea";5;3;3;1)
 
// South area range
$south:=VP Cells ("ViewProArea";0;5;1;3)
 
VP ADD SPAN (VP Combine ranges ($q1;$q2;$south))

Here’s the result:

retrieve merged cells

Now if you want to center the text for all of the merged cells in your document, you can use the VP Get spans command to retrieve all the merged cells:


// Search all the merge cells in the spreadsheet

$range:=VP Get spans (VP All ("ViewProArea"))
 
//apply a style to all spans
$style:=New object("vAlign";vk vertical align center;"hAlign";vk horizontal align center)
VP SET CELL STYLE ($range;$style)

remove merged cells

If for any reason you need to remove the cell spans (aka unmerge the cells) in your document, just use the VP REMOVE SPAN:

// Search all the merge cells in the spreadsheet
$range:=VP Get spans (VP All ("ViewProArea"))
 
// remove the found merged cells
VP REMOVE SPAN ($range)

Need more information? Check out the documentation.

Fabrice Mainguené

• Product Owner •

Fabrice Mainguené joined 4D Program team in November, 2016. As a Product Owner, he is in charge of writing the user stories then translating it to functional specifications. His role is also to make sure that the feature implementation delivered is meeting the customer need.

After obtaining a Bachelor degree in Computer Science at CNAM, Fabrice joined a small software publishing company as a Windev developer. Then he worked for different companies in industry and trade areas as a Windev and web developer as well as technical advisor on new features.