Is there any way for Validating cell value in excel (vsto) - c#

Suppose i have some value in excel cell,its type may be anything date,numeric or string
and i want that cell to be validated against its type...
Is there any way of doing this ?
thanks in advance..

I assume you mean that you have contents in a cell, which could be either a date, a double, or a plain string, and that you want to assert what corresponding .NET type it is. I believe there is no direct way of doing this. One approach would be to retrieve the Value2 in the cell, and try to cast it to each of these types, starting from the most restrictive one, until the cast works - i.e. DateTime (DateTime.FromOADate), then double (Convert.ToDouble), then string - and then apply the validation rule that applies to the particular type you found.
I am not quite sure what you mean by validation, though, and what that would buy you. Once you know the type of the content, what would you do with it?

I use Excel's data validation technique in a VSTO application myself. It obviously isn't VSTO but it works well enough. There are some drawbacks like you can't have multiple validations on the same cell (i.e. you have to know what your validating for)

You can directly use the excel's functionality !!!
no need of any coding for that !!
instead of hard work go for the smart work ...
In excel,
Go to the cell where you want the validation to come
Menu ->> Data --> validation
Here you can directly use the various excel validation terms.
Which you can refer here or..
http://support.microsoft.com/kb/211485
Thanx
Regards.

Related

Excel-DNA: grouping rows via C API feature of Excel-DNA

I'm familiar with how to group a range in Excel VSTO/COM interop:
ws.EnableOutlining = true;
ws.Outline.SummaryRow = XlSummaryRow.xlSummaryAbove;
var rng = GetRangeSomeHow();
rng.EntireRow.Group();
rng.EntireRow.OutlineLevel = someLevel;
What is the most efficient way to do this in Excel-DNA? I would imagine there must be a C-API way to do it, encapsulated cleverly in Excel-DNA somehow, but for the life of me, I can't figure it out via online documentation (incl. Google).
There's a lot of posts using code similar to my sample above, but these are pretty expensive calls, especially since I need to do this ~5000 times overall (I have a really big data set).
EDIT:
So there seems to be this method call:
XlCall.Excel(XlCall.xlfGroup...)
The only problem is, I have no idea what the parameters are. It seems an ExcelReference should be passed in, but how is the .EntireRow resolved? Will the C API just handle it for me - in which case I just need to pass a new ExcelReference(1,100,1,1) and be done with it... or is there more to this?
Thanks in advance to anyone who can answer my question!
I don't think the C API GROUP function is te one you're looking for. The documentation says:
GROUP
Creates a single object from several selected objects and returns the
object identifier of the group (for example, "Group 5"). Use GROUP to
combine a number of objects so that you can move or resize them
together.
If no object is selected, only one object is selected, or a group is
already selected, GROUP returns the #VALUE! error value and interrupts
the macro.
I'd suggest you use the COM object model for this kind of thing, even in an Excel-DNA add-in. The C API has not really been updated over the years for the general sheet manipulation like this case, so you're likely to run into some features that don't work right or are incomplete relative to the COM object model.
From your Excel-DNA add-in, just make sure your get hold of the right Application root object with a call to ExcelDnaUtil.Application.
For improved performance of this kind of sheet editing, you pretty much have to use the same tricks as from VBA or VSTO - disable screen updating and calculations etc.

Cast or Convert the Powerpoint.TextRange to a Word.Range

[Short Question] :
How can I Cast or Convert a PowerPoint.TextRange to a Word.Range ?
[Longer description ] :
I'm trying to mark a selected text range as "Do Not Check Spelling",
in PowerPoint 2013, from my code. It's a PowerPoint Add-In written in C#,
but I could adapt it from any other sample in other language (VBA, VB, PowerShell...)
Of course doing it manually with the Review, Language Settings is working perfectly.
My code gets the selected range in a Microsoft.Office.Interop.PowerPoint.TextRange
read from Globals.ThisAddIn.Application.ActiveWindow.Selection.TextRange;
(I've also started some tests with a Microsoft.Office.Core.TextRange2)
These objects contain a LanguageID property. Setting it to msoLanguageIDNoProofing is ignored (other real languages work, though). It seems coherent with the GUI display, where there are two properties, the selected language and a separate checkbox to disable the Spell Check.
I've displayed the TextRange properties for two similar ranges, one with the "Do Not Check Spelling" setting set manually... and it seems there's not an evident difference on them.
So I found by chance the Microsoft.Office.Interop.Word.Range object.
It contains both the LanguageID, and a separate NoProofing property...
so probably PowerPoint is using this Word object when editing Ranges.
My question is, how can I Cast or Convert the Powerpoint.TextRange to a Word.Range ?
These are entirely different objects. There is no direct conversion between these types. You can copy property values from one and set them on another. Or just use the Copy/Paste operations programmatically.

What's the difference between Entity.Attributes and Entity.FormattedValues?

I'm learning how to write custom workflows and am trying to figure out where and in which format all the values I need are stored. I have noticed that I can access Entity instance data in both the Attributes and FormattedValues properties. How do I know when to use which one?
I have noticed MSDN's remark "Entity formatted values are only available on a retrieve operation, not on an update operation.".
For testing I've made two foreach-blocks iterating through both collections. Attributes gives me 65 lines and FormattedValues gives me 39. I can see that, yes, the output from FormattedValues is indeed formatted.
For example, where Attributes gives the output "Microsoft.Xrm.Sdk.OptionSetValue", FormattedValues gives me a string with the actual value.
Which values/attributes are generally excluded from the FormattedValues collection and why?
I'm not 100% sure about this, but the formatted values are the values you will be able to see on the form. In that list you will be able to find money types with the $ symbol, or the labels of the option sets. A text field shouldn't be shown since is already human-readable.
https://community.dynamics.com/crm/b/crmmitchmilam/archive/2013/04/18/crm-sdk-nugget-entity-formattedvalues-property.aspx
Refer to this article to know a little bit more about it. I rareley using that attribute list since the data is in string format. I found it really useful to retrieve the OprionSet lables.
After a quick check it'd appear that the difference between an attribute and a formatted value is that the former to the actual value stored in the DB (or at least the value that was stored there at the occasion of fetching it) while the latter serves what's shown to the user.
I haven't used formatted values but until proven otherwise, I'd say that an entity's attribute will provide you with the typed value that the regarded field is based on (be that int, DateTime and such), whereas its formatted value is the rendered, stringified representation of that value (being dependent on e.g. what form you're referring to, what language etc.)
By that logic, I'd expect the set of formatted values to be a subset to the set of attributes. Also, it should be constituted of exclusively String typed values, while the counterpart is a member of the type conversion table.
An example of the difference I can think of is an option set called picky with the currently selected option named "hazaa" and the ID of 1234. The below sample is written by heart so feel free to correct. It exemplifies the point, though: plainValue will be an integer equal to 1234, while formattedValue will be "hazaa".
int plainValue = (int)entity["picky"];
String formattedValue = (String)entity.FormattedValues["picky"];
I'd say that the attributive approach is more reliable as it'll render the actual values, while the alternative can lead to unexpected outcome. However, there's a certain convenience to it, I must add.
Personally, I'd recommend to look into the method GetAttributeValue<T>(String) or, as every cocky CRM developer would - have your own class with extension methods and use the method Get<T>(T,String) in there. That provides you with the sense of control and offers the best predictability and readability, IMAO.

Custom .Net library or existing library to handle sequence formatting?

I am thinking a library already exists for this, but I need allow my users to create a numbering format for their documents.
For example, let's say we have an RFI from and the user has a specific format the numbering sequence needs to be in. A typical RFI number looks like this for their system: R0000100. The next RFI in line would be R0000101.
Before I set out to creating a formatting engine for numbers such as these, does something already exist that can accommodate this?
Update:
I failed to save the edit to this question. Anyway, I also want to give the users the ability to create their own formats. So, I may have a form where they can input the format: R####### And also allow them to specify the starting integer: in the case 100. Also, I may want to allow them to specify how they want to increment. maybe only by 100s. So the next number may be R0000200. I know this may sound ridiculous, but you never know. That is why I asked if something like this already exists.
If you keep value and format separated, you won't need a library or such a thing.
The numbers would be simple, say, integers i, i.e. 100, 101, 102, that you manage/store however you see fit. The formatting part would simply be a matter of R + i.ToString("0000000"), or if you want to have the format as a string literal string.Format("R{0:0000000}", i).
I know, this might only be an example, but as your question stands, the formatting options, that .NET provides out of the box seem to suffice.
The incrementing of identity field values is most often handled in an RDBMS-style database. This comes with a few benefits, such as built-in concurrency handling. If you want to generate the values yourself, a simple class to get the last-issued value and increment by one would be very easy to create. Make it thread-safe so you don't get any duplicates or gaps and you'll be good to go.

Milliseconds missing when getting a DateTime value from Excel using .Net Interop

If I put a DateTime value into an Excel cell using Range.set_value through .Net COM Interop, and then retrieve the value of that same cell using Range.get_value, the Millisecond part of the value is not returned, though everything else is correct.
Is this a bug?
What is the workaround? I'm guessing that using the Value2 property instead might help. Has anybody else tried this?
If you set a date/time with a millisecond value in Excel manually, does it maintain it? I don't know about the Excel internal object model, but it's conceivable that it just doesn't support milliseconds.
EDIT: Okay, now we know that the set fails (the get may also fail, of course)... you could try setting it as a double after converting it via DateTime.ToOADate(). I'm not saying I'm holding out much hope, but it's worth a try...
As Jon suggested, converting the DateTime to a double using DateTime.ToOADate (then back again using DateTime.FromOADate) works if you set the value using the Range.Value2 property.
The only trouble with this property is that, if you don't already know, it doesn't tell you that the cell is supposed to be treated as a DateTime. I guess to solve this, you'd need to use a two-pass approach: get cell values using Range.get_Value to determine their type, then, for any DateTime cells, get their values again using Range.Value2, then convert using DateTime.FromOADate.
It's probably because of the issue described in this KB article.
In which case, setting the Value2 property should work.

Categories