Help with correcting my fs please

Aly_k
Aly_k Member Posts: 17 ✭

Can someone please assist in helping me correct my fs code.
FeatureScript 2815;
import(path : "onshape/std/common.fs", version : "2815.0");
import(path : "onshape/std/table.fs", version : "2815.0");

annotation { "Feature Type Name" : "Custom Part Table from Config Table" }
export const customPartTable = defineFeature(function(context is Context, id is Id, definition is map)
{
// Name of your configuration table in Onshape
const configTableName = "PARTS"; // Update this if your table is named differently

// MANDATORY PRECONDITION: Must check that the configuration table exists before fetching data.
precondition(hasConfigurationTable(context, configTableName), "Configuration table '" ~ configTableName ~ "' must exist in the Part Studio.");

// Get all row names from the configuration table
var rowNames = getConfigurationRowNames(context, configTableName);

if (size(rowNames) == 0)
{
    // Table is empty, no rows to display
    return;
}

// Collect all parts (rows) and all unique column names (keys)
var allKeys = {};
var PARTS = [];

for (var rowName in rowNames)
{
    // Fetch the parameters for the current row
    var part = getConfigurationParameters(context, configTableName, rowName);

    // Include the row name as the 'Name' column if the row doesn't specify one
    if (!hasKey(part, "Name")) {
        part = part + { "Name": rowName };
    }
    
    // Collect all unique keys for column definitions
    for (var key in part)
    {
        allKeys[key] = true;
    }
    PARTS = append(PARTS, part);
}

// --- Column Generation Logic (Ensures consistent, predictable column order) ---

// Define a preferred order for important columns to ensure consistency
const PREFERRED_KEYS = ["Name", "Desc", "Material", "Length", "Diameter", "Width", "Height"];

var columns = [];
var processedKeys = {}; // Keep track of keys we've already added

// 1. Add preferred keys that exist in the data
for (var i = 0; i < size(PREFERRED_KEYS); i++)
{
    var key = PREFERRED_KEYS[i];
    if (hasKey(allKeys, key))
    {
        columns = append(columns, tableColumn(key));
        processedKeys[key] = true;
    }
}

// 2. Collect remaining unique keys
var remainingKeys = [];
for (var key in allKeys)
{
    if (!hasKey(processedKeys, key))
    {
        remainingKeys = append(remainingKeys, key);
    }
}

// 3. Sort the remaining keys alphabetically for deterministic ordering
remainingKeys = sort(remainingKeys, comparator());

// 4. Append the sorted remaining keys
for (var key in remainingKeys)
{
    columns = append(columns, tableColumn(remainingKeys[key]));
}

// --- End Column Generation Logic ---

// Build table rows dynamically using the collected PARTS data
var rows = [];
for (var part in PARTS)
{
    var row = [];
    // Iterate through the determined, ordered columns
    for (var col in columns)
    {
        var colName = columns[col].name;
        if (hasKey(part, colName))
            row = append(row, part[colName]);
        else
            row = append(row, "—"); // Empty if part does not have this property
    }
    rows = append(rows, row);
}

// Create the custom table
table(id + "PartTable", {
    "columns": columns,
    "rows": rows
});

});

I don't seem to understand what I am doing wrong these are the errors I am getting.

image.png

this fs will be used to reference certain things from a configuration table and turn that info into a custom table for me so I can insert that table into a drawing.

Comments

  • jelte_steur_info
    jelte_steur_info Member Posts: 683 PRO

    one thing:

    // 1. Add preferred keys that exist in the data
    for (var i = 0; i < size(PREFERRED_KEYS); i++)

    i += 1

    then there's something wrong with the precondition definition. But i haven't programmed tables before so i'm not sure what.
    BUT if you add
    precondition{} before the function block, and comment out the existing precondition line,
    There are still a bunch of functions that aren't recognised…