Extracting Slope & Intercept from a Scatterplot Trendline Programmatically - c#

I cannot seem to find a built in property for Excel Chart Trendlines where I can grab the slope or the intercept. It allows me to display the trendline equation on the chart itself in excel, but I can't access it within the code. Anyone know any property or method that I may have been overlooking? I can always calculate the equation separately, it just seems like a waste if Excel already has it, but I can't access it.

I kindof found what I was looking for. I would ideally like to extract the trendline directly from the Excel chart, but I found a way to access excel functions within the code. Forward Ed put me on the right track.
using IExcel = Microsoft.Office.Interop.Excel;
Then declare and initialise a IWorkSheetFunction object
IExcel.IWorksheetFunction iwf;
Now you can use the functions as in
double result = iwf.NormDist(1,2,3,4);

Related

Powerpoint "Save As Picture" from C# Microsoft.Office.Interop.PowerPoint

My question is pretty similar to this one and I'm afraid the answer is the same... I want to save all the shapes/images on a slide as a single png (or jpeg). Programmatically, I get as far as
slide.Shapes.SelectAll();
but don't see a way to save as image. Is this possible? If not, any other suggestions, hopfully w/ examples? (not VBA - I need to automate the whole conversion)
There was a reference to OpenXML in the other post, but I'm not even sure how to pull that in.
I don't know how you'd do this in C# but I'd guess that you'd make use of the same methods as you would with VBA, where you can do:
Activewindow.Selection.ShapeRange.Export( "c:\temp\delete-me.jpg",ppShapeFormatJPG)
ppShapeFormatJPG is a PowerPoint constant, a VBA Long = 1; IIRC that'd be an Integer in C#.
The method also can take two more optional parameters, scalewidth and scaleheight, which govern the width and height of the exported image in undocumented ways. By default, no parms supplied, I get exports at 72 dpi. Larger numbers result in higher pixel count exports but distorted proportions. I'm sure there's some strange logic to it, but it escapes me; all hints welcome!
There's a third optional parm, ExportMode. In my tests, it makes no difference whether you supply it or not, and if you do, which of the available values you choose.

Indirect code execution/C#

Background
We currently have an excel-based system for the creation of specifications for a sound and lighting rental company.
Part of this is a column in the excel sheet called 'autospec', which is made up of Excel formulas for individual stock items ( e.g., you specify a loudspeaker, and then the formulas in the autospec column calculate the cables you need and add them onto the specification automatically.
sample formula
10m microphone cable might have a formula like the following:
=IF([loudspeakers]>0,[loudspeakers]*2,0)+([mixing desk]*4)
We're now moving over to a proper database with a C# front end.
Question
What I would like is to be able to store the autospec formulas for each stock item in a table, and when the user specs an item the front end, the program should find the relevant formula, execute it, and change the spec quantity as appropriate.
Bottom line: I need to execute code contained within a string.
Am I going about this the wrong way?
Is there a better way?
I asked a similar question once: How can I evaluate a C# expression dynamically?
You could probably use that to evaluate those expressions. This not really a safe thing to do, unless you can guarantee nobody will be adding junk (read: evil code) to your database.
If you can break down the formulas into "families", such that each entry in your formula column is a member of a small (5-10) set of formulas with just different parameters, you could try something like this:
[ItemTable]<-[ItemFormulaParameters(param1, param2, param3)]->[FormulaTable(name)]
And have a factory method for instantiating the formula objects by name. Each such formula object has a "calculate(param1, param2, param3)" property...
Either build you're own code interprenter or build a 'dynamic rule engine' that creates some binary representation of c# rules that can be executed by a 'rule engine' you'd have to write.

Getting Excel add ins to modify array formula parameters; or perform 'ctrl-shift-enter'

I am trying to make a C# Excel add in change the parameters of an array formula in-place; i.e. do the same as a user modifying an array formula and hitting ctrl-shift-enter.
Setting the activeCell.FormulaArray property does not achieve this; it throws a 'You cannot change part of an array' error.
Does anyone know how I can achieve this?
A solution that also works in VBA would be brilliant.
I've tried creating some logic that 'walks' to the perimeter of the array formula and deletes it first, but it doesn't account for adjacent array formulas and I believe this is unnecessarily drastic.
Sounds like you are looking for the CurrentArray property
In VBA this will enter a multicell array formula into all the cells of the array formula one cell of which is Z99
Range("Z99").CurrentArray.FormulaArray=" ... "
Adjust.zip on http://tukhi.com/tukhi_fun.html does this.

Can I set auto-width on an Open XML SDK-generated spreadsheet without calculating the individual widths?

I'm working on creating an Excel file from a large set of data by using the Open XML SDK. I've finally managed to get a functional Columns node, which specifies all of the columns which will actually be used in the file. There is a "BestFit" property that can be set to true, but this apparently does not do anything. Is there a way to automatically set these columns to "best fit", so that when someone opens this file, they're already sized to the correct amount? Or am I forced to calculate how wide each column should be in advance, and set this in the code?
The way I understand the spec and this MSDN discussion, BestFit tells you the width was auto-calculated in Excel, but it does not tell Excel that it should calculate it again next time it is opened.
As "goodol" indicates in that discussion, I think the width can only be calculated when you display the column, since it depends on the contents, the font used, other style parameters... So even if you want to pre-calculate the width yourself, be aware that this is only an estimation, and it can be wrong if the contents contain lots of "wide" characters. Or does the Open XML SDK do this for you?
I'm using EPPlus which I highly recommend. Took me a while to figure out how to do it using that, here's what I came up with:
// Get your worksheet in "sheet" variable
// Set columns to auto-fit
for (int i = 1; i <= sheet.Dimension.Columns; i++)
{
sheet.Column(i).AutoFit();
}

Regex and Excel Cells

There is a .NET opensource library called NPOI that allows you to manipulate Excel files. Unfortunately, their ShiftRows function does not adjust any cell references in formulas.
Therefore, I need to create a regex pattern to update them. Take for example a cell containing the following formula:
=(B7/C9) * (A10-B4)
I would like to bump any row references by 1 thus becoming
=(B8/C10) * (A11-B5)
Basically, I just need a pattern that will extract the numbers out into a "MatchCollection". I can do the rest.
Can anyone help?
Thanks.
Take a look at my answer to another question: Which regular expression is able to select excel column names in a formula in C#?
In that answer I match formulas and include code to increment the cell number (and column naming). Also, do a search on Excel column naming here and you'll find other ways to get the names generated or incremented. I probably could shorten the IncrementColumn MatchEvaluator's code using one of those methods but that's what I came up with at the time.
You should be very careful using regular expressions for this purpose.
Excel formulas can become very complex, especially with user-defined functions or when functions apply to ranges.
NPOI contains a Formula evaluation library and I think you should have a look at this as well - especially if the spreadsheets are out of your control.

Categories