[英]Range across different Word.run contexts using OfficeExtension.TrackedObjects class
I am trying to use the OfficeExtension.TrackedObjects class to access a range across different contexts (documentation and similar questions set out below - although slightly outdated).我正在尝试使用 OfficeExtension.TrackedObjects class 来访问不同上下文的范围(下面列出的文档和类似问题 - 尽管有点过时)。 The goal is to have a taskpane search list the results in the taskpane, then select the specific result in-text when clicking the listed result (using javascript).
目标是让任务窗格搜索列出任务窗格中的结果,然后在单击列出的结果(使用 javascript)时在文本中显示 select 特定结果。
Here is what I have:这是我所拥有的:
var items = [];
function basicSearch() {
Word.run(function (context) {
const results = context.document.body.search("Online");
results.load("length, text, items");
return context.sync().then(function () {
context.trackedObjects.add(results);
for (let i = 0; i < results.items.length; i++) {
let x = results.items[i].text;
createtable("TestList", i, x, x);
items.push(results.items[i]);
}
});
return context.sync();
});
}
function createtable(id, x, y, z) {
var table = document.getElementById(id);
var row = table.insertRow(-1);
var cell1 = row.insertCell(0);
var cell2 = row.insertCell(1);
var cell3 = row.insertCell(2);
cell1.type = "button";
cell1.onclick = function () { selectrange(x) };
cell2.type = "button";
cell2.onclick = function () { selectrange(x) };
cell3.type = "button";
cell3.onclick = function () { selectrange(x) };
cell1.innerHTML = x;
cell2.innerHTML = y;
cell3.innerHTML = z;
}
function selectrange(x) {
results.load("text");
results.items[x].select();
results.context.sync();
}
Could someone show me where I have gone wrong, or provide a full working example of how to track and call an object (or collection of objects) for use?有人可以告诉我我哪里出错了,或者提供一个完整的工作示例来说明如何跟踪和调用 object(或对象集合)以供使用吗?
Resources:资源:
https://learn.microsoft.com/en-us/javascript/api/office/officeextension.trackedobjects?view=common-js-preview&viewFallbackFrom=office-js How can a range be used across different Word.run contexts? https://learn.microsoft.com/en-us/javascript/api/office/officeextension.trackedobjects?view=common-js-preview&viewFallbackFrom=office-js 如何在不同的 Word.run 上下文中使用范围? Word Online Add-In: Using objects across multiple contexts Tracked Objects throwing errors in Word Online https://leanpub.com/buildingofficeaddins (Building Office Add-ins using Office.js has a working example, but it is in typescript and does not use trackedObjects - I have not been able to replicate it in my add-in).
Word Online 加载项:在多个上下文中使用对象Tracked Objects 在 Word Online 中抛出错误https://leanpub.com/buildingofficeaddins (使用 Office.js 构建 Office 加载项有一个工作示例,但它在 typescript 中并且没有使用 trackedObjects - 我无法在我的加载项中复制它)。
When I run the above code, it says "ReferenceError: Can't find variable: results".当我运行上面的代码时,它说“ReferenceError:找不到变量:结果”。 I want it to select the specific search results displayed and pressed in the list.
我想让它把select具体的搜索结果显示出来并压在列表中。 Any assistance would be greatly appreciated.
任何帮助将不胜感激。
first, you need to define results as a global variable so that you can pass it to other functions.首先,您需要将结果定义为全局变量,以便您可以将其传递给其他函数。 please reference the following code:
请参考以下代码:
var results;
async function basicSearch() {
await Word.run(async (context) => {
results = context.document.body.search("Online");
results.track();
await context.sync();
});
}
async function selectrange() {
await Word.run(results, async (context) => {
results.load();
await context.sync();
results.items[0].select();
await context.sync();
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.