KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Programmatically Insert a Row in 4D View Pro
PRODUCT: 4D | VERSION: 18 | PLATFORM: Mac
Published On: December 27, 2019

Suppose that you are converting an old database to v18, and that database makes extensive use of 4D View plugins that are managed via code rather than user interface. Converting that 4D View plugin code to 4D View Pro code may not seem straightfoward. Particularly, creating methods to insert and delete rows and columns in 4DVP can be a difficult task at first. Below is an example to show how you could build a method to insert a row.

// --------------------------------------------------------------------------------
// Method: VP_INSERT_ROW
//
// Description:
//    Inserts a new blank row immediately underneath the current
//    active row in a 4D View Pro area
//
// Parameters: $1 (TEXT) - View Pro area name
//
// Output: N/A
// --------------------------------------------------------------------------------

C_TEXT($1;$vpName_t;$sName_t)
C_LONGINT($currRow_li;$lastRow_li)
C_OBJECT($active_o;$sheet_o;$data_o)

If (Count parameters=1)
   $vpName_t:=$1
   $active_o:=VP Get selection ($vpName_t)
   If ($active_o.ranges#Null)
    $sheet_o:=VP Export to object ($vpName_t)
  
    // Get current sheet
     $sName_t:="Sheet"+String($active_o.ranges[0].sheet+1)
  
    // Get last row number
    $lastRow_li:=$sheet_o.spreadJS.sheets[$sName_t].rowCount
  
    // Get currently selected row number
    $currRow_li:=$active_o.ranges[0].row
  
     // Append new row to sheet
     $sheet_o.spreadJS.sheets[$sName_t].rowCount:=\
      $sheet_o.spreadJS.sheets[$sName_t].rowCount+1
  
    // Iterate from last row to one below the currently selected row
    For ($i;$lastRow_li;$currRow_li+1;-1)
  
      // Shift row data down
      If ($sheet_o.spreadJS.sheets[$sName_t].data.dataTable[String($i)]#Null)
       // Fail safe for sheet with no data
  
        // Get row data -> Duplicate on to next row
        $data_o:=$sheet_o.spreadJS.sheets[$sName_t].data.dataTable[String($i)]
        $sheet_o.spreadJS.sheets[$sName_t].data.dataTable[String($i+1)]:=$data_o
  
        // Clear previous row data
        OB REMOVE($sheet_o.spreadJS.sheets[$sName_t].data.dataTable;String($i))
      End if
  
    End for
  
    VP IMPORT FROM OBJECT ($vpName_t;$sheet_o)
  End if
End if

Pass in the name of your VP area into the above method and it should conveniently insert a row in the sheet when executed. The method makes use of VP Get selection to get the active row in the sheet, VP Export to object to convert the VP area into an object to manipulate, and VP IMPORT FROM OBJECT to convert the VP object back to VP area. Of course, the method can be modified and extended to cover deleting rows and inserting/deleting columns as well.