Why does window System dynamic comment is not work in excel? - ms-office

I am working with office js. my add-in inserts a comment in the cell that is working properly with mac system office and office 365 but I can't insert comments in the windows system office. the version of office 2016. what did I do wrong? Please guide me thanks.
Here is my code snippet
await Excel.run(async (ctx) => { let wb = ctx.workbook;
await ctx.sync().then(async () => {
try {
const address="sheet!A4"
var comment = wb.comments.getItemByCell(address);
comment.delete();
wb.comments.add(address, "This is simple test comment");
} catch (error) {
if (error.code == Excel.ErrorCodes.itemNotFound) {
wb.comments.add(address, "This is simple test comment");
console.log("Add comment successfully!");
}
}
});
await ctx.sync();
});
enter image description here

#KaranChokshi, sorry I just noticed your office version is 2016, which hasn't supported comments API. The comments API are available since API set: ExcelApi 1.10, refer to learn.microsoft.com/en-us/office/dev/add-ins/reference/… for more details. And maybe you can help to double confirm, the UI side also didn't has comments related features.
And another point is that you may need to move the await ctx.sync() to the try{}, then you can catch the detail error as expected (your snapshot has no error shows):
try {
const address="sheet!A4"
var comment = wb.comments.getItemByCell(address);
comment.delete();
wb.comments.add(address, "This is simple test comment");
await ctx.sync();
} catch (error) {
if (error.code == Excel.ErrorCodes.itemNotFound) {
wb.comments.add(address, "This is simple test comment");
console.log("Add comment successfully!");
}
Please let me know if have any other issues.

Related

Office excel add ins onNameChanged event on "context.workbook.worksheets" not working

Using below code to identify worksheet rename event and perform activity after rename. Same is working fine in online version of excel(online office365) on Chrome/edge browser but its not working on desktop version of excel(Microsoft 365 MSO (16.0.14326.21170) - 32-bit).
export const onSheetNameChange = event => {
Excel.run(context => {
return context.sync().then(() => {
const { nameAfter, nameBefore } = event;
console.log('nameBefore=>', nameBefore);
console.log('nameAfter=>', nameAfter);
});
});
};
export const onSheetRenameHandler = () => {
Excel.run(context => {
const sheets = context.workbook.worksheets;
sheets.onNameChanged.add(onSheetNameChange);
return context
.sync()
.then(() => {
console.log(
'A handler has been registered for the OnNameChanged event.',
);
})
.catch(error => {
console.log('error=>', error);
});
});
};
Its giving below ERROR:
error=> RichApi.Error: You cannot perform the requested operation.
at new n (excel-win32-16.01.js:25:241192)
at i.processRequestExecutorResponseMessage (excel-win32-16.01.js:25:305358)
at excel-win32-16.01.js:25:303421
If I change event handler code like below:
const sheets= context.workbook.worksheets.getActiveWorksheet();
sheets.onNameChanged.add(onSheetNameChange);
Then its working on desktop excel but only for active sheet. If I add new sheet and tries to rename the sheet onSheetNameChange event handler is not getting called.
I am using promise based approach. Even tried same code as per mentioned in THIS LINK. and getting same error.
Can someone please help to understand if there is any issue in above mentioned "onSheetRenameHandler " method? why its not working if I tries to load the sheet like "const sheets = context.workbook.worksheets" and then add the handler on it.
Fixed by using below code. As it requires to add event handler on each sheet.
const onSheetRenameAddHandler = async callBack => {
await Excel.run(async context => {
const sheets = context.workbook.worksheets;
sheets.load('items/name');
sheets.load('name');
await context.sync();
for (let i = 0; i < sheets.items.length; i += 1) {
sheets.items[i].onNameChanged.add(callBack);
}
await context.sync();
console.log(
'A handler has been registered for the OnNameChanged event.',
);
});
}
One thing observed if we add new sheet in workbook then that sheet wont be added this onNameChanged handler method, working on that part as well as removing of this event else it will push the events on running stack which causes the duplicate execution of event handler.

io.connect() is not working and i can't figure what is the problem

[and this is the angular code ][1] this is my node js code
Please copy/paste your actual code. Screenshots are discouraged on SO.
Be sure to include ERROR HANDLING in all of your I/O operations. For example:
https://socket.io/docs/v4/client-initialization/
socket.on("connect_error", (err) => {
if (err.message === "invalid credentials") {
socket.auth.token = "efgh";
socket.connect();
}
});
... or ...
https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/onerror
webSocket.onerror = function(event) {
console.error("WebSocket error observed:", event);
};
Your next step is to get a viable error message.
Please review the library's documentation to determine the "best" way to catch and handle errors for your particular application.

Excel: How to determine if a cell is in edit mode prior to running context.sync()

an add in I'm developing is having issues with context.sync failing when a cell is in editing mode.
I would like to use delayForCellEdit in Excel.Run but it would cause the UI to hang until the user exits edit mode.
Is there a method to determine if Excel is in edit mode prior to calling context.sync?
My first inclination is to run
Excel.run(ctx => {
ctx.sync().then(() => {
console.log("not in edit mode);
}).catch(err => {
console.log("in edit mode");
});
});
as a heartbeat every few seconds, but would like something a bit more elegant.
Currently we only support delayForCellEdit to delay the context.sync() until exit edit mode. And I tested locally didn't meet the hang issue as you mentioned.
Here is my sample code for test and just for your reference:
$("#run").click(() => tryCatch(run));
async function run() {
await Excel.run({ delayForCellEdit: true }, async context => {
let sheet;
sheet = context.workbook.worksheets.getItem("Sheet1");
let range = sheet.getRange("A1");
range.values = [["1"]];
await context.sync();
});
}

Google translate API not working in firebase functions

I am struggling to figure out whether google-translate-api stopped working in firebase functions. I have the following code which used to work but now all I get is locale Not Supported.
exports.translateNow = functions.database.ref('/Translate/lang'.onWrite(event => {
let loc='en';//event.data.val();
console.log("LOCA "+loc);
translateMessage('Ik spreek Engels',loc).then(function(result){
console.log("PYAM ",result);
}).catch(function(err){
console.log("ERR ",err);
});
return true;});
The locale is read from firebase database but I've hard coded en for English which is still not working.
Here is translateMessage
let translateMessage=function(text,loc){
let theLoc=loc;
if(theLoc=='ab'||theLoc=='aa'||theLoc=='ak'||theLoc=='an'||theLoc=='as'||theLoc=='av'||theLoc=='ae'||theLoc=='ay'||theLoc=='bm'||theLoc=='ba'||theLoc=='bh'||theLoc=='ce'||theLoc=='zh'||theLoc=='ki'){
theLoc='en'
}
return new Promise(function(resolve,reject){
translate(text, {from: 'en', to: theLoc}).then(res => {
resolve(res.text);
}).catch(function(result){
reject("Not Supported");
});
});};
Please someone help here's my include const translate = require('google-translate-api');

Office JS - deleting a content control issue

I'm trying to delete a content control in Word and I'm going through these docs: https://dev.office.com/reference/add-ins/word/contentcontrol
This is my code:
let mycc = context.document.contentControls.getByTag('mycc');
if (mycc) {
context.load(mycc, 'text');
context.sync().then(() => {
mycc.items[0].delete(true);
context.sync().then(() => { ... })
})
}
No error is thrown and the code inside the inner most sync works (inserting another content control).
What am I doing wrong?
EDIT: Found the issue. The content control was indeed being deleted, but I wanted to delete the content, as well. Instead of passing true to delete, I should have passed false.
I'm unable to reproduce the issue that you've described. Given that the code that you've posted does not include any error-handling logic, I'd suspect that perhaps an error is silently being thrown, but you're not detecting it.
The following code snippet successfully deletes the first content control that has the tag mycc, and includes a catch statement that handles any error which might occur inside of the Word.run.
Word.run(function (context) {
var myContentControls = context.document.contentControls.getByTag("mycc");
if (myContentControls) {
context.load(myContentControls, "text");
return context.sync()
.then(function () {
myContentControls.items[0].delete(true);
return context.sync()
.then(function () {
console.log("Done!");
});
});
}
}).catch(OfficeHelpers.Utilities.log);

Resources