示例应用程序

应用程序 Global Variables 举例说明了如何定义全局变量以及使用函数在应用程序内或跨应用程序访问它们。此应用程序存储全局变量(全局键和全局值)。它还定义函数以使其他应用程序可以访问这些全局变量、更新它们的值或添加新的全局变量。

应用程序 Global Variables 包含以下表单和函数:

  1. Form1 带有字段全局键全局值,用以输入全局变量名称和变量值。
  2. 函数 getGlobalValue 用于从这个应用程序内或从其他应用程序访问全局变量。
  3. 函数 updateglobalvalue 用于从其他应用程序更新全局变量。
 

a. 函数 – getGlobalValue

getGlobalValue 函数基于指定的键返回全局变量的值。如果 form1 中不存在变量值,则返回空值。

string sample.getGlobalValue(string variableName)
{
if (count(Form1[Global_Key == input.variableName]) >0)
{
col = Form1 [Global_Key == input.variableName];return col.Global_Value;}
else
{
return "";}
}

代码说明

 

string sample.getGlobalValue(string variableName) – 函数 getGlobalValue 使用 variableName 作为字符串参数来定义。该函数返回字符串值。

if (count(Form1[Global_Key == input.variableName]) >0) – 计算 Form1中其值等于传递给函数的参数值的记录数目。如果计数大于 0,则执行 If 语句。如果不是,则执行 else 语句。

col = Form1 [Global_Key == input.variableName]; - 提取 Form1 中其值等于传递给函数的参数值的记录。将它存储在一个名为“col”的集合变量中

return col.Global_Value; – 该函数从名为“col”的集合变量返回 Global_Value

return “”; – 如果 form1 中不存在变量值,则 else 语句会返回空值。

b. 函数 – updateglobalvalue

getGlobalValue 函数基于指定的键更新全局变量的值。如果 form1 中不存在变量值,则在 Form1 中添加具有指定键和值的新记录。

void sample.updateglobalvalue(string variableKey, string variableValue)
{
if (count(Form1[Global_Key == input.variableKey]) >0)
{
col = Form1 [Global_Key == input.variableKey];
col.Global_Value = input.variableValue;
}
else
{
insert into Form1
[
Added_User = zoho.loginuser
Global_Key = input.variableKey
Global_Value = input.variableValue
]
}
}

代码说明:

void sample.updateglobalvalue(string variableKey, string variableValue) - 函数 updateglobalvalue 使用字符串参数 variablekey 和 variableValue 来定义。该函数将不会返回任何值。

if (count(Form1[Global_Key == input.variableKey]) >0) – 计算 Form1 中其值等于传递给函数的参数 (variableKey) 值的记录数目。如果计数大于 0,则执行 If 语句。如果不是,则执行 else 语句。

col = Form1 [Global_Key == input.variableKey]; - 提取 Form1 中其值等于传递给函数的参数 (variableKey) 值的记录。将它存储在一个名为“col”的集合变量中

col.Global_Value = input.variableValue; – 将 col.Global_Value 更新为传递给函数的参数 (variableValue) 的值。

return col.Global_Value; – 该函数从名为“col”的集合变量返回 Global_Value

else - 如果 Form1 中不存在变量值,则执行 else 语句,它会在 Form1 中添加具有指定键和值的新记录。

将函数添加到您的应用程序

  1. 从“脚本”页签中选择“函数”页签。
  2. 单击显示在左侧的函数树中的“新建”图标
  3. 选择选项“编写脚本/复制示例函数”
  4. 复制/粘贴函数代码到文本区域,然后单击“完成”以更新更改。
  5. 名为 getGlobalValue 的函数将创建在命名空间示例下面。
访问全局变量
 

应用程序 Update Global Variables 通过调用上述函数,举例说明了如何访问“Global Variable”应用程序中定义的“全局变量”。此应用程序包含一个名为 Update Global Variables 的无状态表单,它包含 Enter Key 和 Value 字段

  1. 当在上述表单中输入键时,会调用函数 getGlobalValue 以从“Global Variables”应用程序提取键的值并将它显示在当前表单。
  2. 然后通过调用 updateglobalvalue 函数,让修改的值在‘Global Variable’应用程序中更新。
  3. 如果输入了新的键值对,则它将作为新全局变量添加。

调用函数

a. “GlobalVariable”应用程序中定义的函数 getGlobalValue 从 Update_global_Variables 表单中的Enter_Key 字段的用户输入时脚本调用,正如下面的代码中突出显示的那样。当在 Enter_key 字段中输入全局变量键时,执行该脚本。该函数将从 Form1 返回全局变量值并在 Value 字段中更新它。设置变量 deluge 语句用于使用 getGlobalValue 函数返回的值设置 Value 字段 (input.Value)。

b. 函数 updateglobalvalue 从表单的“点击时”脚本调用,用输入键和值作为函数的参数。当点击表单按钮时执行脚本。该函数将在“Global Variable”应用程序中更新全局变量的值。

form Update_global_Variables
{
displayname = "Update global Variables"
store data in zc = false

must have Enter_Key
(
displayname = "Enter Key"
type = text
on user input
{
input.Value = globalvariable.sample.getGlobalValue(input.Enter_Key);
}
)

must have Value
(
type = text
)

actions
{
on_click
(
type = submit
displayname = "on click"
on click
{
globalvariable.sample.updateglobalvalue(input.Enter_Key, input.Value);
}
)
}
}

若要安装应用程序

了解如何将应用程序安装到您的帐户 - 点击此处