我已收到我想討論的公式大約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)]
));