I am working with office js but unable to add multiple selected cells comments. I have already read office js document but can't find a similar solution which I want. if I have selected a single cell below code work properly.
await Excel.run(async (context) => {
const range = context.workbook.getSelectedRange();
const comment = context.workbook.comments.getItemByCell(range);
comment.content = "add comment";
await context.sync();
});
I am a beginner for office js and following code get address Sheet1!I4:K4.so does I have manually loop through adding a comment like I4, J4, K4 but it not better way. if I am wrong please correct me thank you.
try {
await Excel.run(async (context) => {
try {
var range = context.workbook.getSelectedRange();
range.load(["address"]);
return context.sync().then(function () {
const addString = range.address;
//Sheet1!I4:K4
});
} catch (error) {
// eslint-disable-next-line no-empty
if (error.code == Excel.ErrorCodes.itemNotFound) {
} else {
throw error;
}
}
});
} catch (error) {
ErrorProvider.getInstance().notify(JSON.stringify(error));
}
I think this does what you want. See the code below:
async function run() {
await Excel.run(async (context) => {
let wb = context.workbook
let ws = wb.worksheets.getActiveWorksheet()
let rang = wb.getSelectedRange()
rang.load("rowCount, columnCount")
await context.sync()
let rangRowCount = rang.rowCount
let rangColCount = rang.columnCount
let rangeAddresses = []
for (let i = 0; i < rangRowCount; i++) {
{
for (let j = 0; j < rangColCount; j++)
{
let newRang = rang.getCell(i,j)
rangeAddresses.push(newRang.load("address"))
}
}
}
await context.sync()
rangeAddresses.forEach(e => {
wb.comments.add(e.address, "This is a comment")
})
})
}