Peek - 脚本函数
Peek() 用于在表格中返回已经加载或内部内存中存在的行的字段值。可以将行号指定为表格。
Syntax:
Peek(
field_name
[, row_no[, table_name ] ])
Return data type: 双
Arguments:
参数 | 说明 |
---|---|
field_name | 需要返回值的字段的名称。 输入值必须为字符串(例如引用的文字)。 |
row_no |
表格中的行用于指定所需的字段。可以是表达式,但解算结果必须为整数。0 表示第一个记录,1 表示第二个记录,以此类推。负数表示从表格末端开始计算的顺序。-1 表示最后读取的记录。 如果未指定row_no,则假定为 -1。 |
table_name | 表格标签不能以冒号结束。如果未指定table_name,则假定为当前表格。如果用于 LOAD 语句之外或指向另外一个表格,则必须包括 table_name。 |
Limitations:
在内部表格的首个记录中,此函数返回 NULL 值。
Example:
将示例脚本添加到应用程序并运行。然后,要将结果列中列出的字段添加到应用程序中的表格才能查看结果。
EmployeeDates:
Load * Inline [
EmployeeCode|StartDate|EndDate
101|02/11/2010|23/06/2012
102|01/11/2011|30/11/2013
103|02/01/2012|
104|02/01/2012|31/03/2012
105|01/04/2012|31/01/2013
106|02/11/2013|
] (delimiter is '|');
FirstEmployee:
Load EmployeeCode, Peek('EmployeeCode',0) As EmpCode
Resident EmployeeDates;
EmpCode = 101,因为 Peek('EmployeeCode',0) 返回表格 EmployeeDates 的 EmployeeCode 中的第一个值。
替代参数 row_no 返回表格中其他行的值,如下所示:
Peek('EmployeeCode',2) 用于返回表格中的第三个值:103.
但是,请注意,如果没有将表格指定为第三个参数 table_name,则此函数引用当前表格(在此例中,为内部表格)。 Peek('EmployeeCode',-2) 的结果是一个倍数值:
员工代码 | EmpCode |
---|---|
101 |
- |
102 | - |
103 | 101 |
104 | 102 |
105 | 103 |
106 | 104 |
Example:
FirstEmployee:
Load EmployeeCode, Peek('EmployeeCode',-2,'EmployeeDates') As EmpCode
Resident EmployeeDates;
By specifying the argument table_name as 'EmployeeDates', the function returns the second-to-last value of EmployeeCode in the table EmployeeDates: 105.
Example:
Peek() 函数可用于引用尚未加载的数据。
将示例脚本添加到应用程序并运行。然后,要将结果列中列出的字段添加到应用程序中的表格才能查看结果。
T1:
LOAD * inline [
ID|Value
1|3
1|4
1|6
3|7
3|8
2|1
2|11
5|2
5|78
5|13
] (delimiter is '|');
T2:
LOAD
*,
IF(ID=Peek('ID'), Peek('List')&','&Value,Value) AS List
RESIDENT T1
ORDER BY ID ASC;
DROP TABLE T1;
Create a table in a sheet in your app with ID, List, and Value as the dimensions.
ID | 列表 | 值 |
---|---|---|
1 | 3,4 | 4 |
1 | 3,4,6 | 6 |
1 | 3 | 3 |
2 | 1,11 | 11 |
2 | 1 | 1 |
3 | 7,8 | 8 |
3 | 7 | 7 |
5 | 2,78 | 78 |
5 | 2,78,13 | 13 |
5 | 2 | 2 |
IF() 语句是根据临时表格 T1 构建。
Peek('ID') 引用当前表格 T2 的上一行中的字段 ID。
Peek('List') 引用当前表格 T2 的上一行中的字段 List,目前正在构建要解算的表达式。
如下运算语句:
如果 ID 的当前值与 ID 的上一个值相同,则写入 Peek('List') 的值串联 Value 的当前值。否则,只写入 Value 的当前值。
如果 Peek('List') 已经包含串联结果,则会将 Peek('List') 的新结果串联至其当前值。
Example:
LOAD A, B, numsum( B, Peek( 'Bsum' ) ) as Bsum...;
在 Bsum 中创建 B 的累计。