# ApplyMap - 指令碼函數

ApplyMap 指令碼函數用於將某個運算式的輸出對應到先前載入的對應表。

語法：

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

傳回的資料類型： 雙值

引數：

引數

| 引數 | 描述 |
| --- | --- |
| 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';
```

產生的表格 (銷售人員) 如下所示：

Resulting table

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

## 瞭解更多資訊

* [](https://help.qlik.com/zh-TW/sense/May2025/Subsystems/Hub/Content/Sense_Hub/Scripting/ScriptPrefixes/Mapping.htm)[對應](https://help.qlik.com/zh-TW/sense/May2025/Subsystems/Hub/Content/Sense_Hub/Scripting/ScriptPrefixes/Mapping.htm)
* [在指令碼中使用引號](https://help.qlik.com/zh-TW/sense/May2025/Subsystems/Hub/Content/Sense_Hub/Scripting/use-quotes-in-script.htm)