Skip to content

Instantly share code, notes, and snippets.

@sjardim
Created December 11, 2024 15:04
Show Gist options
  • Save sjardim/33fd52c99d3070f7d592ace24c63a1fa to your computer and use it in GitHub Desktop.
Save sjardim/33fd52c99d3070f7d592ace24c63a1fa to your computer and use it in GitHub Desktop.
This script creates a "heatmap" table on Indesign. Uses tint values based on the cell's percentage value (from 100% to 0%)
/*
This script creates a "heatmap" table on Indesign:
- Checks for existing swatches named "HeatMapTableColor1", "HeatMapTableColor2", etc.
- Shows an error if no swatches are found with this naming pattern
- Shows an error if there aren't enough swatches for the table's columns
- Applies the colors in order (Color1 to first column, Color2 to second column, etc.)
- Uses tint values based on the cell's percentage value (from 100% to 0%)
- Maintains the text color logic (white text on dark backgrounds)
- If you don't want for the script to add a % after the values, if not present, comment the "// Add percentage sign if not present" block.
HOW TO USE:
First create your swatches in InDesign:
Name them "HeatMapTableColor1", "HeatMapTableColor2", etc.
Create at least as many swatches as you have table columns
Then:
1. Select your table (including its header)
2. Run the script
3. The colors will be applied automatically
The script will give helpful error messages if:
No HeatMapTableColor swatches are found
Not enough swatches are found for the number of columns
*/
// @target indesign
main();
function main() {
if (app.documents.length == 0) {
alert("Please open a document with a table.");
return;
}
var doc = app.activeDocument;
var selection = app.selection[0];
if (!(selection instanceof Table)) {
alert("Please select a table.");
return;
}
// Get all available HeatMapTableColor swatches
var heatMapColors = [];
for (var i = 1; i <= 10; i++) { // Check up to 10 colors
var swatchName = "HeatMapTableColor" + i;
try {
var swatch = doc.swatches.itemByName(swatchName);
swatch.name; // Will throw error if swatch doesn't exist
heatMapColors.push(swatchName);
} catch (e) {
// Swatch doesn't exist, skip it
continue;
}
}
// Check if we have any heat map colors defined
if (heatMapColors.length === 0) {
alert("No HeatMapTableColor swatches found!\n\nPlease create swatches named 'HeatMapTableColor1', 'HeatMapTableColor2', etc. before running this script.");
return;
}
var table = selection;
var numColumns = table.columns.length;
// Check if we have enough colors for the table
if (heatMapColors.length < numColumns) {
alert("Not enough color swatches!\n\nTable has " + numColumns + " columns but only found " + heatMapColors.length + " HeatMapTableColor swatches.\n\nPlease create more swatches following the naming pattern 'HeatMapTableColorN'.");
return;
}
// Process each cell in the table
for (var rowIndex = 0; rowIndex < table.rows.length; rowIndex++) {
var row = table.rows[rowIndex];
for (var colIndex = 0; colIndex < numColumns; colIndex++) {
var cell = row.cells[colIndex];
var cellContent = cell.contents.toString();
cellContent = cellContent.replace(/%/g, '');
var value = parseFloat(cellContent);
if (!isNaN(value)) {
// Get color for this column
var swatchName = heatMapColors[colIndex];
// Calculate tint - linear scale from 0 to 100
var tint = value;
// Apply formatting
cell.fillColor = doc.swatches.itemByName(swatchName);
cell.fillTint = tint;
// Set text color based on tint
if (tint > 50) {
cell.texts[0].fillColor = doc.swatches.itemByName("Paper");
} else {
cell.texts[0].fillColor = doc.swatches.itemByName("Black");
}
// Add percentage sign if not present
if (cell.contents.toString().indexOf('%') === -1) {
cell.contents = value.toString() + '%';
}
}
}
}
}
@sjardim
Copy link
Author

sjardim commented Dec 11, 2024

Example table after running the script:
CleanShot 2024-12-11 at 15 05 04

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment