gpt4 book ai didi

excel - Office.js : How to get a property from an item derived from a collection (e. g., NamedItemCollection 中所有 Ranges 的 'address' 属性)在一个批处理中

转载 作者:行者123 更新时间:2023-12-04 21:56:48 24 4
gpt4 key购买 nike

我有一个包含 2000 个命名范围的工作簿。我想获取范围属性,例如地址、columnCount、rowCount 以及可能的单元格值对象。我可以通过 2000 个服务器调用获得这些。这在 Excel 中可以,但在 Excel Online(2000 次服务器往返)中不行。
我想通过几个批处理操作来获得像“地址”这样的属性?
我已经尝试过如下各种组合(不起作用),但无法弄清楚如何。

    Excel.run(function (ctx) {
var nameditems = ctx.workbook.names;
nameditems.load('items');
return ctx.sync().then(function () {
var nameditemsProperties = nameditems.load('address, rowCount');
return ctx.sync().then(function () {
for (var i = 0; i < nameditemsProperties.items.length; i++) {
app.showNotification('address: ' + nameditemsProperties.getItem[i].address);
}
});
})
}).catch(function (error) {
handleErrors(error);
});

我可以(我如何)通过几个批处理操作在数组中获取像“地址”这样的属性吗?
我正在使用 Excel API 1.3。

最佳答案

你很接近......但你需要对 Range 对象进行加载。

您仍然可以通过 2 次同步来完成。第一个获取所有命名项目,第二个加载每个命名项目的地址。

Excel.run(function (ctx) {
var nameditems = ctx.workbook.names.load('name');
var namesToRanges = {};
return ctx.sync()
.then(function () {
nameditems.items.forEach(function (item) {
namesToRanges[item.name] = item.getRange().load("address");
})
})
.then(ctx.sync)
.then(function () {
nameditems.items.forEach(function (item) {
console.log(item.name, namesToRanges[item.name].address);
})
})
})
.catch(function (error) {
OfficeHelpers.Utilities.log(error);
});

对于它的值(value),如果您使用 TypeScript,您将获得更好的 IntelliSense 和更易于阅读的代码:
async function run() {
try {
await Excel.run(async (ctx) => {
const nameditems = ctx.workbook.names.load('name');
const namesToRanges: { [name: string]: Excel.Range } = {};
await ctx.sync()
nameditems.items.forEach(function (item) {
namesToRanges[item.name] = item.getRange().load("address");
})
await ctx.sync();
nameditems.items.forEach(function (item) {
console.log(item.name, namesToRanges[item.name].address);
})
});
} catch (error) {
OfficeHelpers.Utilities.log(error);
}
}

您可以在电子书“ Building Office Add-ins using Office.js”中找到有关使用 TypeScript 和 Office.js 的一些信息。全面披露:我是这本书的作者,但我可以保证你会在那里找到很多有用的 Material ,包括使用 TypeScript 和一般的 Office.js 模式。

更新:更新代码以允许非范围命名项目:
$("#run").click(run);

async function run() {
try {
await Excel.run(async (ctx) => {
const nameditems = ctx.workbook.names.load('name, type, value');
const namesToValues: { [name: string]: Excel.Range | any } = {};
await ctx.sync()

nameditems.items.forEach(item => {
if (item.type === Excel.NamedItemType.range) {
namesToValues[item.name] = item.getRange().load("address");
} else {
namesToValues[item.name] = item.value;
}
});

await ctx.sync();

nameditems.items.forEach(item => {
const value = namesToValues[item.name];
console.log(item.name,
(value instanceof Excel.Range) ? value.address : value);
});
});
} catch (error) {
OfficeHelpers.Utilities.log(error);
}
}

关于excel - Office.js : How to get a property from an item derived from a collection (e. g., NamedItemCollection 中所有 Ranges 的 'address' 属性)在一个批处理中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42958363/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com