Programmer's Guide > Programming and Scripting Techniques > Working with Tables > Example: Inserting a Column Based on the Current Selection
  
Example: Inserting a Column Based on the Current Selection
This example uses the function tbl_insert_column to insert a column to the left of the current selection. If the selection is invalid, that is, it is discontiguous or not a rectangle, a message is displayed in a dialog box and tbl_insert_column returns zero.
To run this sample code:
1. Copy the tbl_insert_column code to a file named insertcol.js in Arbortext-path\custom\scripts.
2. Start Arbortext Editor, open a Arbortext XML Docbook template, insert a 5x5 table, and enter the following command at the Arbortext Editor command line:
source insertcol.js
3. Select a portion of the table.
4. Enter the following command at the Arbortext Editor command line:
js tbl_insert_column()

//-----------------------------------------------------------------
// Function: tbl_insert_column
//
// Description:
// Inserts one or more columns into a document
//
// Parameter:
// insertLeft: if true (nonzero), adds columns to the left of
// the target
//
// Returns:
// 0 if the insert failed, 1 if it succeeded
//
//-----------------------------------------------------------------
function tbl_insert_column(insertLeft)
{
if(insertLeft == undefined){insertLeft = 0;}

var doc = Application.activeDocument;
//Check to see that there's either a table selection, or that the
//cursor is in a table cell.
//To see of a cursor is in a cell:
//get the range that is the cell containing the cursor
//get the cell node
//get the cell containing the caret
if((doc.selectionType != doc.TABLE_SELECTION) &&
((cell = doc.insertionPoint.endContainer.enclosingCell) == null)){
Application.alert("No table object is selected");
return 0;
}
//get the table selection from the active document
var tilePlex = doc.tableSelection;

//if the selection is empty, i.e., just a cursor in a cell,
//add that cell to the tableTilePlex to create a 1x1 rectangle
if(tilePlex.empty){
tilePlex.addObject(cell);
}

//ensure table selection will accept inserted columns
if(!tilePlex.modifiable){
Application.alert("table cannot be modified");
return 0;
}

//ensure table selection is contiguous and does not cross
//grid boundaries
var validRectangle = tilePlex.pasteRectangle;
if(validRectangle == null){
Application.alert("The table selection is discontiguous or crosses grid boundaries");
return 0;
}

//At this point, the selection is valid and can be modified, add the
//columns to the grid.
//A new column is added for each one that the user has selected.
var newGrid = validRectangle.lowerLeft.grid;
for(i = 0; i < validRectangle.width; i++){
try{
if(insertLeft){
newGrid.addColumn(validRectangle.lowerLeft.column);
}
else{
newGrid.addColumn(validRectangle.upperRight.column.columnRight);
}
}
catch(e){Application.alert("Column insertion failed because " + e.code);}
}

//success
return 1;
}//end of tbl_insert_column
To implement the previous example using JScript, change the line:
if((doc.selectionType != doc.TABLE_SELECTION) &&
to be:
if((doc.selectionType != 2) &&