> ## Documentation Index
> Fetch the complete documentation index at: https://docs.kasoftware.com/llms.txt
> Use this file to discover all available pages before exploring further.

# excelGet

> 按工作表名称从 Excel 工作簿中获取值，支持特定单元格引用和带可选输出映射的单元格范围查询。

从 Excel 工作簿中获取值。 当 **翻译模式** 设置为 **模板** 时，此操作通常在 [Excel 端口](../../connectors/excel) 的模板文件中使用。 不过，此操作也可以用于专用脚本场景，例如[Script 端口](../../connectors/script)。

## 必需的参数

* **sheet**: 工作簿中要从中检索值的工作表。

## 可选的参数

* **version**：目标工作簿的 Excel 版本。 允许的值为`AUTO`、`95`、`97-2003`、`2007`。 默认为 `AUTO`。
* **file**：Excel 工作簿在磁盘上的完整路径，包括文件名。 必须指定`file`或`handle`参数。
* **handle**：对[excelOpen](./op-excel-open)创建的Excel数据的可读句柄引用。 如果指定`file`参数，则不需要`handle`。
* **map:\***：一组一个或多个输入，其中映射参数的名称位于冒号之前（例如，`map:foo`），值是工作表中单元格的名称或范围。 例如，值为`C1`的属性`map:foo`在操作的指定输出项上创建`foo`属性，其值是在目标工作表的单元格 C1 中找到的值。

  还可以指定一个单元格范围以检索该范围内所有单元格的值。 例如，`C1:C10` 检索单元格 C1 到 C10 的所有值。 可以使用通配符`*`来获取列长度的单元格值。 例如，`C1:C*`检索从 C1 到列末尾的所有值。

## 输出属性

* **\***: 取决于工作表的内容和指定的查询。 如果存在列标题，则它们用于命名输出属性。 此外，如果在输入项上设置`map:*`参数，则输出项包含具有相同名称的属性。 有关其他上下文，请参阅[示例](#示例)。

## 示例

有关其他信息和更多示例，请参阅 [Excel 端口](../../connectors/excel) 文档的 [Excel 到 XML](../../connectors/excel#excel-to-xml) 部分。

### 获取值并映射到新对象

此示例使用 excelGet 操作从指定工作表中的单元格和列中获取特定的范围值，并将它们映射到新项目。 稍后可以在模板中使用新项目来填充 XML 输出文件的特定 XML 元素，作为 Excel 端口模板模式的一部分。 有关 `arc:call` 中使用的 `arc:map` 关键字的详细信息，请参阅 [arc:map](../keyword-reference/op-arc-map)。

```xml theme={null}
<!-- Read the Excel -->
<arc:set attr="excel.file" value="[FilePath]" />
<arc:set attr="excel.sheet" value="Sheet1" />
<arc:set attr="excel.version" value="2007" />

<!-- Map the specific and ranged values to various attributes -->
<arc:setm item="excel">
  map:PONumber = "C5"
  map:PODate = "C6"
  map:DeliveryDate = "C7"
  map:ShipDate = "C8"
  map:BillerName = "I11"
  map:ShipperName = "C11"
  map:ShipperAddressLine1 = "C12"
  map:ShipperCity = "C13"
  map:ShipperState = "C14"
  map:ShipperZip = "C15"
  map:LineUPC = "B19:B*"
  map:LineQty = "C19:C*"
  map:LineUnit = "D19:D*"
  map:LinePrice = "E19:E*"
  map:LineDesc = "F19:F*"
  map:LineAllowanceRate = "I19:I*"
  map:LineAllowanceType = "J19:J*"
</arc:setm>

<!-- Calling the excelGet operation and using the arc:map keyword to map the output of the operation from the "result" item to a new "data" item that can be used later -->
<arc:call op="excelGet" in="excel" out="result">
  <arc:map from="result" to="data" map="*=*" />
</arc:call>
```

### 在 Script 端口中使用 excelGet

此示例使用 [Script 端口](../../connectors/script) 中的 excelGet 操作打开磁盘上现有的 Excel 工作簿并从特定单元格读取数据。 然后关闭 Excel 工作簿，脚本将该数据作为新的输出文件推送。

```xml theme={null}
<!-- Creating the input item for the operation and passing it in -->
<arc:set attr="excel.file" value="C:\Temp\movies.xlsx" />
<arc:call op="excelOpen" in="excel" out="result" >
  <!-- Resolving the handle from excelOpen to use in excelGet -->
  <arc:set attr="get.handle" value="[result.handle]" />
  <arc:set attr="get.sheet" value="film" />
  <arc:set attr="get.version" value="2007" />
  <arc:set attr="get.map:favoritemovie" value="A2" />
  <arc:set attr="get.map:favoritemovieyear" value="B2" />
  <!-- Calling excelGet inside excelOpen to use the handle -->
  <arc:call op="excelGet" in="get" out="out">
    <!-- Creating some output data and file from the data read from the excel sheet -->
    <arc:set attr="output.data" value="My favorite movie is [out.favoritemovie] and it came out in [out.favoritemovieyear]." />
    <arc:set attr="output.filename" value="results.txt" />
    <!-- Using the arc:finally keyword to execute the closing of the handle last -->
    <arc:finally>
      <!-- Calling excelClose to close the handle -->
      <arc:call op="excelClose" in="excel" out="close">
        <!-- Check to ensure the handle was closed and throw an error if it was not -->
        <arc:exists attr="close.success" >
          <arc:else>
            <arc:throw code="CloseFailed" desc="The handle was not closed successfully." />
          </arc:else>
        </arc:exists>
      </arc:call>
    </arc:finally>
  </arc:call>
</arc:call>
<!-- Push the output item out as a file -->
<arc:push item="output" />
```
