Office JS API - settings.getItem 不是函数
Office JS API - settings.getItem is not a function
我正在尝试检索我之前在同一会话中保存的文档中的设置,但是我得到 settings.getItem is not a function
。
我正在尝试通过 Chrome 中的 Word Online 会话来执行此操作,并且一直在参考文档 here, and have also seen this (但我认为它不适合我的具体问题)
如上所述,保存设置工作正常,当我下载文件时我可以看到设置已保存。
我为此使用了 react + redux,所以下面的代码片段来自 redux action creator:
export function getSetting() {
//this function tries to find a setting within the document
return function(dispatch, getState) {
window.Word.run(
function(context) {
let settings = Office.context.document.settings
var thisSetting
try {
console.log('attempting to find existing setting for ' + constants.ORG_ID)
thisSetting = settings.getItem(constants.ORG_ID)
console.log('got ' + thisSetting.value)
context.load(thisSetting)
} catch (error) {
thisSetting = null
console.log('couldn\'t find setting)
console.log(error.message)
}
return context.sync()
.then(
function() {
if (thisSetting) {
dispatch(requestSetThisSetting (thisSetting.value))
console.log('got setting ' + thisSetting.value)
}
}
)
}
)
}
}
关于如何成功检索设置的任何想法?
根据答案更新了代码(下方)
我已经根据 Rick 和 Juan 提供的答案(谢谢你们!)用工作代码更新了这个 post
export function getSetting() {
//this function tries to find a setting within the document
return function(dispatch, getState) {
let currentSetting = Office.context.document.settings.get(constants.ORG_ID)
console.log('current setting is ' + currentSetting)
if (currentSetting) dispatch(requestSetSetting(currentSetting))
}
}
您似乎是在将 Shared API Office.context.document.settings 混合到特定于 Word 的 API 中的 Word.run 中。看看特殊的 Word 包装器 API(来自 Word 特定 APIs 的 1.4 版):SettingsCollection。这是context.document.settings对象,但是这里的"context"是传给Word.run的Word.RequestContext对象,不是Office.context对象。您的代码使用 Shared API Office.context.document.settings 获取设置对象,但随后它从 Word.RequestContext.document.settings.
在幕后,文件中的 OOXML 设置相同,但通过两个不同的 APIs 访问它可能会出现问题。
更新:根据 Juan Balmori 的评论,这里有一些关于使用共享 API 读取设置的信息:Getting the value of a setting. And see here for info on the distinction between the Common and host-specific APIs: JavaScript API for Office.
我正在尝试检索我之前在同一会话中保存的文档中的设置,但是我得到 settings.getItem is not a function
。
我正在尝试通过 Chrome 中的 Word Online 会话来执行此操作,并且一直在参考文档 here, and have also seen this
如上所述,保存设置工作正常,当我下载文件时我可以看到设置已保存。
我为此使用了 react + redux,所以下面的代码片段来自 redux action creator:
export function getSetting() {
//this function tries to find a setting within the document
return function(dispatch, getState) {
window.Word.run(
function(context) {
let settings = Office.context.document.settings
var thisSetting
try {
console.log('attempting to find existing setting for ' + constants.ORG_ID)
thisSetting = settings.getItem(constants.ORG_ID)
console.log('got ' + thisSetting.value)
context.load(thisSetting)
} catch (error) {
thisSetting = null
console.log('couldn\'t find setting)
console.log(error.message)
}
return context.sync()
.then(
function() {
if (thisSetting) {
dispatch(requestSetThisSetting (thisSetting.value))
console.log('got setting ' + thisSetting.value)
}
}
)
}
)
}
}
关于如何成功检索设置的任何想法?
根据答案更新了代码(下方)
我已经根据 Rick 和 Juan 提供的答案(谢谢你们!)用工作代码更新了这个 post
export function getSetting() {
//this function tries to find a setting within the document
return function(dispatch, getState) {
let currentSetting = Office.context.document.settings.get(constants.ORG_ID)
console.log('current setting is ' + currentSetting)
if (currentSetting) dispatch(requestSetSetting(currentSetting))
}
}
您似乎是在将 Shared API Office.context.document.settings 混合到特定于 Word 的 API 中的 Word.run 中。看看特殊的 Word 包装器 API(来自 Word 特定 APIs 的 1.4 版):SettingsCollection。这是context.document.settings对象,但是这里的"context"是传给Word.run的Word.RequestContext对象,不是Office.context对象。您的代码使用 Shared API Office.context.document.settings 获取设置对象,但随后它从 Word.RequestContext.document.settings.
在幕后,文件中的 OOXML 设置相同,但通过两个不同的 APIs 访问它可能会出现问题。
更新:根据 Juan Balmori 的评论,这里有一些关于使用共享 API 读取设置的信息:Getting the value of a setting. And see here for info on the distinction between the Common and host-specific APIs: JavaScript API for Office.