如果 Excel 自定义函数不使用 共享运行时,它将在 仅限 JavaScript 的运行时中运行。 在该运行时中,身份验证通常需要与任务窗格进行对话流和令牌共享。
使用 OfficeRuntime.displayWebDialog 登录,使用 OfficeRuntime.storage 在运行时之间缓存和共享令牌。
注意
建议对 共享运行时使用自定义函数,除非有特定原因不使用共享运行时。 有关运行时的详细信息,请参阅 Office 外接程序中的运行时。
身份验证工作流
以下工作流是不使用共享运行时的自定义函数的典型工作流。
- 用户在 Excel 单元格中运行自定义函数。
- 自定义函数调用
OfficeRuntime.displayWebDialog以在对话框中打开登录页,用户可在其中输入其凭据。 - 登录页返回对话框的访问令牌。
- 对话框调用 Office.ui.messageParent 将访问令牌发送到自定义函数。 有关详细信息,请参阅 将信息从对话框发送到主机页。
- 自定义函数将访问令牌存储在 中
OfficeRuntime.storage。 - 加载项的任务窗格从
OfficeRuntime.storage获取令牌。
通过示例试用
使用在 自定义函数中使用 OfficeRuntime.storage 示例测试自定义函数与任务窗格之间的令牌存储和检索。
对话框 API
如果令牌不存在,则应使用 OfficeRuntime.displayWebDialog 要求用户登录。 用户输入凭据后,生成的访问令牌可以存储为 中的 OfficeRuntime.storage项。
注意
仅限 JavaScript 的运行时使用对话框对象,该对象与任务窗格使用的浏览器运行时中的对话框对象略有不同。 两者都称为“对话框 API”,但若要在仅限 JavaScript 的运行时中对用户进行身份验证,请使用 OfficeRuntime.displayWebDialog,而不是 Office.ui.displayDialogAsync。
对话框 API 示例
在以下代码示例中, getTokenViaDialog 使用 OfficeRuntime.displayWebDialog 显示对话框。 此示例显示了方法功能,但不是完整的身份验证实现。
/**
* Function retrieves a cached token or opens a dialog box if there is no saved token. Note that this isn't a sufficient example of authentication but is intended to show the capabilities of the displayWebDialog method.
* @param {string} url URL for a stored token.
*/
function getTokenViaDialog(url) {
return new Promise (function (resolve, reject) {
if (_dialogOpen) {
// Can only have one dialog box open at once. Wait for previous dialog box's token.
let timeout = 5;
let count = 0;
const intervalId = setInterval(function () {
count++;
if(_cachedToken) {
resolve(_cachedToken);
clearInterval(intervalId);
}
if(count >= timeout) {
reject("Timeout while waiting for token");
clearInterval(intervalId);
}
}, 1000);
} else {
_dialogOpen = true;
OfficeRuntime.displayWebDialog(url, {
height: '50%',
width: '50%',
onMessage: function (message, dialog) {
_cachedToken = message;
resolve(message);
dialog.close();
return;
},
onRuntimeError: function(error, dialog) {
reject(error);
},
}).catch(function (e) {
reject(e);
});
}
});
}
OfficeRuntime.storage 对象
仅限 JavaScript 的运行时在全局窗口中没有 localStorage 可用的对象,你通常在其中存储数据。 相反,代码应通过使用 OfficeRuntime.storage 在自定义函数和任务窗格之间共享数据来设置和获取数据。
建议的用法
当需要从不使用共享运行时的自定义函数加载项进行身份验证时,代码应检查OfficeRuntime.storage,以查看是否已获取访问令牌。 如果没有,请使用 OfficeRuntime.displayWebDialog 对用户进行身份验证,检索访问令牌,然后将令牌存储在 中 OfficeRuntime.storage 以供将来使用。
存储令牌
以下示例演示如何使用 OfficeRuntime.storage存储和检索令牌。
如果自定义函数进行身份验证,它将接收一个访问令牌,该令牌必须存储在 中 OfficeRuntime.storage。 以下代码示例演示如何调用 storage.setItem 以存储值。 函数 storeValue 是一个自定义函数,用于存储用户的值。 可以对其进行修改以存储所需的任何令牌值。
/**
* Stores a key-value pair into OfficeRuntime.storage.
* @customfunction
* @param {string} key Key of item to put into storage.
* @param {*} value Value of item to put into storage.
*/
function storeValue(key, value) {
return OfficeRuntime.storage.setItem(key, value).then(function (result) {
return "Success: Item with key '" + key + "' saved to storage.";
}, function (error) {
return "Error: Unable to save item with key '" + key + "' to storage. " + error;
});
}
当任务窗格需要访问令牌时,它可以从项中 OfficeRuntime.storage 检索令牌。 以下代码示例演示如何使用 storage.getItem 方法来检索令牌。
/**
* Read a token from storage.
* @customfunction GETTOKEN
*/
function receiveTokenFromCustomFunction() {
const key = "token";
const tokenSendStatus = document.getElementById('tokenSendStatus');
OfficeRuntime.storage.getItem(key).then(function (result) {
tokenSendStatus.value = "Success: Item with key '" + key + "' read from storage.";
document.getElementById('tokenTextBox2').value = result;
}, function (error) {
tokenSendStatus.value = "Error: Unable to read item with key '" + key + "' from storage. " + error;
});
}
一般指导
Office 加载项基于 Web,因此可以使用任何 Web 身份验证技术。 自定义函数没有一种必需的身份验证模式。 从 授权 Office 外接程序中的外部服务 开始,了解设计模式和权衡。
在开发自定义函数时,避免使用以下位置存储数据:
-
localStorage:不使用共享运行时的自定义函数无权访问全局window对象,因此无权访问 中localStorage存储的数据。 -
Office.context.document.settings:此位置不安全,任何使用该加载项的人都可以提取此信息。
后续步骤
了解如何 调试自定义函数。