Sometimes the best way to display a data set is in a table, organized in rows and columns. And since OmniGraffle is all about visualizing data, it offers the ability to create fully editable tables by cloning a specified graphic to create tables of multiple “cells,” with each “table cell” being a clone of the original specified graphic.
Here are the scripting terms and commands for the Table class:
Table Constructors
new Table(Graphic) • Create a table by replicating the indicated graphic for the cells.
Table Properties
columns (Number) • Number of columns in the table.
rows (Number) • Number of rows in the table.
Table Functions
graphicAt(rowNumber, columnNumber) (Graphic) • Retrieve the contained graphic at the given row and column index.
setRowHeight(numberOfPoints, rowNumber) • Change the height of all graphics in the given row index so that the row itself is the new height.
setColumnWidth(numberOfPoints, columnNumber) • Change the width of all graphics in the given column index so that the column itself is the new width.
Getting Tables
Here is a script for creating an array of references to all tables on the current canvas:
cnvs = document.windows[0].selection.canvas
var tables = new Array()
cnvs.graphics.forEach(function(graphic){
if(graphic.toString() === '[object Table]'){tables.push(graphic)}
})
Using the commands and properties of the Table class, you can create automation tools for quickly building and editing tables.
Create Table by Cloning the Selected Graphic
In this first example, a table is constructed by cloning a selected graphic to be the cells of the created table. As you can see in the example, the process simply involves using a reference to the selected graphic to create a new table object, and then indicating the number of rows and column the table object should have. The result is a new table with the indicated number of rows and columns.
if(document.windows[0].selection.graphics.length == 1){
graphic = document.windows[0].selection.graphics[0]
table = new Table(graphic)
table.columns = 4
table.rows = 3
} else {
new Alert('SELECTION ERROR','Please select a single graphic.').show(function(result){})
}
newAlert('SELECTION ERROR','Please select a single graphic.').show(function(result){})
08
}
Create a Cloned Table from JSON
Building upon the concepts used in the previous script, this script also creates a new table object using a selected graphic as the template for the table cells. However, this script determines the number of rows and columns to add to the table object, based upon the data in a JSON object (JSON = JavaScript Object Notation).
Once the table has been created, it is populated with the data from the JSON object.
if(document.windows[0].selection.graphics.length == 1){
graphic = document.windows[0].selection.graphics[0]
// Reconstitute data from JSON string
JSONstr = '[{"id":"ABV","state":"STATE","TSPCPC":"Cap Per Capita","TSPC":"Power(MW)","POPULATION":"POPULATION"},{"id":"AZ","state":"Arizona","TSPCPC":"166.9","TSPC":"1093.5","POPULATION":"6,553,225"},{"id":"HI","state":"Hawaii","TSPCPC":"136.5","TSPC":"190.0","POPULATION":"6,553,225"},{"id":"NV","state":"Nevada","TSPCPC":"122.9","TSPC":"339.1","POPULATION":"2,758,931"},{"id":"NJ","state":"New Jersey","TSPCPC":"109.6","TSPC":"971.4","POPULATION":"8,864,590"},{"id":"NM","state":"New Mexico","TSPCPC":"88.4","TSPC":"184.4","POPULATION":"2,085,538"},{"id":"CA","state":"California","TSPCPC":"66.7","TSPC":"2537.4","POPULATION":"38,041,430"}]'
dataSet = JSON.parse(JSONstr)
rowCount = dataSet.length
columnCount = Object.keys(dataSet[0]).length
table = new Table(graphic)
table.columns = columnCount
table.rows = rowCount
// Populate the data to the table
for (var i = 0; i < rowCount; i++){
var obj = dataSet[i];
var x = 0
for (var key in obj){
var attrName = key;
var attrValue = obj[key];
table.graphicAt(i,x).text = attrValue
x = x + 1
}
}
} else {
new Alert('SELECTION ERROR','Please select a single graphic.').show(function(result){})
}
JSONstr = '[{"id":"ABV","state":"STATE","TSPCPC":"Cap Per Capita","TSPC":"Power(MW)","POPULATION":"POPULATION"}, etc.]'
05
dataSet = JSON.parse(JSONstr)
06
rowCount = dataSet.length
07
columnCount = Object.keys(dataSet[0]).length
08
table = newTable(graphic)
09
table.columns = columnCount
10
table.rows = rowCount
11
// Populate the data to the table
12
for (var i = 0; i < rowCount; i++){
13
varobj = dataSet[i];
14
varx = 0
15
for (varkeyinobj){
16
varattrName = key;
17
varattrValue = obj[key];
18
table.graphicAt(i,x).text = attrValue
19
x = x + 1
20
}
21
}
22
} else {
23
new Alert('SELECTION ERROR','Please select a single graphic.').show(function(result){})
24
}
Create a new Table
In this example, the source graphic is created by the script itself instead of requiring the user to have an existing graphic selected. Various style properties, such as fill and stroke color are applied to the created graphic, before it is used to create the table.
// add a shape to the current canvas
cnvs = document.windows[0].selection.canvas
rect = new Rect(72, 72, 120, 36)
graphic = cnvs.addShape("Rectangle", rect)
// graphic properties
graphic.strokeThickness = 2
graphic.strokeColor = Color.white
// solid properties
graphic.fillColor = Color.darkGray
graphic.fillType = FillType.Solid
// solid text properties
graphic.text = 'PLACEHOLDER'
graphic.textSize = 14
graphic.textColor = Color.white
graphic.textHorizontalAlignment = HorizontalTextAlignment.Center
graphic.textVerticalPlacement = VerticalTextPlacement.Middle
graphic.textHorizontalPadding = 2
graphic.textVerticalPadding = 2
// create the table
table = new Table(graphic)
table.columns = 4
table.rows = 3
// adjust header row and column size
table.setRowHeight(72,0)
table.setColumnWidth(160,0)
newAlert('SELECTION ERROR','Please select a single table.').show(function(result){})
14
}
15
} else {
16
newAlert('SELECTION ERROR','Please select a single table.').show(function(result){})
17
}
Here’s another way to change the value of every cell of a table. Since a table is a specialized group, the Table class inherits some properties from the Group class, including the graphics property, whose value is an array of the individual graphics comprising a table. You can use the forEach() function to iterate the array, changing the value of the object’s properties.
if(document.windows[0].selection.graphics.length == 1){
table = document.windows[0].selection.graphics[0]
if(table.toString() == '[object Table]'){
table.graphics.forEach(function(cell){
cell.fillColor = Color.white
cell.textColor = Color.black
cell.strokeColor = Color.black
})
} else {
new Alert('SELECTION ERROR','Please select a single table.').show(function(result){})
}
} else {
new Alert('SELECTION ERROR','Please select a single table.').show(function(result){})
}
newAlert('SELECTION ERROR','Please select a single table.').show(function(result){})
11
}
12
} else {
13
newAlert('SELECTION ERROR','Please select a single table.').show(function(result){})
14
}
Here’s a variation of the previous scripts that prompts the user to choose between two sets of “styles” to apply to the graphics of the selected table. Note the use of the graphicAt() method to identify a specific table cell for alteration:
if(document.windows[0].selection.graphics.length == 1){
table = document.windows[0].selection.graphics[0]
if(table.toString() == '[object Table]'){
var alert = new Alert("Table Style", "Should the table be styled black text over white background, or white text over a black background?")
alert.addOption("Black Text")
alert.addOption("White Text")
alert.addOption("Cancel")
alert.show(function(result){
if (result == 0){
for(r = 0; r < table.rows; r++){
for(c = 0; c < table.columns; c++){
cell = table.graphicAt(r,c)
cell.fillColor = Color.white
cell.textColor = Color.black
cell.strokeColor = Color.black
}
}
} else if (result == 1){
for(r = 0; r < table.rows; r++){
for(c = 0; c < table.columns; c++){
cell = table.graphicAt(r,c)
cell.fillColor = Color.black
cell.textColor = Color.white
cell.strokeColor = Color.white
}
}
} else {
throw new Error('script cancelled')
}
})
} else {
new Alert('SELECTION ERROR','Please select a single table.').show(function(result){})
}
} else {
new Alert('SELECTION ERROR','Please select a single table.').show(function(result){})
}
varalert = newAlert("Table Style", "Should the table be styled black text over white background, or white text over a black background?")
05
alert.addOption("Black Text")
06
alert.addOption("White Text")
07
alert.addOption("Cancel")
08
alert.show(function(result){
09
if (result == 0){
10
for(r = 0; r < table.rows; r++){
11
for(c = 0; c < table.columns; c++){
12
cell = table.graphicAt(r,c)
13
cell.fillColor = Color.white
14
cell.textColor = Color.black
15
cell.strokeColor = Color.black
16
}
17
}
18
} else if (result == 1){
19
for(r = 0; r < table.rows; r++){
20
for(c = 0; c < table.columns; c++){
21
cell = table.graphicAt(r,c)
22
cell.fillColor = Color.black
23
cell.textColor = Color.white
24
cell.strokeColor = Color.white
25
}
26
}
27
} else {
28
thrownewError('script cancelled')
29
}
30
})
31
} else {
32
newAlert('SELECTION ERROR','Please select a single table.').show(function(result){})
33
}
34
} else {
35
newAlert('SELECTION ERROR','Please select a single table.').show(function(result){})
36
}
And here’s a script for changing the styling of the top row header of the selected table:
if(document.windows[0].selection.graphics.length == 1){
table = document.windows[0].selection.graphics[0]
if(table.toString() == '[object Table]'){
var alert = new Alert("Table Style", "Should the top table row be styled black text over white background, or white text over a black background?")
alert.addOption("Black Text")
alert.addOption("White Text")
alert.addOption("Cancel")
alert.show(function(result){
r = 0
if (result == 0){
for(c = 0; c < table.columns; c++){
cell = table.graphicAt(r,c)
cell.fillColor = Color.white
cell.textColor = Color.black
cell.strokeColor = Color.black
}
} else if (result == 1){
for(c = 0; c < table.columns; c++){
cell = table.graphicAt(r,c)
cell.fillColor = Color.black
cell.textColor = Color.white
cell.strokeColor = Color.white
}
} else {
throw new Error('script cancelled')
}
})
} else {
new Alert('SELECTION ERROR','Please select a single table.').show(function(result){})
}
} else {
new Alert('SELECTION ERROR','Please select a single table.').show(function(result){})
}
varalert = newalert("Table Style", "Should the top table row be styled black text over white background, or white text over a black background?")
05
alert.addOption("Black Text")
06
alert.addOption("White Text")
07
alert.addOption("Cancel")
08
alert.show(function(result){
09
r = 0
10
if (result == 0){
11
for(c = 0; c < table.columns; c++){
12
cell = table.graphicAt(r,c)
13
cell.fillColor = Color.white
14
cell.textColor = Color.black
15
cell.strokeColor = Color.black
16
}
17
} elseif (result == 1){
18
for(c = 0; c < table.columns; c++){
19
cell = table.graphicAt(r,c)
20
cell.fillColor = Color.black
21
cell.textColor = Color.white
22
cell.strokeColor = Color.white
23
}
24
} else {
25
thrownewError('script cancelled')
26
}
27
})
28
} else {
29
newAlert('SELECTION ERROR','Please select a single table.').show(function(result){})
30
}
31
} else {
32
newAlert('SELECTION ERROR','Please select a single table.').show(function(result){})
33
}
34
And here’s a script for changing the styling of the left column header of the selected table:
if(document.windows[0].selection.graphics.length == 1){
table = document.windows[0].selection.graphics[0]
if(table.toString() == '[object Table]'){
var alert = new Alert("Table Style", "Should the left table column be styled black text over white background, or white text over a black background?")
alert.addOption("Black Text")
alert.addOption("White Text")
alert.addOption("Cancel")
alert.show(function(result){
var c = 0
if (result == 0){
for(r = 0; r < table.rows; r++){
cell = table.graphicAt(r,c)
cell.fillColor = Color.white
cell.textColor = Color.black
cell.strokeColor = Color.black
}
} else if (result == 1){
for(r = 0; r < table.rows; r++){
cell = table.graphicAt(r,c)
cell.fillColor = Color.black
cell.textColor = Color.white
cell.strokeColor = Color.white
}
} else {
throw new Error('script cancelled')
}
})
} else {
new Alert('SELECTION ERROR','Please select a single table.').show(function(result){})
}
} else {
new Alert('SELECTION ERROR','Please select a single table.').show(function(result){})
}