admin管理员组文章数量:1023744
I'm developing an Excel add-in using office.js library, and need to create some named ranges to track and access later.
However according to the API documentation:
NamedItemCollection: .md
NamedItem: .md
There is no method for creating named items.
Is there a way to name and reference an Excel range using office.js? Thanks.
I'm developing an Excel add-in using office.js library, and need to create some named ranges to track and access later.
However according to the API documentation:
NamedItemCollection: https://github./OfficeDev/office-js-docs/blob/master/reference/excel/nameditemcollection.md
NamedItem: https://github./OfficeDev/office-js-docs/blob/master/reference/excel/nameditem.md
There is no method for creating named items.
Is there a way to name and reference an Excel range using office.js? Thanks.
Share Improve this question edited Jul 20, 2016 at 19:04 Michael Saunders 2,6681 gold badge14 silver badges21 bronze badges asked Jul 19, 2016 at 22:16 Liang GuoLiang Guo 331 silver badge5 bronze badges3 Answers
Reset to default 4There isn't a way to name a range in Excel through Office.js
. Only the user can name a range, through the Excel UI.
The good news is that there's a different way to acplish the same goal:
If you want to maintain a reference to a range, there's no need to name it; it already has a name like Sheet1!A1:B10
.
If you want to maintain a reference to a range even if the user adds/deletes rows/columns before it or inside it, use a Binding:
var myBindings = Office.context.document.bindings;
var myAddress = "Sheet1!A1:B10";
myBindings.addFromNamedItemAsync(myAddress, "matrix", {id:"myBind"}, function(result){});
And then when you want to access the range later, you can:
Excel.run(function (ctx) {
var foundBinding = ctx.workbook.bindings.getItem("myBind");
var myRange = foundBinding.getRange();
myRange.load('values');
return ctx.sync().then(function() {
console.log(myRange.values);
});
});
-Michael Saunders, PM for Office add-ins
there is the Excel.NamedItemCollection which provides an add() method that generates Named references
https://learn.microsoft./en-us/javascript/api/excel/excel.nameditemcollection?view=excel-js-preview
async function addNameToHeader() {
await Excel.run(async(context) => {
const sheet = context.workbook.worksheets.getItem("Sample");
const headerRange = sheet.getRange("A1:D1");
sheet.names.add("ExpensesHeader", headerRange);
const namedItems = sheet.names.load("name, type");
await context.sync();
});
}
async function formatRangeByName() {
await Excel.run(async(context) => {
const sheet = context.workbook.worksheets.getItem("Sample");
const headerRowName = sheet.names.getItemOrNullObject("ExpensesHeader");
headerRowName.load();
await context.sync();
if (headerRowName.value) {
const headerRange = headerRowName.getRange();
headerRange.format.fill.color = "red";
} else {
console.log("No named item created for the range.");
}
await context.sync();
});
}
I'm developing an Excel add-in using office.js library, and need to create some named ranges to track and access later.
However according to the API documentation:
NamedItemCollection: .md
NamedItem: .md
There is no method for creating named items.
Is there a way to name and reference an Excel range using office.js? Thanks.
I'm developing an Excel add-in using office.js library, and need to create some named ranges to track and access later.
However according to the API documentation:
NamedItemCollection: https://github./OfficeDev/office-js-docs/blob/master/reference/excel/nameditemcollection.md
NamedItem: https://github./OfficeDev/office-js-docs/blob/master/reference/excel/nameditem.md
There is no method for creating named items.
Is there a way to name and reference an Excel range using office.js? Thanks.
Share Improve this question edited Jul 20, 2016 at 19:04 Michael Saunders 2,6681 gold badge14 silver badges21 bronze badges asked Jul 19, 2016 at 22:16 Liang GuoLiang Guo 331 silver badge5 bronze badges3 Answers
Reset to default 4There isn't a way to name a range in Excel through Office.js
. Only the user can name a range, through the Excel UI.
The good news is that there's a different way to acplish the same goal:
If you want to maintain a reference to a range, there's no need to name it; it already has a name like Sheet1!A1:B10
.
If you want to maintain a reference to a range even if the user adds/deletes rows/columns before it or inside it, use a Binding:
var myBindings = Office.context.document.bindings;
var myAddress = "Sheet1!A1:B10";
myBindings.addFromNamedItemAsync(myAddress, "matrix", {id:"myBind"}, function(result){});
And then when you want to access the range later, you can:
Excel.run(function (ctx) {
var foundBinding = ctx.workbook.bindings.getItem("myBind");
var myRange = foundBinding.getRange();
myRange.load('values');
return ctx.sync().then(function() {
console.log(myRange.values);
});
});
-Michael Saunders, PM for Office add-ins
there is the Excel.NamedItemCollection which provides an add() method that generates Named references
https://learn.microsoft./en-us/javascript/api/excel/excel.nameditemcollection?view=excel-js-preview
async function addNameToHeader() {
await Excel.run(async(context) => {
const sheet = context.workbook.worksheets.getItem("Sample");
const headerRange = sheet.getRange("A1:D1");
sheet.names.add("ExpensesHeader", headerRange);
const namedItems = sheet.names.load("name, type");
await context.sync();
});
}
async function formatRangeByName() {
await Excel.run(async(context) => {
const sheet = context.workbook.worksheets.getItem("Sample");
const headerRowName = sheet.names.getItemOrNullObject("ExpensesHeader");
headerRowName.load();
await context.sync();
if (headerRowName.value) {
const headerRange = headerRowName.getRange();
headerRange.format.fill.color = "red";
} else {
console.log("No named item created for the range.");
}
await context.sync();
});
}
本文标签: javascriptHow to name and reference an Excel range using officejsStack Overflow
版权声明:本文标题:javascript - How to name and reference an Excel range using office.js - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745598129a2158294.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论