向 Excel 电子表单写入信息

现在,我们已设定了构造模型和计算零件代号必需的所有参数值。我们要撰写的最后一个规则会将与此文件关联的零件代号以及其他参数值传递到 Excel 电子表单。其中的某些零件代号包含在 iPart 中,而螺钉零件代号则是由此规则生成的。

update_excel_spreadsheet_rule 可利用“编辑规则”对话框“代码段”区域中“Excel 数据链接”类别中所提供的一组函数。

  1. 添加名为 update_excel_spreadsheet_rule 的新规则。

    此规则可根据模型状态填充必要的单元格。在电子表单中,要向其中传递数据的所有单元格已经命名,从而与要撰写的信息相对应。

  2. 创建规则的第一部分,用以打开电子表单并撰写前三个单元格值。

    复制代码块

    GoExcel.CellValue("part_number.xls", "Sheet1", "Block_Type") = component_type
    GoExcel.CurrentCellValue("Block_Style") = block
    GoExcel.CurrentCellValue("Block_Part_Number") = iProperties.Value("manifold_block:1", "Project", "Part Number")

    在此部分中,我们将参考此教程项目附带的 part_number.xls 电子表单文件。然后设定 Block_TypeBlock_StyleBlock_Part_Number 单元的值。前两个值是根据部件参数设定的,最后一个值是根据块的“零件代号”iProperty 设定的。

  3. 创建规则的下一部分,用以将三个端口尺寸的值写入电子表单。

    复制代码块

    GoExcel.CurrentCellValue("port_a_size") = port_a_size
    If block = "tee" Then
    GoExcel.CurrentCellValue("port_b_size") = port_b_size
    Else
    GoExcel.CurrentCellValue("port_b_size") = "N/A"
    End If
    GoExcel.CurrentCellValue("port_c_size") = port_c_size

    请注意我们是如何对弯头样式集成块使用 N/A 占位符值的。

  4. 在规则中再添加一部分,用于根据集成块零部件中包含的参数指定单元格值。

    复制代码块

    GoExcel.CurrentCellValue("block_depth") = Parameter("manifold_block:1", "block_depth")
    GoExcel.CurrentCellValue("block_width") = Parameter("manifold_block:1", "block_width")
    GoExcel.CurrentCellValue("block_height") = Parameter("manifold_block:1", "block_height")

    请注意此部分中对 manifold_block:1 零部件的参考。

  5. 在规则中添加一部分,用以按照 screw_part_number_rule 计算得出的活接头零部件和螺钉零件的零件代号指定单元值。

    复制代码块

    GoExcel.CurrentCellValue("port_a_union_cap") = port_a_union_part_number
    GoExcel.CurrentCellValue("port_a_screw_kit") = port_a_screw_part_number
    If block = "tee" Then
    GoExcel.CurrentCellValue("port_b_union_cap") = port_b_union_part_number
    GoExcel.CurrentCellValue("port_b_screw_kit") = port_b_screw_part_number
    Else
    GoExcel.CurrentCellValue("port_b_union_cap") = "N/A"
    GoExcel.CurrentCellValue("port_b_screw_kit") = "N/A"
    End If
    GoExcel.CurrentCellValue("port_c_union_cap") = port_c_union_part_number
    GoExcel.CurrentCellValue("port_c_screw_kit") = port_c_screw_part_number

    就像规则的前一部分中的 port_b_size 一样,注意对 Port B 相关值的条件控制。

  6. 若要结束此规则,请将更改保存到电子表单中。使用“代码段”区域中“Excel 数据链接”节点中提供的 GoExcel.Save 函数。

    复制代码块

    GoExcel.Save
  7. 单击“确定”保存并关闭规则。关闭后执行规则时,电子表格会随之更新。执行之后,规则会自动关闭电子表单。
  8. 通过 Excel 打开 part_number.xls 电子表单,并确认是否已更新。由于电子表单已经通过 Excel 打开时规则不能更新电子表单,因此请先关闭电子表单文档,然后再次执行规则。

上一页 | 下一页