JSL:如何复制公式中的最后一个项并将该值添加到同一公式中大家好,
我正在寻找一个脚本,它允许我首先选择多个公式列,并在每个公式列中获取最后一个数字项并将该值添加到已经存在的公式中。
为了说明目的:
运行脚本之前的一个公式列:

然后(只是复制并添加了值 39,78...):

由于我有数百个不同的公式列,因此在这里有一个自动脚本就太好了。
期待您的帮助!
谢谢!
/马可
高级统计建模自动化和脚本数据访问质量与工艺工程回复:JSL:如何复制公式中的最后一项并将此值添加到同一公式中这里有两种方法可以做到这一点,将公式作为字符串操作,然后作为表达式操作:
Names Default To Here( 1 );
//Make some sample data
dt =New Table( "Untitled",
Add Rows( 100 )
);
For Each( {i}, 1 :: 6,
Eval(
Eval Expr(
dt << New Column( "ToFix " || char(i),
Expression,
"None",
Formula( Vec Quadratic( [1 3 5, 3 2 6, 5 6 1], [1 3 5, 2 4 6] ) * Expr( i ) )
)
)
)
);
// Select some columns
dt << Select Columns( (dt << Get Column References)[4::5] );
//'fix' each selected column formula by manipulating the formula as a string
for each( {col, c}, dt << Get Column References( dt << Get Selected Columns ),
if( Starts With( col << get name(), "ToFix"),
newf = regex(
char(col << Get Formula),
"(.* \* (\d+))",
"\1 + \2"
);
//show(Parse(newf));
Eval( Eval Expr(
col << Set Formula( Expr( Parse(newf) ) )
) );
)
);
// Select some different columns
dt << Select Columns( (dt << Get Column References)[6::7] );
//'fix' each selected column formula by manipulating the formula as an expression
for each( {col, c}, dt << Get Column References( dt << Get Selected Columns ),
if( Starts With( col << get name(), "ToFix"),
f = col << Get Formula;
v = Arg(col << Get Formula, 2);
//show(Name Expr(f));
newf = Eval Expr( Add(Expr(Name Expr(f)), Expr(Name Expr(v))));
show(newf);
Eval( Eval Expr(
col << Set Formula( Expr( Name Expr(newf) ) )
) );
)
);
回复:JSL:如何复制公式中的最后一项并将此值添加到同一公式中非常感谢您的解决方案@ih !
我不得不稍微调整正则表达式以满足我的需求,但你的帖子绝对是找到它的关键!
对于任何其他用户,请在下面找到修改后的代码:
// Select some columns
dt = currentdatatable();
dt << Get Selected Columns;
//'fix' each selected column formula by manipulating the formula as a string
for each( {col, c}, dt << Get Column References( dt << Get Selected Columns ),
newf = regex(
char(col << Get Formula),
"(.* \* (\d+\.?\d*))",
"\1 + \2"
);
//show(Parse(newf));
Eval( Eval Expr(
col << Set Formula( Expr( Parse(newf) ) )
) );
);回复:JSL:如何复制公式中的最后一项并将此值添加到同一公式中根据您的公式,您也许还可以使用 Arg()。 下面是一个非常简单的例子:
Names Default To Here(1);
dt = New Table("Untitled 2",
Add Rows(2),
New Column("Column 1", Numeric, "Continuous", Format("Best", 12), Set Values([1, 2])),
New Column("Column 2", Numeric, "Continuous", Format("Best", 12), Formula(:Column 1 * 2)),
New Column("Column 3", Numeric, "Continuous", Format("Best", 12), Formula(:Column 2 * 3))
);
cur_col_idx = 3;
Show(column(dt, cur_col_idx) << get formula);
Eval(Substitute(
Expr(
Column(dt, cur_col_idx) << Set Formula(_old_f_ + _new_mult_)
),
Expr(_old_f_), Column(dt, cur_col_idx) << Get Formula,
Expr(_new_mult_),
old_f = Column(dt, cur_col_idx - 1) << Get Formula;
Arg Expr(old_f, N Arg(old_f))
));
Show(column(dt, cur_col_idx) << get formula);