Programmer's Guide > Programming and Scripting Techniques > Working with Tables > Example: Inserting and Modifying a Table
  
Example: Inserting and Modifying a Table
This example uses the function addTable to perform the following actions:
Insert a six-row five-column table into the first paragraph of a Arbortext XML Docbook template.
Span cells 1-2 and 3-5 of the first row and add text to the spanned cells.
Convert the first row to a header row.
Turn off rules for the entire table.
The function appendText is a utility function for adding text to a cell.
To run this sample code:
1. Copy addTable and appendText to a file named addtable.js in Arbortext-path\custom\scripts.
2. Start Arbortext Editor, open a Arbortext XML Docbook template, and enter the following commands at the Arbortext Editor command line:
source addtable.js
js addtable

//-----------------------------------------------------------------
// Function: appendText
//
// Description: A utility function called by addTable.
// Adds text to a cell
//
// Parameters: cell: the target for the added text
// text: the text to be added
//
//-----------------------------------------------------------------
function appendText(cell, text)
{
var cellRange = cell.contents;
cellRange.collapse( false );
var textNode = cell.document.createTextNode(text);
cellRange.insertNode(textNode);
}
//-----------------------------------------------------------------
// Function: addTable
//
// Description: Add a table to the first para in a document
//
// Parameters: NONE
//
//-----------------------------------------------------------------
function addTable(){
var doc = Application.activeDocument;
var para = doc.getElementsByTagName("para").item(0);
try{
var set = para.insertTable("OASIS Exchange", "table", 5, 6, null);
}
catch(e){Application.alert("Exception " + e.code() +
" caught in insertTable");
return 0;}
var grid = set.grids.item(0);
var firstRow = grid.row(1);
// Span cells 1-2 and 3-5
firstRow.cell(1).span(firstRow.cell(2));
firstRow.cell(3).span(firstRow.cell(5));
appendText(firstRow.cell(1), "Cells 1 and 2");
appendText(firstRow.cell(3), "Cells 3-5");
// Change first row to a header row
firstRow.setAttribute("header_level",1);
//turn off the table rules
var rules = grid.rules;
for (i = 0; i < rules.length; i++) {
rules.item(i).setAttribute("style", "blank");
}
}//end of addTable