public static unsafe int TryExcelImpl(int xlFunction, out object result, params object[] parameters) { int xlReturn; // TODO: EXCEL2007 - Review for multithreaded.... // Set up the memory to hold the result from the call XlOper resultOper = new XlOper(); resultOper.xlType = XlType.XlTypeEmpty; XlOper* pResultOper = &resultOper; // No need to pin for local struct // Special kind of ObjectArrayMarshaler for the parameters (rank 1) using (XlObjectArrayMarshaler paramMarshaler = new XlObjectArrayMarshaler(1, true)) { XlOper** ppOperParameters = (XlOper**)paramMarshaler.MarshalManagedToNative(parameters); xlReturn = Excel4v(xlFunction, pResultOper, parameters.Length, ppOperParameters); } // pResultOper now holds the result of the evaluated function // Get ObjectMarshaler for the return value ICustomMarshaler m = XlObjectMarshaler.GetInstance(""); result = m.MarshalNativeToManaged((IntPtr)pResultOper); // And free any memory allocated by Excel Excel4v(xlFree, (XlOper*)IntPtr.Zero, 1, &pResultOper); return xlReturn; }
public static unsafe IntPtr GetCallerSheetId4() { IntPtr retval = IntPtr.Zero; XlOper resultOper = new XlOper(); XlOper* pResultOper = &resultOper; int xlReturn; xlReturn = Excel4v(xlfCaller, pResultOper, 0, (XlOper**)IntPtr.Zero); if (xlReturn == 0) { if (resultOper.xlType == XlType.XlTypeReference) { retval = resultOper.refValue.SheetId; Excel4v(xlFree, (XlOper*)IntPtr.Zero, 1, &pResultOper); } } return retval; }
private static unsafe extern int Excel4v(int xlfn, XlOper* pOperRes, int count, XlOper** ppOpers);
public static unsafe IntPtr GetCurrentSheetId4() { // In a macro type function, xlSheetNm seems to return the Active sheet instead of the Current sheet. // So we first try to get the Current sheet from the caller. IntPtr retval = GetCallerSheetId4(); if (retval != IntPtr.Zero) return retval; // Else we try the old way. XlOper SRef = new XlOper(); SRef.xlType = XlType.XlTypeSReference; //SRef.srefValue.Count = 1; //SRef.srefValue.Reference.RowFirst = 1; //SRef.srefValue.Reference.RowLast = 1; //SRef.srefValue.Reference.ColumnFirst = 1; //SRef.srefValue.Reference.ColumnLast = 1; XlOper resultOper = new XlOper(); XlOper* pResultOper = &resultOper; XlOper* pSRef = &SRef; XlOper** ppSRef = &(pSRef); int xlReturn; xlReturn = Excel4v(xlSheetNm, pResultOper, 1, ppSRef); if (xlReturn == 0) { XlOper resultRef = new XlOper(); XlOper* pResultRef = &resultRef; xlReturn = Excel4v(xlSheetId, pResultRef, 1, (XlOper**)&(pResultOper)); // Done with pResultOper - Free Excel4v(xlFree, (XlOper*)IntPtr.Zero, 1, &pResultOper); if (xlReturn == 0) { if (resultRef.xlType == XlType.XlTypeReference) { return resultRef.refValue.SheetId; } // Done with ResultRef - Free it too Excel4v(xlFree, (XlOper*)IntPtr.Zero, 1, &pResultRef); } } return retval; }
internal static unsafe void SetExcelReference(XlOper* pOper, XlOper.XlMultiRef* pMultiRef, object /*ExcelReference*/ r) { IntPtr sheetId = ExcelReferenceGetSheetId(r); int[][] rects = ExcelReferenceGetRectangles(r); int rectCount = rects.GetLength(0); pOper->xlType = XlType.XlTypeReference; pOper->refValue.SheetId = sheetId; pOper->refValue.pMultiRef = pMultiRef; pOper->refValue.pMultiRef->Count = (ushort)rectCount; XlOper.XlRectangle* pRectangles = (XlOper.XlRectangle*)(&pOper->refValue.pMultiRef->Rectangles); for (int i = 0; i < rectCount; i++) { pRectangles[i].RowFirst = (ushort)rects[i][0]; pRectangles[i].RowLast = (ushort)rects[i][1]; pRectangles[i].ColumnFirst = (byte)rects[i][2]; pRectangles[i].ColumnLast = (byte)rects[i][3]; } }
public unsafe static IntPtr GetCurrentSheetId4() { IntPtr retval = IntPtr.Zero; XlOper SRef = new XlOper(); SRef.xlType = XlType.XlTypeSReference; //SRef.srefValue.Count = 1; //SRef.srefValue.Reference.RowFirst = 1; //SRef.srefValue.Reference.RowLast = 1; //SRef.srefValue.Reference.ColumnFirst = 1; //SRef.srefValue.Reference.ColumnLast = 1; XlOper resultOper = new XlOper(); XlOper* pResultOper = &resultOper; XlOper* pSRef = &SRef; XlOper** ppSRef = &(pSRef); int xlReturn; xlReturn = Excel4v(xlSheetNm, pResultOper, 1, ppSRef); if (xlReturn == 0) { XlOper resultRef = new XlOper(); XlOper* pResultRef = &resultRef; xlReturn = Excel4v(xlSheetId, pResultRef, 1, (XlOper**)&(pResultOper)); // Done with pResultOper - Free Excel4v(xlFree, (XlOper*)IntPtr.Zero, 1, &pResultOper); if (xlReturn == 0) { if (resultRef.xlType == XlType.XlTypeReference) { return resultRef.refValue.SheetId; } // Done with ResultRef - Free it too Excel4v(xlFree, (XlOper*)IntPtr.Zero, 1, &pResultRef); } } return retval; }