studio-web
latest
false
重要 :
请注意,此内容已使用机器翻译进行了部分本地化。 新发布内容的本地化可能需要 1-2 周的时间才能完成。
UiPath logo, featuring letters U and I in white

Studio Web 用户指南

上次更新日期 2025年9月22日

脚本

“脚本”活动使用 JavaScript 在您的工作流中启用自定义数据操作。它集成到 API 工作流中,允许您:

  • 提取、格式化和重组 API 响应。
  • 汇总并整合数据。
  • 执行计算和数据转换。
  • 标准化后续步骤的数据格式。

使用“脚本”活动优化嵌套或零散的 API 响应,然后将其传递到后续工作流活动。

已知限制

  • 仅限于数据操作,不能直接执行外部 API 请求。
  • 在工作流执行上下文中运行,仅允许访问现有工作流数据和步骤输出。

使用“脚本”活动

要将“脚本”活动添加到工作流中:
  1. 在 API 工作流设计器画布上,选择加号 (+) 图标。系统将显示“添加活动”菜单。
  2. 选择脚本
  3. “属性”面板中,展开“表达式编辑器”,然后在“代码”面板中写入 JavaScript 逻辑。
  4. 测试工作流以执行活动并生成输出字段以供以后使用。
为获得最佳使用效果,请遵循以下建议:
  • 使用return语句高效构建 JSON 输出。
  • 使用.map()将数组转换为结构化对象。
  • 使用 Autopilot 表达式生成器根据工作流上下文自动生成 JavaScript,从而最大程度地减少手动编码工作。

脚本活动示例

以下示例使用“脚本”活动将来自多个工作流步骤的数据整合到结构化的 JSON 对象中。此方法对于 Workday API 特别有用,这些 API 通常需要多次端点调用来检索完整的数据集,例如员工详细信息、经理和直接下属。

下图显示了原始工作流,通过脚本活动,我们会将其合并为 JSON 对象。
Workday 工作流

打开“调试配置”窗口,然后粘贴并保存以下 JSON 语法:

{
  "first_name": "Betty",
  "last_name": "Liu"
}{
  "first_name": "Betty",
  "last_name": "Liu"
}
  1. 在 API 工作流设计器画布上,添加“脚本”活动。
  2. 打开“表达式编辑器”并粘贴以下实现:
    return {
    
        // Details on the worker
        worker: {
            name: $currentItem.descriptor,
            email: $currentItem.person.email
        },
    
        // Details of their manager
        manager: {
            name: $context.outputs.Workers_3.content.descriptor,
            email: $context.outputs.Workers_3.content.person.email
        },
    
            // Details for their direct reports
        reports: $context.outputs.Workday_REST_HTTP_Request_4.content.data.map(report => ({
            name: report.descriptor,
            email: report.primaryWorkEmail
        }))
    }return {
    
        // Details on the worker
        worker: {
            name: $currentItem.descriptor,
            email: $currentItem.person.email
        },
    
        // Details of their manager
        manager: {
            name: $context.outputs.Workers_3.content.descriptor,
            email: $context.outputs.Workers_3.content.person.email
        },
    
            // Details for their direct reports
        reports: $context.outputs.Workday_REST_HTTP_Request_4.content.data.map(report => ({
            name: report.descriptor,
            email: report.primaryWorkEmail
        }))
    }
请注意此示例中的以下方面:
  • 使用$context.outputs检索之前的 API 调用中的数据。
  • 使用.map()将数组转换为结构化的报告列表。
  • 将多个 API 响应合并到单个 JSON 对象中。
  • 已知限制
  • 使用“脚本”活动
  • 脚本活动示例

此页面有帮助吗?

获取您需要的帮助
了解 RPA - 自动化课程
UiPath Community 论坛
Uipath Logo
信任与安全
© 2005-2025 UiPath。保留所有权利。