KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Utility Method: Create Needed Rows or Columns When Setting Value in Nonexistent Cell in ViewPro
PRODUCT: 4D View Pro | VERSION: 19 | PLATFORM: Mac & Win
Published On: January 17, 2022

When inserting values in a 4DView Pro document, the value cannot be inserted if the target cell does not exist. For example, trying to insert a value into the 2000th row of a spreadsheet with only 1000 rows will fail.

During an automated operation that inserts large amounts of data into a View Pro spreadsheet, the total number of columns and rows needed for the data may not always be known. In the case you may need more rows or columns than currently exsists in the spreadsheet, but is unsure of how many, the method below can help.

The method below can be placed right before commands that set the value like VP SET VALUE. The method checks if the target cell exists. If it does not, the needed amount of rows and columns are added. The method takes 4 parameters: ViewPro Area name, target column, target row, and sheet number.

//----------------------------------------------------------------------
//Method: vp_CheckCellExists
//Description: Place this method before commands that insert values into View Pro spreadsheet. It checks if target cell exists. If not, the needed rows and columns are created
//Parameters:
// - $1 (text) : View Pro Area Name
// - $2 (longint): Target column to insert value
// - $3 (longint): Target row to insert value
// - $4 (longint): Optional. Target sheet to insert value
//----------------------------------------------------------------------
C_TEXT($1; $vpName)
C_LONGINT($2; $3; $col; $row; $sheet; $rowCount; $colCount)
$vpName:=$1
$col:=$2
$row:=$3
$sheet:=0

If (Count parameters>=3)
  If (Count parameters=4)
    C_LONGINT($4)
    $sheet:=$4
  End if
  
  $rowCount:=VP Get row count($vpName; $sheet)
  $colCount:=VP Get column count($vpName; $sheet)
  
  If ($row>=$rowCount)
    VP SET ROW COUNT($vpName; $row+1; $sheet)
  End if
  
  If ($col>=$colCount)
    VP SET COLUMN COUNT($vpName; $col+1; $sheet)
  End if
End if


Example:
C_LONGINT($col_l; $row_l)
$col_l:=1
$row_l:=1500

vp_CheckCellExists("VPArea"; $col_l; $row_l)
VP SET VALUE(VP Cell("VPArea"; $col_l; $row_l); New object("value"; "Hello World"))