# 设置变量

{% hint style="info" %}

### 逻辑-设置变量组件

{% endhint %}

<figure><img src="/files/K55SNYmvtLGCPfxO142O" alt="" width="285"><figcaption></figcaption></figure>

{% hint style="info" %}
你可以把变量设置为特定的字段；

比如：你要设置一个URL为变量，那么我们可以用“设置变量”组件，选择Custom，然后把URL填写到输入框内，并且任意一个变量名称；
{% endhint %}

<figure><img src="/files/kFcZqB2nqGC27sUVACx4" alt=""><figcaption></figcaption></figure>

{% hint style="info" %}
您可以使用它来设置为任何变量值。它可以是任何类型的纯文本，也可以是Javascript 代码。
{% endhint %}

<figure><img src="/files/YEkgtsUVBdB4hvaDC3LF" alt=""><figcaption></figcaption></figure>

{% hint style="info" %}

### 变量的表达式（示例）

{% endhint %}

为您的变量添加一个值：

```
{{Score}} + 5
```

计算变量的总和：

```Plain
{{Score}} + {{Answer}}
```

将变量相乘：

```Plain
{{Score}} * {{Multiplier}}
```

计算百分比：

```Plain
{{Score}} * 100 / {{Max Score}}
```

从全名中提取名字

```Plain
{{Full name}}.split(' ')[0]
```

将现有变量转换为大写或小写：

```Plain
{{Name}}.toUpperCase()
```

```Plain
{{Name}}.toLowerCase()
```

这也可以是 Javascript 代码。它将读取代码的返回值并将其设置为您的变量。

```javascript
const name = 'John' + 'Smith'
return name
```

如果您不提供`return`关键字，那么它将自动添加到代码的开头

```javascript
'John' + 'Smith'
```

```javascript
return 'John' + 'Smith'
```

{% hint style="info" %}

在脚本中的变量，是不会被解析的，所以它是真正的 JavaScript 变量。因此，如果你编写`"{{My variable}}"`，它将解析为变量 ID（类似于`vclfqgqkdf000008mh3r6xakty`）。此时的你，需要删除双引号才能正确获取变量内容值。例如，

* ❌ `"{{URL base}}/path"`=>`vclfqgqkdf000008mh3r6xakty/path`
* ✅ `{{URL base}} + '/path'`=>`https://domain.com/path`
* ✅ `${{{URL base}}}/path`=>`https://domain.com/path`
  {% endhint %}

### **Result ID（结果ID）**

这将使用当前**Result ID**设置您的变量。它可以被视为当前聊天用户的用户 ID。<br>

### **Map item with same index（具有相同索引的映射项）**

这是一个方便的设置，它允许您轻松地从一个列表中获取与另一个列表中具有相同索引的项。

当您从另一个服务提取数据时，有时会有两个列表:标签和id。标签是显示给用户的数据，id是用于对该外部服务的其他请求的数据。

该值块允许您从具有与标签中Label相同索引的Id中查找Id

<figure><img src="/files/EsmAS470aWN1Pd82ump2" alt="" width="375"><figcaption></figcaption></figure>

<figure><img src="/files/4WheJaOlICPPGXvs8pv0" alt="" width="375"><figcaption></figcaption></figure>

### **Get user’s geo location（获取用的的地理位置）**

您可以提供以下自定义代码：

```javascript
function getLocation() {
  return new Promise((resolve) => {
    navigator.geolocation.getCurrentPosition(
      (position) =>
        resolve(`${position.coords.latitude}, ${position.coords.longitude}`),
      (error) => resolve('error'),
      { enableHighAccuracy: true, timeout: 5000 }
    )
  })
}

const coords = await getLocation()

// Check for error
if (coords === 'error') {
  return 'Unable to get location'
}

return coords
```

{% hint style="info" %}
注意：

自定义函数仅在客户端浏览器上执行时才有效，因此您需要确保启用“在客户端上执行”选项。

如果不是调试代码的化，建议您关闭“客户端执行”选项，确保发布后的流程可以正常运行
{% endhint %}

<figure><img src="/files/8xyItkkRRGi1wEv0GceV" alt="" width="375"><figcaption></figcaption></figure>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://doc.nyai.chat/jin-jie-jiao-cheng-gong-zuo-liu/luo-ji-zu-jian/she-zhi-bian-liang.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
