Javascript Indirect field reference

I hope I'm asking this question correctly...
Is there/ will there be a function that might allow me to access a field value by reference instead of having to use its name directly?

For example, I have 25 skills, which I represent each skill with its name, rank and modifiers. Fields: Skill1, Rank1, Mod1; Skill2, Rank2, Mod2, ..., Skill25, Rank25, Mod25.

I've been accessing each field as it is named (typed out in the code), which can get very extensive. It would be nice if there was a way to access those field values by reference using a string such as Field('Skill' + varx).Value, Field('Rank' + varx).Value, Field('Mod' + varx).Value.

Comments

  • No, I dont think there's a way to do that. I can look at adding such, but it woundnt be for the 1.x version as I dont want to introduce incompatibilities.
  • I also had an issue with this and found a solution (though kinda ugly) to deal with it.

    you can do something like this.
    for(i=1;i<=25;i++)
    {
        skillstr = eval('Skill' + i);
        rankstr = eval('Rank' + i);
        modstr = eval('Mod' + i);
    
        skillstr.Text = somevalue;  if i = 1 this will reference  Skill1.Text and so on.
        rankstr.Text = some other value;
        modstr.Text = yet another value;
    }
    

    The trick is using the eval() function. Its not really considered a good thing to do, but it is the only way I have found to do what you want.
  • Thank you royb!
    Your snippet helped me writing a script function that can evaluate a dynamic formula.

    Example.
    Say you have DataStore with named Skills
    Skill,Attribute
    Sneak,Dexterity+Quickness
    Melee Weapons,Strength+Quickness
    
    Add field Dexterity
    Add field Quickness
    Add field Strength
    Add field Skill1 with List property := Skills This will make it a drop down box with all items in the datastore
    Add field SkillFormula1 with Field Source := Lookup( 'Skills', Skill1.Text, 'Attribute') This will display the formula for selected item
    Add field FormulaResult1 with Field Source := CalculateSkillPotential(SkillFormula1.Text) This will calculate the value of the formula using fields in the sheet
    Some extra field for user information, txtOut and txtError
    // ******************************************************************************
    // Routine:       CalculateSkillPotential
    // Description:   Calculates skill potential
    // Parameters:    Attributes formula
    // Returns:       Potential as Number
    // ==============================================================================
    // Date       Version  Author                         Description
    // ---------- -------- ------------------------------ ---------------------------
    // 2013-03-21 1.0.0    Anders Forslund                Routine created
    // ******************************************************************************
    function CalculateSkillPotential(strAttributes)
    {
        // some debug related text fields
        txtOut.Text="";
        txtError.Text="";
        // setup default variables
        var dblPotential=0;
        var dblAttribute=0;
        // check that string entered is not empty
        if (strAttributes.length > 0)
        {
            // prepare string
            // split  into array
            var n=strAttributes.split("+");
            // loop array
            for (var i in n){
                // get attribute name
                strAttribute = n[i] ;
                // get value for attribute
                // enter error checking part
                try
                {
                    // check that valid number is entered in the text
                    if (isNumber(eval(strAttribute + "\.Text")))
                    {
                        // get value
                        dblAttribute = eval(strAttribute + "\.Value");
                        // sum potential
                        dblPotential = dblPotential + dblAttribute;
                    }
                    else
                    {
                        // invalid character in field
                        txtOut.Text= "Invalid number in field " + strAttribute;
                        return "E!";
                    }
                }
                catch (e)
                {
                    // something went wrong evaluating the attribute, inform user
                    txtError.Text= "Error evaluating attribute " + strAttribute + ": " + e.message;
                    return "";
                }
                finally
                {
                    // do nothing
                }
        }
        // completed ok
        return (dblPotential);
        }
        else
        {
        return "";
        }
    }
    
    Comments on the code above.
    Using dots(.) in string functions is tricky since the dot is a special character in javascript. That is why the escape character "\" is used in conjunction with the dot(\.)

    Support function
    // ******************************************************************************
    // Routine:       isNumber
    // Description:   Evaluates expression to check if it is numeric
    // Parameters:    Expression
    // Returns:       True/false
    // ==============================================================================
    // Date       Version  Author                         Description
    // ---------- -------- ------------------------------ ---------------------------
    // 2013-03-21 1.0.0    Anders Forslund                Routine created
    // ******************************************************************************
    function isNumber(n)
    {
      return !isNaN(parseFloat(n)) && isFinite(n);
    }
    

Leave a Comment