简体   繁体   English

通过 Excel 工作表中的按钮进行 Office 身份验证

[英]Office Authentication from a button in an Excel worksheet

I would like to have a button in a worksheet that:我想在工作表中有一个按钮:

  1. reads some data from the worksheet,从工作表中读取一些数据,
  2. makes a call to a remote endpoint using Office Authentication,使用 Office 身份验证调用远程端点,
  3. writes the response back in the worksheet.将响应写回到工作表中。

There are several potential ways to do this: Office Scripts, Office Add-ins (with the new JS API), VBA, and old-style add-ins (COM or Excel Add-ins).有几种可能的方法可以做到这一点:Office 脚本、Office 加载项(使用新的 JS API)、VBA 和旧式加载项(COM 或 Excel 加载项)。 All of them however present difficulties that I couldn't solve.然而,所有这些都提出了我无法解决的困难。

Office Scripts办公脚本

Neither OfficeRuntime.auth nor Office.auth are available from Office Scripts. Office 脚本中均不提供OfficeRuntime.authOffice.auth Moreover, the fetch API is only available for scripts saved on a user's OneDrive folder, not for scripts saved in Sharepoint.此外, fetch API 仅适用于保存在用户 OneDrive 文件夹中的脚本,不适用于保存在 Sharepoint 中的脚本。

Office Add-ins办公插件

I could almost make it work, but only as a task panel add-in.我几乎可以让它工作,但只能作为任务面板插件。

The code would be something like the following:代码将类似于以下内容:

async function button_click() {
    await Excel.run( async (context) => {

        let my_sheet = context.workbook.worksheets.getItem('my_sheet');
        let my_data = my_sheet.getRange('my_data');
        let request_data = do_stuff(my_data);
        
        let token = await OfficeRuntime.auth.getAccessToken();
        let headers = new Headers();
        headers.set('Authorization', token);
        let response = await fetch(SERVICE_URL,{
            headers: headers,
            body: request_data
        })

        let my_output = my_sheet.getRange('my_output');
        my_output.values = response.body;
        
    })
}

The problem here is: how can I trigger the function from a button in the worksheet ?这里的问题是:如何从工作表中的按钮触发 function ? I have several different buttons and I prefer to keep the UI in the worksheet.我有几个不同的按钮,我更喜欢将 UI 保留在工作表中。

VBA VBA

VBA has the opposite problem: it is easy to trigger actions from the worksheet, but there is no library (that I know of) implementing Office Authentication. VBA 有相反的问题:很容易从工作表触发操作,但没有库(据我所知)实现 Office 身份验证。 Even the excellent VBA-Web only implements OAuth2 with client credentials (username and password).即使是优秀的VBA-Web也只使用客户端凭据(用户名和密码)实现 OAuth2。

COM and Excel Add-ins COM 和 Excel 插件

Honestly, I haven't tried.老实说,我没试过。 Add-ins deployment and updating are cumbersome and have caused me many headaches in the past.插件部署和更新很麻烦,过去曾让我很头疼。 I would rather use one of the other methods if possible.如果可能的话,我宁愿使用其他方法之一。

My question: How can I call a remote service using Office Authentication from a button in a worksheet?我的问题:如何从工作表中的按钮使用 Office 身份验证调用远程服务?

More in detail:更详细:

Is there a way in Office Scripts to access Office.auth ? Office 脚本中有没有办法访问Office.auth

How can I invoke a function in an Office Add-in from a button in a worksheet?如何从工作表中的按钮调用 Office 加载项中的 function?

Is there something equivalent to OfficeRuntime.auth.getAccessToken in VBA? VBA中是否有等同于OfficeRuntime.auth.getAccessToken的东西?

Is there a way in Office Scripts to access Office.auth? Office 脚本中是否有访问 Office.auth 的方法?

No. Office Scripts does not have access to any of the Office JS APIs.不可以。Office 脚本无权访问任何 Office JS API。 You can only use the ExcelScript API surface .您只能使用ExcelScript API 表面

How can I invoke a function in an Office Add-in from a button in a worksheet?如何从工作表中的按钮调用 Office 加载项中的 function?

You could make a content add-in .您可以制作一个内容插件 However, this would not be the same as a Script button that uses Excel shapes as buttons.但是,这与使用 Excel 形状作为按钮的脚本按钮不同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM