【WPSJS 加载项】ToggleButton控件onAction回调参数顺序不符合标准

VBA Ribbon - toggleButtononAction 回调函数的参数顺序应为 (control, pressed)

然而,在 WPS Office 环境中,该回调函数接收到的参数顺序似乎被颠倒为 (pressed, control)。这导致直接使用标准 API 编写的代码在 WPS 中无法正常工作(例如,control 变量实际接收到的是布尔值,而 pressed 变量接收到的是对象)。

ribbon.xml

<customUI xmlns="http://schemas.microsoft.com/office/2006/01/customui" onLoad="OnLoad">
    <ribbon startFromScratch="false">
        <tabs>
            <tab id="CustomTab" label="SampleTab">
                <group id="CustomGroup" label="SampleGroup">
                    <!-- 定义一个 ToggleButton 控件 -->
                    <toggleButton 
                        id="ToggleBtn01" 
                        label="Toggle Button" 
                        getLabel="GetLabel" 
                        getPressed="GetPressed" 
                        onAction="OnAction" 
                        size="large"/>
                </group>
            </tab>
        </tabs>
    </ribbon>
</customUI>

index.ts

// 定义 RibbonControl 接口
interface IRibbonControl {
    Id: string;
    // 其他标准属性...
}

/**
 * ToggleButton onAction 处理函数
 * 标准参数顺序应为: (control: IRibbonControl, pressed: boolean)
 * WPS 当前行为: (pressed: boolean, control: IRibbonControl)
 */
function OnAction(arg1: unknown, arg2?: unknown) {
  console.log("Debug: Arguments received ->", arguments);
  
  let control: IRibbonControl;
  let pressed: boolean;

  // 检测参数类型以确定参数顺序
  if (typeof arg1 === 'boolean') {
    // WPS 环境: (pressed, control)
    console.warn("Detected WPS behavior: Arguments are reversed (pressed, control).");
    pressed = arg1 as boolean;
    control = arg2 as IRibbonControl;
  } else {
    // 标准 Office 环境: (control, pressed)
    console.log("Detected Standard Office behavior: Arguments are (control, pressed).");
    control = arg1 as IRibbonControl;
    pressed = arg2 as boolean;
  }

  // 业务逻辑
  if (control) {
      console.log(`Control ID: ${control.Id}, State: ${pressed ? "Pressed" : "Released"}`);
  }
}

// 导出回调函数供 Ribbon 调用
export const RibbonCallbacks = {
  OnAction,
  // ...
}
广东省
浏览 101
收藏
3
分享
3 +1
1
+1
全部评论 1
 
陈俊东
陈俊东

@金山办公

感谢反馈,后续我们会尽量考虑兼容旧加载项前提,兼容vba的写法
· 广东省
回复