OfficeRuntime.Storage interface

异步、全局和持久键值存储。

注解

API 集:SharedRuntime 1.1,邮箱 1.10

Excel、PowerPoint 和 Word 加载项的 SharedRuntime 1.1 要求集中提供了此接口。也从 Outlook 的邮箱要求集 1.10 开始提供。

重要提示: 在 Outlook 中,仅对 Windows 上的 Outlook 中实现的 基于事件的激活 功能提供支持。 Mac 版 Outlook 或 Outlook 网页版不支持此界面。

存储限制为每个域 10 MB,可由多个加载项共享。

方法

getItem(key)

基于项的键从存储中检索项。 返回承诺。 如果 Promise 未解析,则返回 null。

getItems(keys)

基于其密钥从存储中检索多个项目。 返回承诺。 如果 Promise 未解析,则返回 null。

getKeys()

从存储中检索所有密钥的数组。 返回承诺。

removeItem(key)

基于项的键从存储中删除项。 返回承诺。

removeItems(keys)

从存储中删除多个项目。 返回承诺。

setItem(key, value)

将键值对设置为存储中或更新现有的键值对。 返回承诺。

setItems(keyValues)

将多个项目设置为存储中或更新存储中的多个项目。 返回承诺。

方法详细信息

getItem(key)

基于项的键从存储中检索项。 返回承诺。 如果 Promise 未解析,则返回 null。

getItem(key: string): Promise<string | null>;

参数

key

string

要检索的项目的密钥。 必须为字符串。

返回

Promise<string | null>

注解

API 集:SharedRuntime 1.1,邮箱 1.10

Excel、PowerPoint 和 Word 加载项的 SharedRuntime 1.1 要求集中提供此方法。也从 Outlook 的邮箱要求集 1.10 开始提供。

重要提示: 在 Outlook 中,仅对 Windows 上的 Outlook 中实现的 基于事件的激活 功能提供支持。 Mac 版 Outlook 或 Outlook 网页版不支持此方法。

示例

// Retrieve a value from the shared runtime storage by its key.
async function getItem() {
    const key = document.getElementById('readKey').value.trim();

    const value = await OfficeRuntime.storage.getItem(key);

    if (value === null) {
        console.log(`Item not found - Key "${key}" not found in storage.`);
    } else {
        console.log(`Item retrieved successfully - Key: "${key}"\nValue: "${value}"`);
    }
}

getItems(keys)

基于其密钥从存储中检索多个项目。 返回承诺。 如果 Promise 未解析,则返回 null。

getItems(keys: string[]): Promise<{ [key: string]: string | null }>;

参数

keys

string[]

要删除的项目的密钥。 必须是字符串数组。

返回

Promise<{ [key: string]: string | null }>

注解

API 集:SharedRuntime 1.1,邮箱 1.10

Excel、PowerPoint 和 Word 加载项的 SharedRuntime 1.1 要求集中提供此方法。也从 Outlook 的邮箱要求集 1.10 开始提供。

重要提示: 在 Outlook 中,仅对 Windows 上的 Outlook 中实现的 基于事件的激活 功能提供支持。 Mac 版 Outlook 或 Outlook 网页版不支持此方法。

示例

// Retrieve a set of default values from the shared runtime storage.
async function getDefaultSettings() {
  const keysToRetrieve = [
      'username',
      'email',
      'theme',
      'lastLogin',
      'preferences'
  ];

  const items = await OfficeRuntime.storage.getItems(keysToRetrieve);
  for (const [key, value] of Object.entries(items)) {
      console.log(`"${key}": "${value}"`);
  }
}

getKeys()

从存储中检索所有密钥的数组。 返回承诺。

getKeys(): Promise<string[]>;

返回

Promise<string[]>

注解

API 集:SharedRuntime 1.1,邮箱 1.10

Excel、PowerPoint 和 Word 加载项的 SharedRuntime 1.1 要求集中提供此方法。也从 Outlook 的邮箱要求集 1.10 开始提供。

重要提示: 在 Outlook 中,仅对 Windows 上的 Outlook 中实现的 基于事件的激活 功能提供支持。 Mac 版 Outlook 或 Outlook 网页版不支持此方法。

示例

// Retrieve all keys currently stored in the shared runtime storage.
async function getAllKeys() {
  const keys = await OfficeRuntime.storage.getKeys();

  if (keys.length === 0) {
    console.log('No items in storage.');
    showStatus('Storage is empty', 'info');
  } else {
    console.log(`Found ${keys.length} key(s):\n\n${keys.toString()}`);
  }
}

removeItem(key)

基于项的键从存储中删除项。 返回承诺。

removeItem(key: string): Promise<void>;

参数

key

string

要删除的项目的密钥。 必须为字符串。

返回

Promise<void>

注解

API 集:SharedRuntime 1.1,邮箱 1.10

Excel、PowerPoint 和 Word 加载项的 SharedRuntime 1.1 要求集中提供此方法。也从 Outlook 的邮箱要求集 1.10 开始提供。

重要提示: 在 Outlook 中,仅对 Windows 上的 Outlook 中实现的 基于事件的激活 功能提供支持。 Mac 版 Outlook 或 Outlook 网页版不支持此方法。

示例

// Delete an item from the shared runtime storage by its key.
async function removeItem() {
    const key = document.getElementById('deleteKey').value.trim();
        
    // Check if key exists first.
    const value = await OfficeRuntime.storage.getItem(key);

    if (value === null) {
        console.log(`Key "${key}" not found`);
    } else {
      await OfficeRuntime.storage.removeItem(key);
      console.log(`Item "${key}" removed successfully`);
    }
}

removeItems(keys)

从存储中删除多个项目。 返回承诺。

removeItems(keys: string[]): Promise<void>;

参数

keys

string[]

要删除的项目的密钥。 必须是字符串数组。

返回

Promise<void>

注解

API 集:SharedRuntime 1.1,邮箱 1.10

Excel、PowerPoint 和 Word 加载项的 SharedRuntime 1.1 要求集中提供此方法。也从 Outlook 的邮箱要求集 1.10 开始提供。

重要提示: 在 Outlook 中,仅对 Windows 上的 Outlook 中实现的 基于事件的激活 功能提供支持。 Mac 版 Outlook 或 Outlook 网页版不支持此方法。

示例

// Delete all the items from the shared runtime storage.
async function clearAllStorage() {
  const keys = await OfficeRuntime.storage.getKeys();
  await OfficeRuntime.storage.removeItems(keys);
  console.log(`All ${keys.length} items cleared from storage`);
}

setItem(key, value)

将键值对设置为存储中或更新现有的键值对。 返回承诺。

setItem(key: string, value: string): Promise<void>;

参数

key

string

要设置的项目的键。 必须为字符串。

value

string

必须为字符串。

返回

Promise<void>

注解

API 集:SharedRuntime 1.1,邮箱 1.10

Excel、PowerPoint 和 Word 加载项的 SharedRuntime 1.1 要求集中提供此方法。也从 Outlook 的邮箱要求集 1.10 开始提供。

重要提示: 在 Outlook 中,仅对 Windows 上的 Outlook 中实现的 基于事件的激活 功能提供支持。 Mac 版 Outlook 或 Outlook 网页版不支持此方法。

示例

// Save a key-value pair in the shared runtime storage.
async function storeValue() {
  const key = document.getElementById('createKey').value.trim();
  const value = document.getElementById('createValue').value.trim();

  await OfficeRuntime.storage.setItem(key, value);
}

setItems(keyValues)

将多个项目设置为存储中或更新存储中的多个项目。 返回承诺。

setItems(keyValues: { [key: string]: string }): Promise<void>;

参数

keyValues

{ [key: string]: string }

要设置的键值对。 必须为字符串。

返回

Promise<void>

注解

API 集:SharedRuntime 1.1,邮箱 1.10

Excel、PowerPoint 和 Word 加载项的 SharedRuntime 1.1 要求集中提供此方法。也从 Outlook 的邮箱要求集 1.10 开始提供。

重要提示: 在 Outlook 中,仅对 Windows 上的 Outlook 中实现的 基于事件的激活 功能提供支持。 Mac 版 Outlook 或 Outlook 网页版不支持此方法。

示例

// Save multiple key-value pairs in the shared runtime storage.
async function setDefaultSettings() {
  const demoData = {
      'username': 'JohnDoe',
      'email': 'john.doe@example.com',
      'theme': 'dark',
      'lastLogin': new Date().toISOString(),
      'preferences': JSON.stringify({ notifications: true, language: 'en' })
  };

  await OfficeRuntime.storage.setItems(demoData);
}