# ApplyMap - 脚本函数

ApplyMap 脚本函数用于将表达式输出映射至先前加载的映射表。

语法：

ApplyMap('map\_name', expression [ , default\_mapping ] )

返回数据类型： 双

参数：

ApplyMap 参数

| 参数 | 说明 |
| --- | --- |
| map\_name | 以前通过 mapping load 或 mapping select 语句创建的映射表的名称。该名称必须用单直引号引起来。<br><br>警告注释如果您在宏扩展变量中使用此函数并引用不存在的映射表，函数调用将失败且不会创建字段。 |
| expression | 表达式，即将被映射的结果。 |
| default\_mapping | 如果已指定，即如果映射表不包含与 expression 相匹配的值，则可将此值用作默认值。如果未指定，则 expression 的值将原样返回。 |

警告注释ApplyMap 的输出字段不应当具有和其输入字段中的一个相同的名称。这可能导致意外结果。不适合使用的示例：ApplyMap('Map', A) as A.

示例：

在此例中，我们加载了销售人员和国家代码（表示销售人员所居住的国家）的列表。我们使用表格将国家代码映射到国家，以便将国家代码替换为国家名称。在映射表中仅定义了三个国家，其他国家代码已映射到'Rest of the world'。

```
// Load mapping table of country codes: map1: mapping LOAD *  Inline [ CCode, Country Sw, Sweden Dk, Denmark No, Norway ] ;  // Load list of salesmen, mapping country code to country // If the country code is not in the mapping table, put Rest of the world Salespersons: LOAD *,  ApplyMap('map1', CCode,'Rest of the world') As Country  Inline [ CCode, Salesperson  Sw, John Sw, Mary Sw, Per  Dk, Preben Dk, Olle No, Ole  Sf, Risttu ] ;  // We don't need the CCode anymore Drop Field 'CCode';
```

最终生成的表格（销售人员）如下所示：

Example 1

| Salesperson | Country |
| --- | --- |
| John | Sweden |
| Mary | Sweden |
| Per | Sweden |
| Preben | Denmark |
| Olle | Denmark |
| Ole | Norway |
| Risttu | Rest of the world |

## 了解详情

* [](https://help.qlik.com/zh-CN/qlikview/May2024/Subsystems/Client/Content/QV_QlikView/Scripting/ScriptPrefixes/Mapping.htm)[Mapping](https://help.qlik.com/zh-CN/qlikview/May2024/Subsystems/Client/Content/QV_QlikView/Scripting/ScriptPrefixes/Mapping.htm)
* [在脚本中使用引号](https://help.qlik.com/zh-CN/qlikview/May2024/Subsystems/Client/Content/QV_QlikView/Scripting/use-quotes-in-script.htm)