You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用Power Automate或Office Scripts读取Excel表单控件?

在线Excel中用Office Scripts和Power Automate读取表单控件值

一、Office Scripts 读取表单控件核心方法

在线Excel仅支持表单控件(Form Controls),ActiveX控件无法在在线环境使用。以下是各类控件的读取实现:

1. 读取复选框(Checkbox)状态

表单控件的复选框属于Shape对象,需通过形状名称或遍历识别,获取其checked属性:

function main(workbook: ExcelScript.Workbook) {
  const sheet = workbook.getWorksheet("Sheet1"); // 替换为你的工作表名称
  const checkboxShape = sheet.getShape("Check Box 1"); // 替换为复选框的形状名称

  // 判断是否为复选框控件
  if (checkboxShape.getFormControlType() === ExcelScript.FormControlType.checkBox) {
    const isChecked = checkboxShape.getFormControl().asCheckBox().getChecked();
    console.log("复选框状态:", isChecked);
    return { checkboxStatus: isChecked };
  }
}

2. 读取下拉列表(Drop Down)选中值

表单控件的下拉列表需获取其选中索引对应的选项值:

function main(workbook: ExcelScript.Workbook) {
  const sheet = workbook.getWorksheet("Sheet1");
  const dropdownShape = sheet.getShape("Drop Down 1"); // 替换为下拉列表的形状名称

  if (dropdownShape.getFormControlType() === ExcelScript.FormControlType.dropDown) {
    const dropdownControl = dropdownShape.getFormControl().asDropDown();
    const selectedIndex = dropdownControl.getSelectedIndex();
    const allOptions = dropdownControl.getItems();
    const selectedValue = allOptions[selectedIndex];

    console.log("下拉选中值:", selectedValue);
    return { dropdownSelectedValue: selectedValue };
  }
}

3. 读取按钮(Button)属性

按钮控件主要用于触发动作,可读取其显示文本:

function main(workbook: ExcelScript.Workbook) {
  const sheet = workbook.getWorksheet("Sheet1");
  const buttonShape = sheet.getShape("Button 1"); // 替换为按钮的形状名称

  if (buttonShape.getFormControlType() === ExcelScript.FormControlType.button) {
    const buttonText = buttonShape.getTextFrame().getTextRange().getText();
    console.log("按钮文本:", buttonText);
    return { buttonText: buttonText };
  }
}

批量读取所有表单控件

如果需要一次性读取工作表内所有表单控件,可遍历所有形状筛选:

function main(workbook: ExcelScript.Workbook) {
  const sheet = workbook.getWorksheet("Sheet1");
  const allShapes = sheet.getShapes();
  const controlValues: Record<string, any> = {};

  for (const shape of allShapes) {
    const controlType = shape.getFormControlType();
    if (controlType === ExcelScript.FormControlType.none) continue; // 跳过非表单控件形状

    switch (controlType) {
      case ExcelScript.FormControlType.checkBox:
        controlValues[shape.getName()] = shape.getFormControl().asCheckBox().getChecked();
        break;
      case ExcelScript.FormControlType.dropDown:
        const dropdown = shape.getFormControl().asDropDown();
        controlValues[shape.getName()] = dropdown.getItems()[dropdown.getSelectedIndex()];
        break;
      case ExcelScript.FormControlType.button:
        controlValues[shape.getName()] = shape.getTextFrame().getTextRange().getText();
        break;
      // 可扩展其他表单控件类型(如单选按钮等)
    }
  }

  console.log("所有控件值:", controlValues);
  return controlValues;
}

二、Power Automate 调用Office Scripts获取控件值

Power Automate无法直接读取表单控件,需通过调用Office Scripts实现,步骤如下:

  • 新建一个手动触发的流(或根据需求选择其他触发条件,如文件更新)。
  • 添加Excel Online (Business)动作 → 运行脚本。
  • 配置动作参数:
    • 选择存储Excel文件的位置(OneDrive/SharePoint)。
    • 选择目标Excel文件和工作表。
    • 选择你之前保存的Office Scripts脚本。
  • 运行流后,脚本返回的控件值会作为输出数据,可后续用于其他操作(如发送邮件、存入数据库等)。

注意事项

  • 表单控件的名称可通过Excel在线版的开发工具选项卡 → 选择对象,点击控件后在右上角的名称框查看。
  • 若控件绑定到单元格,也可直接读取绑定单元格的值作为替代方案(部分场景更简便)。

内容的提问来源于stack exchange,提问作者user108939

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.24 11:24:55