Add Column
Adds a new column with values to an existing DataTable. This node is useful for extending your table structure and populating it with data in a single operation.
Common Properties
- Name - The custom name of the node.
- Color - The custom color of the node.
- Delay Before (sec) - Waits in seconds before executing the node.
- Delay After (sec) - Waits in seconds after executing node.
- Continue On Error - Automation will continue regardless of any error. The default value is false.
If the ContinueOnError property is true, no error is caught when the project is executed, even if a Catch node is used.
Input
- Table - DataTable to add the column to
- Column Name - Name of the new column to add
- Values - Array of values for the new column
Output
- Table - Updated DataTable with the new column and values
Behavior
The node behaves differently based on whether the table already has rows:
- Table has existing rows: The values array is matched to existing rows by index. The length of values should match the number of existing rows.
- Table is empty: New rows are created automatically, one for each value in the array.
Example Usage
Adding a Column to an Empty Table
// Start with an empty table that has columns: ["Name", "Age"]
var table = /* from Create DataTable */;
// Add a new column with values
var columnName = "Email";
var values = ["john@example.com", "jane@example.com", "bob@example.com"];
// Result: Table with 3 rows and 3 columns
// Row 0: Name="", Age="", Email="john@example.com"
// Row 1: Name="", Age="", Email="jane@example.com"
// Row 2: Name="", Age="", Email="bob@example.com"
Adding a Column to a Populated Table
// Table already has 3 rows with columns: ["Name", "Age"]
// Row 0: Name="John", Age=30
// Row 1: Name="Jane", Age=25
// Row 2: Name="Bob", Age=35
var columnName = "Salary";
var values = [50000, 55000, 60000];
// Result: Table with 3 rows and 3 columns
// Row 0: Name="John", Age=30, Salary=50000
// Row 1: Name="Jane", Age=25, Salary=55000
// Row 2: Name="Bob", Age=35, Salary=60000
Adding Calculated Values
// Add a column with calculated values from another source
var prices = [10.50, 20.00, 15.75];
var quantities = [5, 3, 10];
// Calculate totals
var totals = [];
for (var i = 0; i < prices.length; i++) {
totals.push(prices[i] * quantities[i]);
}
// Add as a new column
var columnName = "Total";
// totals = [52.50, 60.00, 157.50]
Tips
- Ensure the values array length matches the number of rows if the table is already populated
- Column names must be unique within the table
- Values can be of any type (strings, numbers, booleans, objects)
- This node is efficient for adding columns with pre-computed values
- For adding empty columns, use a values array filled with empty strings or null values
Common Errors
Empty Table Error
Error: Table cannot be empty. Please provide a valid DataTable.
Solution: Ensure you're passing a valid DataTable object from a previous node (e.g., Create DataTable, Read CSV).
Empty Column Name Error
Error: Column name cannot be empty. Please provide a valid column name.
Solution: Provide a non-empty string for the column name.
// Wrong
var columnName = "";
// Correct
var columnName = "Status";
Empty Values Error
Error: Values cannot be empty. Please provide at least one value for the column.
Solution: Ensure the values array contains at least one element.
// Wrong
var values = [];
// Correct
var values = ["Active"];
Value Count Mismatch
Error: Index out of bounds when values.length < existing row count
Solution: Ensure the values array has enough elements to match existing rows.
// If table has 5 rows, but only 3 values provided
// This will cause an error
// Correct approach:
var rowCount = 5; // Know your table size
var values = new Array(rowCount).fill("DefaultValue");
See Also
- Create DataTable - Create a new table
- Remove Column - Remove columns from a table
- Add Row - Add rows to a table
- Get Column - Retrieve column values