AutomationElement can't find row cell values (but UISpy does) - c#-4.0

I'm trying to get data from a window that has 3 lists (SysListView32).
I do the same on the three of them, both UISpy and Inspect DO see rows and cells in the three of them, but when in code two of them works perfect (Caption="List2" and Caption="List3"), but the third contains ONLY white empty strings.
If I try:
IntPtr PrizeListHandle = Win32Utils.FindWindowByCaption(Lobby, "List1");
IUIAutomationElement dataGridPrizes = autom.ElementFromHandle(PrizeListHandle);
IUIAutomationGridPattern gridPrizes = dataGridPrizes.GetCurrentPattern(10006);
string linea = gridPrizes.GetItem(0, 0).CurrentName;
linea turns to be and empty string, gridPrizes has 17 rows, 3 columns, all cells are empty strings.
If I try:
IntPtr PrizeListHandle = Win32Utils.FindWindowByCaption(Lobby, "List1");
IUIAutomationElement dataGridPrizes = autom.ElementFromHandle(PrizeListHandle);
int propIdClassName = 30004; // UIA_ClassNamePropertyId;
IUIAutomationPropertyCondition conditionListItem = (IUIAutomationPropertyCondition)autom.CreatePropertyCondition(propIdClassName, "list item");
IUIAutomationElementArray children = dataGridPrizes.FindAll(interop.UIAutomationCore.TreeScope.TreeScope_Children, conditionListItem);
int i = children.Length;
i is 0. Why?
It's important to note that when using UISpy it finds EVERYTHING. Also, I have tried both managed an unmanaged version on Automation, with same exact results.
I'm using Windows Server 2008 R2.
Here's a screenshot:
Thank you in advance

You need use ScrollItemPattern.ScrollIntoView() bring into view.
var pattern = (ScrollItemPattern)aeDataGridCell
.GetCurrentPattern(ScrollItemPatternIdentifiers.Pattern);
pattern.ScrollIntoView();

Related

Why is my "defined name" (range) value not being set with this Spreadsheet Light code?

I've got this code to apply a "header" (big, top-of-the-sheet "title") to a sheet:
// Initialize
private static SLDocument sl;
. . .
sl = new SLDocument();
// Create a Style
SLStyle styleHeading = sl.CreateStyle();
styleHeading.SetFont(FontSchemeValues.Major, 36);
styleHeading.Font.Italic = true;
styleHeading.Font.FontName = "Candara";
// Create a Defined Name (Range) and give it a value and style
sl.SetDefinedName("UnitName", "Sheet1!$A$1:$A$13");
sl.SetCellValue("UnitName", "Pennsylvania Platypi Presumptuously Parasailing");
sl.SetCellStyle("UnitName", styleHeading);
// Save the sheet
string appDataFolder = HttpContext.Current.Server.MapPath("~/App_Data/");
string spreadsheetLightFilename = "PlatypiTest.xlsx";
string fullspreadsheetLightPath = Path.Combine(appDataFolder, spreadsheetLightFilename);
sl.SaveAs(fullspreadsheetLightPath);
Note: I verified that "Sheet1" was right with this code:
var nameList = sl.GetSheetNames();
string s = nameList[0]; // "s" is "Sheet1"
The file is created and saved, but it is devoid of content; when I open it, cell A1 is highlighted, but is content-free.
Am I missing a vital step, or going about this completely wrong?
What are you doing is logically fine.
This line
sl.SetDefinedName("UnitName", "Sheet1!$A$1:$A$13");
indeed creates a named range. You can see it if you open the resulting file in Excel and look at the cell selector:
or the Name Manager:
The problem is though that Spreadsheet Light has a very basic support for Defined names - basically all you can do is to create a name and use it inside the formulas. All methods that manipulate content expect single cell reference. Btw, all these methods do not throw exception if you don't pass a valid cell reference, but return bool indicating success/failure.
For instance, if you change your code to
bool success1 = sl.SetCellValue("UnitName", "Pennsylvania Platypi Presumptuously Parasailing");
bool success2 = sl.SetCellStyle("UnitName", styleHeading);
you will see that both success variables are false.
Shortly, if you want to bring some content to the Excel file, you should do it cell by cell. It even does not support regular (unnamed) ranges.
Theoretically, at least, you could do it this way:
// from http://stackoverflow.com/questions/36481802/what-is-the-analogue-to-excel-interops-worksheet-usedrange-rows-in-spreadsheet
var stats = sl.GetWorksheetStatistics();
var rowcount = stats.NumberOfRows;
SLStyle entireSheetRangeStyle = sl.CreateStyle();
entireSheetRangeStyle.// (set style vals)
. . .
sl.SetRowStyle(1, rowcount, entireSheetRangeStyle);

Matlab Data Preprocessing and Dynamic Struct Assignments

I'm quite new to Matlab and I'm struggling trying to figure out how to properly preprocess my data in order to make some calculations with it.
I have an Excel table with financial log returns of many companies such that every row is a day and every column is a company:
I imported everything correctly into Matlab like this:
Now I have to create what's caled "rolling windows". To do this I use the following code:
function [ROLLING_WINDOWS] = setup_returns(RETURNS)
bandwidth = 262;
[rows, columns] = size(RETURNS);
limit_rows = rows - bandwidth;
for i = 1:limit_rows
ROLLING_WINDOWS(i).SYS = RETURNS(i:bandwidth+i-1,1);
end
end
Well if I run this code for the first column of returns everything works fine... but my aim is to produce the same thing for every column of log returns. So basically I have to add a second for loop... but what I don't get is which syntax I need to use in order to make that ".SYS" dynamic and based on my array of string cells containing company names so that...
ROLLING_WINDOWS(i)."S&P 500" = RETURNS(i:bandwidth+i-1,1);
ROLLING_WINDOWS(i)."AIG" = RETURNS(i:bandwidth+i-1,2);
and so on...
Thanks for your help guys!
EDIT: working function
function [ROLLING_WINDOWS] = setup_returns(COMPANIES, RETURNS)
bandwidth = 262;
[rows, columns] = size(RETURNS);
limit_rows = rows - bandwidth;
for i = 1:limit_rows
offset = bandwidth + i - 1;
for j = 1:columns
ROLLING_WINDOWS(i).(COMPANIES{j}) = RETURNS(i:offset, j);
end
end
end
Ok everything is perfect... just one question... matlab intellissense tells me "ROLLING_WINDOWS appears to change size on every loop iteration bla bla bla consider preallocating"... how can I perform this?
You're almost there. Use dynamic field names by building strings for fields. Your fields are in a cell array called COMPANIES and so:
function [ROLLING_WINDOWS] = setup_returns(COMPANIES, RETURNS)
bandwidth = 262;
[rows, columns] = size(RETURNS);
limit_rows = rows - bandwidth;
%// Preallocate to remove warnings
ROLLING_WINDOWS = repmat(struct(), limit_rows, 1);
for i = 1:limit_rows
offset = bandwidth + i - 1;
for j = 1:columns
%// Dynamic field name referencing
ROLLING_WINDOWS(i).(COMPANIES{j}) = RETURNS(i:offset, j);
end
end
end
Here's a great article by Loren Shure from MathWorks if you want to learn more: http://blogs.mathworks.com/loren/2005/12/13/use-dynamic-field-references/ ... but basically, if you have a string and you want to use this string to create a field, you would do:
str = '...';
s.(str) = ...;
s is your structure and str is the string you want to name your field.

OLE2 command for returning the number of columns in an Excel file

Do we have an OLE2 command that can be used in Oracle Forms to return the number of columns in my excel file?!
I want to open an excel file from Oracle Forms and go through all columns and then just some the colomns.
Thanks!
To accomplish this I use a trivial solution, I iterate through all the col until the first empty one in the first line. Of course my file have always have the first row's col filled.
When I have a non filled row I use a constant, manually defined...
I iterate through like this:
Variant ws = /*(set your worksheet here)*/;
int col = 1;
for (int col = 1; toString(ws.olePropertyGet("Cell", row, col).olePropertyGet("value")) != ""); ++col)
//do stuff ++count;
It is dirty, but I never found a better way to do this, and I will follow this question to find a new one.

OleDB Jet - Float issues in reading excel data

When I read a sheet into a DataTable using the OleDbDataReader, floating point numbers loose their precision.
I tried forcing OleDb to read the excel data as string, but although the data is now contained in a DataRow with each Column defined as System.String it looses precision (18.125 -> 18.124962832).
Any idea how to avoid this behaviour?
I just tested your data and this method posted here worked.
i.e. the cell value kept it's precision 18.124962832, when put into DataSet.
I'm pretty sure that Jet tries to assign a datatype to each column based on what it sees in the first five rows. If something after those first five rows doesn't fall into that data type it will either convert it or return nothing at all.
Do the first five rows of your spreadsheet have a lower precision than the items that are begin truncated?
Take a look at this post.
The output from the code below shows you how to get the underlying number and the formatted text with SpreadsheetGear for .NET:
Here is the output from the code:
x=18.124962832 y=18.124962832 formattedText=18.125
Here is the code:
namespace Program
{
class Program
{
static void Main(string[] args)
{
// Create a new workbook and get a reference to Sheet1!A1.
var workbook = SpreadsheetGear.Factory.GetWorkbook();
var sheet1 = workbook.Worksheets[0];
var a1 = workbook.Worksheets[0].Cells["A1"];
// Put the number in the cell.
double x = 18.124962832;
a1.Value = x;
a1.NumberFormat = "0.000";
double y = (double)a1.Value;
string formattedText = a1.Text;
System.Console.WriteLine("x={0} y={1} formattedText={2}", x, y, formattedText);
}
}
}
You can see live SpreadsheetGear samples here and download the free trial here.
Disclaimer: I own SpreadsheetGear LLC

Obtaining First Excel Sheet Name With OleDbConnection

I have one problem. I need to get the excel sheet name in a work book, which looks on the very left sheets tab -the first one from my point of view.
I am using this code:
public static string GetFirstExcelSheetName(OleDbConnection connToExcel)
{
DataTable dtSheetName =
connToExcel.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
List<String> lstExcelSheet = new List<string>(dtSheetName.Rows.Count);
foreach (DataRow row in dtSheetName.Rows)
lstExcelSheet.Add(row["TABLE_NAME"].ToString());
return lstExcelSheet[0];
}
The problem here is it is returning the rows not in the visual tab order but in a very different order - most probably the row created date.
How can it be possible to get the sheetnames table ordered according to their tab order so that I can easily get the 1st excel sheet name?
Thanks,
kalem keki
It should be the zero-th item in the workbooks(?) collection.
I think you have the right index, wrong collection.
Sorry, didn't notice you're using the rows collection of a datatable.
That's a different problem.
How do you create the datatable?
You might have to change the sort property of the dataview.
Dim dtSheetnames As DataTable = oleDBExcelConnection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, New Object() {Nothing, Nothing, Nothing, "TABLE"})
Dim FirstSheetName As String = dtSheetnames.Rows(0)!TABLE_NAME.ToString
The row 0 is not the first sheet in the excel file, rows are sorted by alphabetical order in this collection :/
I recommend using the NPOI library (http://npoi.codeplex.com/) rather than OleDB to retrieve data (including metadata) from Excel.
IIRC, OleDB will also fail for sheet names that include spaces or dollar signs.
OleDbConnection oconn = new OleDbConnection(#"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Session["path"].ToString() + "; Extended Properties=Excel 12.0;Persist Security Info=False;");
oconn.Open();
myCommand.Connection = oconn;
DataTable dbSchema = oconn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
if (dbSchema == null || dbSchema.Rows.Count < 1)
{
throw new Exception("Error: Could not determine the name of the first worksheet.");
}
string firstSheetName = dbSchema.Rows[0]["TABLE_NAME"].ToString();

Resources