//------------------------------------- public static void CreateNewSession() { if (CellsArrayInput == null || CellsListInput == null || minRowInput == 0 || minColInput == 0 || numRowInput == 0 || numColInput == 0) { StopSession(); return; } takeFromExcel = false; firstRun = false; StopSessionObservers(); UseInput(); string data = ""; int minRowFmt = 0, minColFmt = 0, numRowFmt = 0, numColFmt = 0; string[,] fmtArray = ExcelCellsArray.ToFormatArray(CellsList, out minRowFmt, out minColFmt); if (fmtArray != null) { numRowFmt = fmtArray.GetLength(0); numColFmt = fmtArray.GetLength(1); for (int iR = 0; iR < numRowFmt; iR++) { for (int iC = 0; iC < numColFmt; iC++) { data += fmtArray[iR, iC] + "\t"; } } } int minRowVal = 0, minColVal = 0, numRowVal = 0, numColVal = 0; string[,] valArray = ExcelCellsArray.ToValueArray(CellsList, out minRowVal, out minColVal); if (valArray != null) { numRowVal = valArray.GetLength(0); numColVal = valArray.GetLength(1); for (int iR = 0; iR < numRowVal; iR++) { for (int iC = 0; iC < numColVal; iC++) { data += valArray[iR, iC] + "\t"; } } } StartSessionObservers(); ExcelAppleScript.StartExcel("CleanupByMekko"); firstRun = true; timeOpenExcel = DateTime.Now.Ticks; long elapsedTime = (timeOpenExcel - timeStartSession) / TimeSpan.TicksPerMillisecond; if (printDebugOutput) { Console.Out.WriteLine("Excel session started {0} sec", elapsedTime / 1000.0); } ExcelDataSender dataSender = new ExcelDataSender("SetRangeValueByMekko", minRowFmt.ToString(), minColFmt.ToString(), numRowFmt.ToString(), numColFmt.ToString(), minRowVal.ToString(), minColVal.ToString(), numRowVal.ToString(), numColVal.ToString()); dataSender.Send(data); }
//------------------------------------- private static void OnExcelTableChanged(NSNotification notification) { ExcelDataReceiver data = notification.Object as ExcelDataReceiver; if (data.dataList.Count == 0) { if (printDebugOutput) { Console.Out.WriteLine("No data exist on Excel"); } CreateNewSession(); return; } if (observExcelEmpty != null) { NSNotificationCenter.DefaultCenter.RemoveObserver(observExcelEmpty); } observExcelEmpty = null; if (takeFromExcel) // Session data is invalid. Compare Excel data and Input data { takeFromExcel = false; if (ExcelCellsArray.CompareListAndArray(data.dataList, CellsArrayInput, minRowInput, minColInput)) { if (printDebugOutput) { Console.Out.WriteLine("Restore closed session"); } UseInput(); } else { if (printDebugOutput) { Console.Out.WriteLine("Excel and input data are different"); } StopSessionObservers(); ClearSessionData(); DispatchQueue.MainQueue.DispatchAsync(() => { CreateNewSession(); }); } return; } if (firstRun) // First run after session data setted. Compare it with Excel data { timeFirstRespond = DateTime.Now.Ticks; long elapsedTime = (timeFirstRespond - timeOpenExcel) / TimeSpan.TicksPerMillisecond; if (printDebugOutput) { Console.Out.WriteLine("Session first respond time= {0} sec", elapsedTime / 1000.0); } firstRun = false; if (ExcelCellsArray.CompareListAndArray(data.dataList, CellsArray, minRow, minCol) != true) { if (printDebugOutput) { Console.Out.WriteLine("Excel communication error"); } StopSession(); } return; } List <ExcelCellInfo> cellsAdded = new List <ExcelCellInfo> (); List <ExcelCellInfo> cellsChanged = new List <ExcelCellInfo> (); int maxRow = minRow + numRow - 1; int maxCol = minCol + numCol - 1; foreach (ExcelCellInfo cellInfo in data.dataList) { int iR = cellInfo.row; int iC = cellInfo.col; if (iR < minRow || maxRow < iR || iC < minCol || maxCol < iC) { cellsAdded.Add(cellInfo); } else { ExcelCell cell = CellsArray [iR - minRow, iC - minCol]; if (cell.content != cellInfo.content || cell.formula != cellInfo.formula || cell.format != cellInfo.format || cell.prefix != cellInfo.prefix) { if (cell.content == "" && cell.format == "" && cell.formula == "") { cellsAdded.Add(cellInfo); } else { cellsChanged.Add(cellInfo); cell.content = cellInfo.content; cell.formula = cellInfo.formula; cell.format = cellInfo.format; cell.prefix = cellInfo.prefix; } } } } CellsList = null; CellsList = ExcelCellsArray.ToCellList(CellsArray, minRow, minCol); CellsArray = null; if (cellsAdded.Count > 0) { foreach (ExcelCellInfo cellInfo in cellsAdded) { CellsList.Add(cellInfo); } } CellsList.Sort(); CellsArray = ExcelCellsArray.RebuildSessionArray(CellsList, out minRow, out minCol, out numRow, out numCol); if (printDebugOutput && cellsAdded.Count + cellsChanged.Count > 0) { System.Console.Out.WriteLine("------beg----------"); foreach (ExcelCellInfo cellInfo in cellsAdded) { Console.Out.Write("Added : "); cellInfo.Print(); } foreach (ExcelCellInfo cellInfo in cellsChanged) { Console.Out.Write("Changed: "); cellInfo.Print(); } Console.Out.WriteLine("------end---------- total {0} cells\n", cellsAdded.Count + cellsChanged.Count); } data.Dispose(); }
//------------------------------------- public static void UpdateSpreadSheetFromArray(ChartData chartData) { // chartDataArray = chartData; // // int lastRow = chartDataArray.Data.GetLength (0); // int lastCol = chartDataArray.Data.GetLength (1); // if (lastRow < 2 && lastCol < 2) // { // //Logger.LogVerbose("UpdateSpreadSheetFromArray", "Data Array is empty!"); // return; // } // // CellsListInput = new List<ExcelCellInfo> (); // // DataCell cell = null; // for (int row = 0; row < lastRow; row++) // { // for (int col = 0; col < lastCol; col++) // { // cell = (DataCell)chartDataArray.Data.GetValue(row, col); // if (cell != null) // { // ExcelCellInfo cellInfo = new ExcelCellInfo (cell); // CellsListInput.Add (cellInfo); // } // } // } // CellsListInput.Sort (); CellsListInput = new List <ExcelCellInfo> (); List <ExcelCellInfo> cellsInput = TestList(); foreach (ExcelCellInfo info in cellsInput) { CellsListInput.Add(info); } CellsListInput.Sort(); takeFromExcel = false; firstRun = false; CellsArrayInput = ExcelCellsArray.RebuildSessionArray(cellsInput, out minRowInput, out minColInput, out numRowInput, out numColInput); if (observExcelChanges != null && CellsArray != null && CellsList != null && minRowInput == minRow && minColInput == minCol && numRowInput == numRow && numColInput == numCol) { bool theSame = true; for (int iR = 0; iR < numRow; iR++) { for (int iC = 0; iC < numCol; iC++) { if (CellsArray [iR, iC] != CellsArrayInput [iR, iC]) { theSame = false; break; } } if (!theSame) { break; } } if (theSame) { if (printDebugOutput) { Console.Out.WriteLine("Old session and input data are the same"); } StopSessionObservers(); ClearInputData(); StartSessionObservers(); ExcelAppleScript.StartExcel(); return; } else { if (printDebugOutput) { Console.Out.WriteLine("Old session and input data are different"); } StopSessionObservers(); ClearSessionData(); DispatchQueue.MainQueue.DispatchAsync(() => { CreateNewSession(); }); return; } } else { if (printDebugOutput) { Console.Out.WriteLine("No old session. Read data from Excel"); } StopSessionObservers(); ClearSessionData(); takeFromExcel = true; StartSessionObservers(checkEmptyRespond: true); ExcelAppleScript.StartExcel("SendContentToMekko"); return; } }