Is it possible to add a comment to a cell using a User Defined Function?
Thanks in advance,
Yes, it should work the same as setting a cell. You can even use user defined functions for images (as you can see in the sample B0.Features Page )
It works the same as a normal field: Just add a comment in the template and write <#userdefinedfunction()> inside
Apologies Adrian, I should've been a little more clear:
Inside a named range, is it possible to add a comment to a cell (which doesn't have a comment in the template) from a UDF?
For example, in a __Responses__ range, have a cell (without an existing comment) with the formula:
ah, ok, sorry for the misunderstanding.
About the question: Technically you can, but I am not completely sure you should. In any case, I'll explain and leave the decision to you :)
The thing with UDFs is that they are supposed to be static: You pass a bunch of values and it returns a calculated value. They aren't supposed to have side effects like adding a comment, and that's why in the Evaluate method we don't offer you a handle to the XlsFile.
But, you can do it. To do it, you would have to:
1)Have a variable holding the XlsFile object so you can add the comment in the UDF.
2)Pass the row and the column where you want to insert it to the udf. You can use =Row() and =Column() Excel functions for this.
3)In the evaluate method, use the XlsFile stored in 1) to add the comment.
Mostly that is it. I made a simple example that you can get at:
Which shows how it works.
ok, I sent "Post reply" too soon. I also wanted to mention that the reason I think you might not need any of this is because you can do it directly with FlexCel and no UDF.
You want to add a comment in a cell that doesn't have a comment in the template. But you can think it on reverse: You can add a comment to the template (instead of writing <#CommentUDF()> in the cell). Then send null to the cells where you don't want a comment, and the text to the cells where you want it. FlexCel will automatically delete all null comments, so the end result should be the same.
In your approach, you add comments only to some cells.
In this other approach, you add a comment to "all" the cells, but remove those you don't want by assigning null to them.
Perfect, that's exactly what I needed!
Thank you for explaining in detail, I can clearly see the pros / cons of each approach.