我已收到我想讨论的公式大约3年前从@tx尼尔森,从那以后就再也没有放过它,并且之前多次思考过这个问题,慢慢地更加认真地对待脚本编写。 与此同时,公式救了我很多的时间。
在下面的示例中,相同的公式添加了 3 次,每次都有一点变化。 我想讨论的问题是复制和运行表的表脚本(包括新添加的公式列)时抛出的不同错误消息。
Names Default To Here( 1 );
// first, we need an example table
dt = New Table( "ExampleTable",
Add Rows( 24 ),
New Column( "BatchId",
Character,
"Nominal",
Set Values(
{"A", "A", "A", "A", "A", "A", "B", "B", "B", "C", "C", "C", "D", "D",
"D", "E", "E", "E", "F", "F", "F", "G", "G", "G"}
)
),
New Column( "StorageCondition",
Character,
"Nominal",
Set Values(
{"25°C/60%RH", "25°C/60%RH", "25°C/60%RH", "5°C", "5°C", "5°C",
"25°C/60%RH", "25°C/60%RH", "25°C/60%RH", "25°C/60%RH", "25°C/60%RH",
"25°C/60%RH", "25°C/60%RH", "25°C/60%RH", "25°C/60%RH", "25°C/60%RH",
"25°C/60%RH", "25°C/60%RH", "25°C/60%RH", "25°C/60%RH", "25°C/60%RH",
"25°C/60%RH", "25°C/60%RH", "25°C/60%RH"}
)
),
New Column( "Time",
Numeric,
"Continuous",
Format( "Best", 12 ),
Set Values(
[0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2]
)
),
New Column( "QcParameter",
Numeric,
"Continuous",
Format( "Best", 12 ),
Set Values(
[0.2, 0.3, 0.4, 0.2, 0.21, 0.22, 0.3, 0.9, 1.2, ., 0.2, 0.4, 0.1, ., 0.1,
0, 0, 0, 0.1, 0, 0, 0, 0.1, 0]
)
)
);
// adding formula column via approach nr. 1<BR />// this adds the formula alright, but when copying and executing the resulting table script will leave the formula column empty<BR />// no need to discuss this, because this obviously missing the 'eval(eval expr(expr()))' wrapping (see screenshot below)
dt << New Column( "QcParameter [% rel.]_1",
numeric,
formula(
curBatch = :BatchId;
curCondition = :StorageCondition;
theRows = dt << get rows where(
:BatchId == curBatch & :StorageCondition == curCondition & :Time == 0
);
d0Row = theRows[1];
d0Time = :QcParameter[d0Row];
(100 * :QcParameter / d0Time);
)
);
// adding formula column via approach nr. 2<BR />// this will fill the formula column as desired when copying and running the table script with the added formula columns<BR />// but it will also throw an error message when new rows are added to the table then (see screenshot below)
Eval(
Eval Expr(
dt << New Column( "QcParameter [% rel.]_2",
numeric,
formula(
curBatch = :BatchId;
curCondition = :StorageCondition;
theRows = Expr( dt ) << get rows where(
:BatchId == curBatch & :StorageCondition == curCondition & :Time == 0
);
d0Row = theRows[1]; // removed from approach nr. 3
d0Time = :QcParameter[d0Row];
(100 * :QcParameter / d0Time);
)
)
)
);
// adding formula column via approach nr. 3
// this will fill the formula column as desired when copying and running the table script with the added formula columns<BR />// and it will NOT throw an error message when new rows are added to the table then (see screenshot below)
Eval(
Eval Expr(
dt << New Column( "QcParameter [% rel.]_3",
numeric,
formula(
curBatch = :BatchId;
curCondition = :StorageCondition;
theRows = Expr( dt ) << get rows where(
:BatchId == curBatch & :StorageCondition == curCondition & :Time == 0
);
d0Time = :QcParameter[theRows];
(100 * :QcParameter) / d0Time;
)
)
)
);
使用其他公式列重新运行表脚本时公式列“QcParameter [%r rel.]_1]”的错误消息的屏幕截图:

重新运行具有附加公式列的表脚本时公式列“QcParameter [%r rel.]_2]”的错误消息的屏幕截图:

重新运行具有附加公式列的表脚本时公式列“QcParameter [%r rel.]_3]”的错误消息的屏幕截图:

我问这个冗长问题的原因是我假设在方法 nr 中使用变量“d0Row”肯定有某种原因。 2、不是吗?对于方法nr。 3 我只是删除了这个变量(见下面的屏幕截图),结果公式的表现“更好”,即,当通过点点击临时添加新行到数据表时,没有错误消息。

因此,我的问题是,我删除“d0Row”变量是否做错了什么。 在现实生活中,我正在使用更大的数据表,并且无法单独检查每一行的准确性。
我不完全确定我正在关注您的问题,但我看到的是,当您复制公式 1 时,您对 dt 的引用丢失了。在示例 2 和 3 中,最终数据表引用指向数据表“ExampleTable”,并且数据表名称(如图所示)为“Example Table 2”。再说一次,如果我理解你的问题,我认为你可以通过使用“Current Daa Table()”来解决问题。
curBatch = :BatchId;
curCondition = :StorageCondition;
theRows = Current Data Table() << get rows where(
:BatchId == curBatch & :StorageCondition == curCondition & :Time == 0
);
d0Row = theRows[1];
d0Time = :QcParameter[d0Row];
If( Row() == 11,
Show( therows, d0Time )
);
(100 * :QcParameter) / d0Time;
与这个问题不完全相关,但您可以使用一些 Col 函数魔法来“简化”公式。 这些建议应该有效,但您应该在替换旧的工作公式之前验证它们
dt << New Column("FormulaA", Numeric, Continuous, Formula(
100 * :QcParameter / Col Sum(If(:Time == 0, :QcParameter, .),:BatchID, :StorageCondition)
));
如果数据的排序方式使得时间 0 始终位于该组的第一位,则可以使其更容易理解(也许)
dt << New Column("FormulaB", Numeric, Continuous, Formula(
100 * :QcParameter / QcParameter[Col Min(Row(), :BatchID, :StorageCondition)]
));