Spire.XLS for .NET is a professional Excel .NET library that can be used to any type of .NET( C#, VB.NET, ASP.NET, .NET Core) application, both ASP.NET web sites and Windows Forms application.

Mon May 20, 2013 11:31 am

I'm trying to add a comment and override the author name. However, the Author property on the comment is read-only.

In the resulting Excel file, the author is not blank but it has the current Windows principal identity name in it which is completely wrong on a server running ASP.NET without impersonation. I need to be able to specify the Author, or keep it empty. Disclosing the user name under which the ASP.NET application runs is a no-go!

In the forums I read that this should be possible but it doesn't say how?
http://e-iceblue.com/forum/post3329.html#p3329

avonwyss
 
Posts: 4
Joined: Thu Oct 28, 2010 9:37 pm

Tue May 21, 2013 3:42 am

Dear avonwyss,

Thanks for your inquiry.
Please try the code snippet below to remove author name from one comment.

Code: Select all
Worksheet sheet = workbook.Worksheets[0];
            CellRange range = sheet.Range["A1"];
            string author = range.Comment.Author;
            string text = range.Comment.Text;
            text = text.Remove(0, author.Count() + 2);
            range.Comment.Text = text;

There is any issue, please feel free to contact us.

Regards,
Amy
E-iceblue support team
User avatar

amy.zhao
 
Posts: 2328
Joined: Wed Jun 27, 2012 8:50 am

Fri May 24, 2013 10:18 am

Dear avonwyss,

Do you try the code? Does it solve your issue?
Please give us a feedback at your early convenience.

Thanks and Regards,
Amy
E-iceblue support team
User avatar

amy.zhao
 
Posts: 2328
Joined: Wed Jun 27, 2012 8:50 am

Fri May 31, 2013 11:54 am

Dear Amy, sorry for the late reply.

No, it doesn't seem to work in my case. When I set a breakpoint, I see that the Text property does not include the author name before saving the sheet, however the Author property is non-empty. My expectation would be to be able to assign the Author property?

avonwyss
 
Posts: 4
Joined: Thu Oct 28, 2010 9:37 pm

Mon Jun 03, 2013 10:02 am

Dear avonwyss,

Sorry, we don't provide a method to change the Author property. In fact, the comment has no such a property in Excel. When you insert a comment in MS-Excel, the author is appened automatically by Excel program. But the author is just a text special with bold font style. You can edit it, remove it, even insert another string (for example: "Bill:\r\n") before it and then the it looks like the comment has two authors. Any string ends with the string ":\r\n" and has bold font will be parsed as the author. So the Author property isn't exact sometimes. It's just a hint. If your comment just has normal font style (without blod, italic, underline, color, font-size, font-name), you could use the code:
Code: Select all
cell.Comment.Text = new System.Text.RegularExpressions.Regex(@"^[^:]+:\n").Replace(cell.Comment.Text, "")

to reset the author to blank. But it's not reliable always.

Regards,
Amy
E-iceblue support team
User avatar

amy.zhao
 
Posts: 2328
Joined: Wed Jun 27, 2012 8:50 am

Mon Jun 03, 2013 10:49 am

Dear Amy,

I understand that the author is just a specially formatted part of the comment rich text. Nevertheless, since the Spire.XLS component automatically inserts the author name in come cases, this piece of information inserted should be controllable via API for generating files on the server.

Note that the online help for this property also states "Gets or sets the author of the comment." - all that would be needed is a way to have the component insert whatever name I choose instead of the current Windows principal identity name.

Kind regards, Arsène

avonwyss
 
Posts: 4
Joined: Thu Oct 28, 2010 9:37 pm

Tue Jun 04, 2013 6:50 am

Dear Arsène,

Thanks for your reply.
If you want to add a new comment which can identity author' name to your Excel file, please try the code below.

Code: Select all
static void Main(string[] args)
        {
            Workbook workbook = new Workbook();
            Worksheet sheet = workbook.Worksheets[0];
            CellRange range = sheet.Range["A1"];
            CreatComment(range, "Test comment1");
            range = sheet.Range["F2"];
            CreatComment(range, "Test comment2","Amy");
            workbook.SaveToFile("sample.xlsx",ExcelVersion.Version2007);
        }

        static ExcelComment CreatComment(CellRange range,string text, string author=null)
        {
           ExcelComment comment= range.AddComment();
           comment.Text = String.IsNullOrEmpty(author) ? text : author + ":\n" + text;
           if (!String.IsNullOrEmpty(author))
           {
               ExcelFont font = range.Worksheet.Workbook.CreateFont();
               font.KnownColor = ExcelColors.Black;
               font.IsBold = true;
               comment.RichText.SetFont(0, author.Length,font);
           }
           return comment;
        }

Regarding the statement "Gets or sets the author of the comment.", which is a false information. Sorry for inconvenience.
Our dev team is fixing it.

Regards,
Amy
E-iceblue support team
User avatar

amy.zhao
 
Posts: 2328
Joined: Wed Jun 27, 2012 8:50 am

Thu Jun 06, 2013 2:15 pm

Dear Amy,

Thanks for the reply and code, this is very helpful.

Maybe you should also document when or in which situations the component adds the author itself, so that this is more transparent for the user of the library.

Kind regards, Arsène

avonwyss
 
Posts: 4
Joined: Thu Oct 28, 2010 9:37 pm

Fri Jun 07, 2013 3:41 am

Dear Arsène,

Thanks for your feedback and suggestion.
We will publish a document about setting comment author on our website.

There is any issue, please feel free to contact us.

Regards,
Amy
E-iceblue support team
User avatar

amy.zhao
 
Posts: 2328
Joined: Wed Jun 27, 2012 8:50 am

Mon Sep 09, 2013 1:31 am

Dear Arsène,

Thanks for your waiting.
The document has been published in our site. Please refer to the below link.
http://www.e-iceblue.com/Knowledgebase/Spire.XLS/Program-Guide/Add-Comment-with-Author-in-Excel.html

Regards,
Amy
E-iceblue support team
User avatar

amy.zhao
 
Posts: 2328
Joined: Wed Jun 27, 2012 8:50 am

Return to Spire.XLS