public static void customFunctionAutoRun(string cat, formulaColumns frmlaColumnsPair, JArray jsData, string columnsToUse) { try { if (MyRibbon.refresh != true) { helperClass.setGlobalDict(formulaCell.Address[false, false], frmlaColumnsPair); } if (jsData.Count == 0) { MessageBox.Show("No data provided for selected parameters"); } else { try { helperClass.elseFunction(columnsToUse, jsData, dataStartCell, newFormula, formulaCell); } catch (Exception ex) { helperClass.log.Info(ex.Message); helperClass.log.Trace(ex.StackTrace); throw; } } } catch (Exception ex) { helperClass.log.Error(ex); } }
public void PopulateData() { helperClass.log.Info("PopulateData from newPrintData"); try { // Acquire Mutex to avoid multiple functions writing at the same time. DataWriteMutex.WaitOne(); // Since this is executing in a thread wait for excel to be finished whatever calculations its currently doing before writing to other cells. Helps avoid some issues. if (_threaded) { WaitForExcelToBeReady(); } header_to_excel(); data_to_excel(); //Writing final dictionary WaitForExcelToBeReady(); Range endCell = _currentWorksheet.Cells[_dataStartCell.Row + _data.Count, _dataStartCell.Column + _names.Length - 1]; //.Split(',').Length - 1]; Range used = _currentWorksheet.Range[_dataStartCell, endCell]; formulaColumns frmlaColumnsPair2 = new formulaColumns(_newFormula, String.Join(",", _names), used, _formulaCell); //_names, used, _formulaCell); MyRibbon.myFormulasDict[_formulaCell.Address[false, false]] = frmlaColumnsPair2; if (MyRibbon.myMainDict.ContainsKey(MyRibbon.sheet.Index.ToString())) { MyRibbon.myMainDict[MyRibbon.sheet.Index.ToString()] = MyRibbon.myFormulasDict; } else { MyRibbon.myMainDict.Add(MyRibbon.sheet.Index.ToString(), MyRibbon.myFormulasDict); } DataWriteMutex.ReleaseMutex(); } catch (COMException ex) { Trace.WriteLine(ex.Message); helperClass.log.Info(ex.Message); helperClass.log.Trace(ex.StackTrace); // Release Mutex to allow another function to write data. DataWriteMutex.ReleaseMutex(); // The excel RPC server is busy. We need to wait and then retry (RPC_E_SERVERCALL_RETRYLATER or VBA_E_IGNORE) if (ex.HResult == -2147417846 || ex.HResult == -2146777998) { Thread.Sleep(RetryWaitTimeMs); PopulateData(); return; } throw; } Marshal.ReleaseComObject(MyRibbon.sheet); Marshal.ReleaseComObject(MyRibbon.app); GC.Collect(); GC.WaitForPendingFinalizers(); return; }
public static string teGetTS(string cntry, string indctr, string startDate, string endDate, [ExcelArgument(AllowReference = true)] object myArgument) { SearchEngine.fromSearch = false; bool fromTS = true; udfClassHelper("TESeries"); if (myArgument is ExcelMissing) { dataStartCell = formulaCell; newFormula = string.Format($"=TESeries( \"{cntry}\", \"{indctr}\", \"{startDate}\", \"{endDate}\")"); //Debug.WriteLine(newFormula); } else { try { dataStartCell = helperClass.ReferenceToRange((ExcelReference)myArgument); newFormula = string.Format( $"=TESeries( \"{cntry}\", \"{indctr}\", \"{startDate}\", \"{endDate}\", {dataStartCell.Address[false, false]})"); //Debug.WriteLine(newFormula); } catch (Exception) { refError = true; helperClass.getNewDict(fromTS); return("#REF!"); throw; } } formulaColumns frmlaColumnsPair = new formulaColumns(newFormula, cntry, null, formulaCell); MyRibbon.myNewDict = new Dictionary <string, formulaColumns>(); try { XlCall.Excel(XlCall.xlfVolatile, false); } catch (Exception e) { helperClass.log.Error(e.Message); helperClass.log.Trace(e.StackTrace); throw; } if (helperClass.runFormula == "RunAutomatically = 1") { if (MyRibbon.refresh != true) { helperClass.setGlobalDict(formulaCell.Address[false, false], frmlaColumnsPair); } try { var dict = new getDictionary(cntry, indctr, startDate, endDate); Dictionary <DateTime, Dictionary <string, string> > dicts = dict.getDic(); //helperClass.log.Info("Starting function data_to_excel"); var retriever = new RetrieveAndWriteTSData(dict.getColumns().ToArray(), dicts, dataStartCell, newFormula, formulaCell); var thready = new Thread(retriever.fetchData); thready.Priority = ThreadPriority.Normal; thready.IsBackground = true; thready.Start(); } catch (Exception ex) { helperClass.log.Info(ex.Message); helperClass.log.Trace(ex.StackTrace); throw; } } else { MyRibbon.sheet = MyRibbon.app.ActiveSheet; MyRibbon.myFormulasDict = (MyRibbon.myMainDict.ContainsKey(MyRibbon.sheet.Index.ToString())) ? MyRibbon.myMainDict[MyRibbon.sheet.Index.ToString()] : new Dictionary <string, formulaColumns>(); foreach (var item in MyRibbon.myFormulasDict.Keys) { if (MyRibbon.myFormulasDict[item]._formula == newFormula && item == MyRibbon.myFormulasDict[item]._caller.Address[false, false]) { return(formulaCell.Text); } } Dictionary <string, formulaColumns> myNewDict = helperClass.getNewDict(fromTS); try { var dict = new getDictionary(cntry, indctr, startDate, endDate); Dictionary <DateTime, Dictionary <string, string> > dicts = dict.getDic(); var columns = dict.getColumns(); string[] clms = columns.ToArray(); //helperClass.log.Info("Starting function data_to_excel"); var retriever = new RetrieveAndWriteTSData(clms, dicts, dataStartCell, newFormula, formulaCell); var thready = new Thread(retriever.fetchData); thready.Priority = ThreadPriority.Normal; thready.IsBackground = true; thready.Start(); helperClass.RemoveOldKey(myNewDict); } catch (Exception ex) { helperClass.log.Info(ex.Message); helperClass.log.Trace(ex.StackTrace); throw; } } customFunctionEnd("TS"); return((formulaCell.Address == dataStartCell.Address) ? "DateTime" : sharedFunctions.getAnswer(indctr)); }
public static string teGetMarkets(string mrkt, string columnsToUse, [ExcelArgument(AllowReference = true)] object firstCell) { SearchEngine.fromSearch = false; udfClassHelper("TEMarkets", mrkt); if (firstCell is ExcelMissing) { dataStartCell = formulaCell; newFormula = string.Format($"=TEMarkets( \"{mrkt}\", \"{columnsToUse}\")"); } else { try { dataStartCell = helperClass.ReferenceToRange((ExcelReference)firstCell); newFormula = string.Format($"=TEMarkets( \"{mrkt}\", \"{columnsToUse}\", {dataStartCell.Address[false, false]})"); } catch (Exception) { refError = true; helperClass.getNewDict(); return("#REF!"); throw; } } Range pass = null; formulaColumns frmlaColumnsPair = new formulaColumns(newFormula, columnsToUse, pass, formulaCell); MyRibbon.myNewDict = new Dictionary <string, formulaColumns>(); try { XlCall.Excel(XlCall.xlfVolatile, false); } catch (Exception e) { helperClass.log.Error(e.Message); helperClass.log.Trace(e.StackTrace); throw; } if (helperClass.runFormula == "RunAutomatically = 1") { if (MyRibbon.refresh != true) { helperClass.setGlobalDict(formulaCell.Address[false, false], frmlaColumnsPair); } url = host + "markets/" + mrkt + "?client=" + apiKeyFrm.apiKey + "&excel=" + apiKeyFrm.excelVersion; var jsData = new requestData(url).getJSON(); if (jsData.Count == 0) { MessageBox.Show("No data provided for selected parameters"); } else { try { helperClass.elseFunction(columnNamesHack(columnsToUse), jsData, dataStartCell, newFormula, formulaCell); } catch (Exception ex) { helperClass.log.Info(ex.Message); helperClass.log.Trace(ex.StackTrace); throw; } } } else { MyRibbon.sheet = MyRibbon.app.ActiveSheet; MyRibbon.myFormulasDict = (MyRibbon.myMainDict.ContainsKey(MyRibbon.sheet.Index.ToString())) ? MyRibbon.myMainDict[MyRibbon.sheet.Index.ToString()] : new Dictionary <string, formulaColumns>(); foreach (var item in MyRibbon.myFormulasDict.Keys) { if (MyRibbon.myFormulasDict[item]._formula == newFormula && item == MyRibbon.myFormulasDict[item]._caller.Address[false, false]) { return(formulaCell.Text); } } Dictionary <string, formulaColumns> myNewDict = helperClass.getNewDict(); url = host + "markets/" + mrkt + "?client=" + apiKeyFrm.apiKey + "&excel=" + apiKeyFrm.excelVersion; var jsData = new requestData(url).getJSON(); if (jsData.Count == 0) { MessageBox.Show("No data provided for selected parameters"); } else { try { helperClass.elseFunction(columnNamesHack(columnsToUse), jsData, dataStartCell, newFormula, formulaCell); } catch (Exception ex) { helperClass.log.Info(ex.Message); helperClass.log.Trace(ex.StackTrace); throw; } } helperClass.RemoveOldKey(myNewDict); } customFunctionEnd("Markets"); return((formulaCell.Address == dataStartCell.Address)? columnsToUse.Split(',')[0] : sharedFunctions.getAnswer(mrkt)); }
public static string teGetTS(string cntry, string indctr, string startDate, string endDate, [ExcelArgument(AllowReference = true)] object myArgument) { helperClass.log.Info("======================="); helperClass.log.Info("Starting TESeries udf"); string key = Properties.Settings.Default.userApiKey; string answer = "Updated at " + DateTime.Now.TimeOfDay.ToString("hh\\:mm\\:ss"); bool fromTS = true; try { MyRibbon.sheet = MyRibbon.app.ActiveSheet; } catch (Exception) { MyRibbon.app = (Microsoft.Office.Interop.Excel.Application)ExcelDnaUtil.Application; MyRibbon.sheet = MyRibbon.app.ActiveSheet; } ExcelReference caller = XlCall.Excel(XlCall.xlfCaller) as ExcelReference; Range caller_range = helperClass.ReferenceToRange(caller); Range formulaCell = caller_range; //Last cell used in userform Range dataStartCell; string newFormula; if (myArgument is ExcelMissing) { dataStartCell = formulaCell; newFormula = string.Format($"=TESeries( \"{cntry}\", \"{indctr}\", \"{startDate}\", \"{endDate}\")"); //Debug.WriteLine(newFormula); } else { try { dataStartCell = helperClass.ReferenceToRange((ExcelReference)myArgument); newFormula = string.Format($"=TESeries( \"{cntry}\", \"{indctr}\", \"{startDate}\", \"{endDate}\", {dataStartCell.Address[false, false]})"); //Debug.WriteLine(newFormula); } catch (Exception) { refError = true; helperClass.getNewDict(fromTS); return("#REF!"); throw; } } Range pass = null; string columns2 = ""; //formulaColumns frmlaColumnsPair = new formulaColumns(newFormula, columns2, pass, formulaCell); formulaColumns frmlaColumnsPair = new formulaColumns(newFormula, cntry, pass, formulaCell); MyRibbon.myNewDict = new Dictionary <string, formulaColumns>(); if (formulaCell.Address == dataStartCell.Address) { answer = "DateTime"; } try { XlCall.Excel(XlCall.xlfVolatile, false); } catch (Exception e) { helperClass.log.Error(e.Message); helperClass.log.Trace(e.StackTrace); throw; } if (helperClass.runFormula == "RunAutomatically = 1") { helperClass.log.Info("TESeries udf -> RunAutomatically = 1"); if (MyRibbon.refresh != true) { helperClass.setGlobalDict(formulaCell.Address[false, false], frmlaColumnsPair); } try { var dict = new getDictionary(cntry, indctr, key, startDate, endDate); //Dictionary<string, Dictionary<string, string>> dicts = dict.getDic(); Dictionary <DateTime, Dictionary <string, string> > dicts = dict.getDic(); var columns = dict.getColumns(); string[] clms = columns.ToArray(); helperClass.log.Info("Starting function data_to_excel"); var retriever = new RetrieveAndWriteTSData(clms, dicts, key, dataStartCell, newFormula, formulaCell); var thready = new Thread(retriever.fetchData); thready.Priority = ThreadPriority.Normal; thready.IsBackground = true; thready.Start(); } catch (Exception ex) { helperClass.log.Info(ex.Message); helperClass.log.Trace(ex.StackTrace); throw; } } else { helperClass.log.Info("TESeries udf -> RunAutomatically != 1"); MyRibbon.sheet = MyRibbon.app.ActiveSheet; if (MyRibbon.myMainDict.ContainsKey(MyRibbon.sheet.Index.ToString())) { MyRibbon.myFormulasDict = MyRibbon.myMainDict[MyRibbon.sheet.Index.ToString()]; } else { MyRibbon.myFormulasDict = new Dictionary <string, formulaColumns>(); } foreach (var item in MyRibbon.myFormulasDict.Keys) { if (MyRibbon.myFormulasDict[item]._formula == newFormula && item == MyRibbon.myFormulasDict[item]._caller.Address[false, false]) { return(answer); } } Dictionary <string, formulaColumns> myNewDict = helperClass.getNewDict(fromTS); try { var dict = new getDictionary(cntry, indctr, key, startDate, endDate); //Dictionary<string, Dictionary<string, string>> dicts = dict.getDic(); Dictionary <DateTime, Dictionary <string, string> > dicts = dict.getDic(); var columns = dict.getColumns(); string[] clms = columns.ToArray(); helperClass.log.Info("Starting function data_to_excel"); var retriever = new RetrieveAndWriteTSData(clms, dicts, key, dataStartCell, newFormula, formulaCell); var thready = new Thread(retriever.fetchData); thready.Priority = ThreadPriority.Normal; thready.IsBackground = true; thready.Start(); helperClass.RemoveOldKey(myNewDict); } catch (Exception ex) { helperClass.log.Info(ex.Message); helperClass.log.Trace(ex.StackTrace); throw; } } helperClass.log.Info("TS finish"); helperClass.runFormula = "RunAutomatically = 0"; helperClass.origin = true; MyRibbon.refresh = false; helperClass.log.Info("Printing current cell value and finishing TS process"); return(answer); }
public static string teGetHistorical(string cntry, string indctr, string startDate, string endDate, string columnsToUse, [ExcelArgument(AllowReference = true)] object myArgument) { helperClass.log.Info("========================="); helperClass.log.Info("Starting TEHistorical udf"); string key = Properties.Settings.Default.userApiKey; string answer = "Updated at " + DateTime.Now.TimeOfDay.ToString("hh\\:mm\\:ss"); string columns = columnsToUse; try { MyRibbon.sheet = MyRibbon.app.ActiveSheet; } catch (Exception) { MyRibbon.app = (Microsoft.Office.Interop.Excel.Application)ExcelDnaUtil.Application; MyRibbon.sheet = MyRibbon.app.ActiveSheet; } ExcelReference caller = XlCall.Excel(XlCall.xlfCaller) as ExcelReference; Range caller_range = helperClass.ReferenceToRange(caller); Range formulaCell = caller_range; Range dataStartCell; helperClass.fromHistorical = true; string newFormula; if (myArgument is ExcelMissing) { dataStartCell = formulaCell; newFormula = string.Format($"=TEHistorical( \"{cntry}\", \"{indctr}\", \"{startDate}\", \"{endDate}\", \"{columns}\")"); } else { try { dataStartCell = helperClass.ReferenceToRange((ExcelReference)myArgument); newFormula = string.Format($"=TEHistorical( \"{cntry}\", \"{indctr}\", \"{startDate}\", \"{endDate}\", \"{columns}\", {dataStartCell.Address[false, false]})"); } catch (Exception) { refError = true; helperClass.getNewDict(); return("#REF!"); throw; } } Range pass = null; formulaColumns frmlaColumnsPair = new formulaColumns(newFormula, columns, pass, formulaCell); MyRibbon.myNewDict = new Dictionary <string, formulaColumns>(); try { XlCall.Excel(XlCall.xlfVolatile, false); } catch (Exception e) { helperClass.log.Error(e.Message); helperClass.log.Trace(e.StackTrace); throw; } if (formulaCell.Address == dataStartCell.Address) { answer = columns.Split(',')[0]; } if (helperClass.runFormula == "RunAutomatically = 1") { helperClass.log.Info("TEHistorical udf -> RunAutomatically = 1"); if (MyRibbon.refresh != true) { helperClass.setGlobalDict(formulaCell.Address[false, false], frmlaColumnsPair); } JArray jsData = helperClass.SOmeName(cntry, indctr, key, "Hist", startDate, endDate); if (jsData.Count == 0) { MessageBox.Show("No data provided for selected parameters"); } else { try { helperClass.elseFunction(columns, jsData, key, dataStartCell, newFormula, formulaCell); } catch (Exception ex) { helperClass.log.Info(ex.Message); helperClass.log.Trace(ex.StackTrace); throw; } } } else { helperClass.log.Info("TEHistorical udf -> RunAutomatically != 1"); MyRibbon.sheet = MyRibbon.app.ActiveSheet; if (MyRibbon.myMainDict.ContainsKey(MyRibbon.sheet.Index.ToString())) { MyRibbon.myFormulasDict = MyRibbon.myMainDict[MyRibbon.sheet.Index.ToString()]; } else { MyRibbon.myFormulasDict = new Dictionary <string, formulaColumns>(); } foreach (var item in MyRibbon.myFormulasDict.Keys) { if (MyRibbon.myFormulasDict[item]._formula == newFormula && caller_range.Address[false, false] == item) { return(answer); } } Dictionary <string, formulaColumns> myNewDict = helperClass.getNewDict(); JArray jsData = helperClass.SOmeName(cntry, indctr, key, "Hist", startDate, endDate); if (jsData.Count == 0) { MessageBox.Show("No data provided for selected parameters"); } else { try { helperClass.elseFunction(columns, jsData, key, dataStartCell, newFormula, formulaCell); } catch (Exception ex) { helperClass.log.Info(ex.Message); helperClass.log.Trace(ex.StackTrace); throw; } } helperClass.RemoveOldKey(myNewDict); } helperClass.runFormula = "RunAutomatically = 0"; helperClass.origin = true; MyRibbon.refresh = false; helperClass.log.Info("Printing current cell value and finishing Historical process"); return(answer); }
public static string teGetMarkets(string mrkt, string columnsToUse, [ExcelArgument(AllowReference = true)] object firstCell) { helperClass.log.Info("======================="); helperClass.log.Info("Starting TEMarkets udf"); string key = Properties.Settings.Default.userApiKey; // Convert column names to JArray names fullNames = new Dictionary <string, string>(); if (mrkt == "bonds") { for (int i = 0; i < helperClass.bondsNames.Length; i++) { fullNames.Add(helperClass.bondsNames[i], helperClass.bondsNamesFull[i]); } } else { for (int i = 0; i < helperClass.marketsNames.Length; i++) { fullNames.Add(helperClass.marketsNames[i], helperClass.marketsNamesFull[i]); } } string answer = "Updated at " + DateTime.Now.TimeOfDay.ToString("hh\\:mm\\:ss"); string columns = columnsToUse; try { MyRibbon.sheet = MyRibbon.app.ActiveSheet; } catch (Exception) { MyRibbon.app = (Microsoft.Office.Interop.Excel.Application)ExcelDnaUtil.Application; MyRibbon.sheet = MyRibbon.app.ActiveSheet; } ExcelReference caller = XlCall.Excel(XlCall.xlfCaller) as ExcelReference; Range caller_range = helperClass.ReferenceToRange(caller); Range formulaCell = caller_range; //Last cell used in userform Range dataStartCell; string newFormula; if (firstCell is ExcelMissing) { dataStartCell = formulaCell; newFormula = string.Format($"=TEMarkets( \"{mrkt}\", \"{columns}\")"); } else { try { dataStartCell = helperClass.ReferenceToRange((ExcelReference)firstCell); newFormula = string.Format($"=TEMarkets( \"{mrkt}\", \"{columns}\", {dataStartCell.Address[false, false]})"); } catch (Exception) { refError = true; helperClass.getNewDict(); return("#REF!"); throw; } } Range pass = null; formulaColumns frmlaColumnsPair = new formulaColumns(newFormula, columns, pass, formulaCell); MyRibbon.myNewDict = new Dictionary <string, formulaColumns>(); try { XlCall.Excel(XlCall.xlfVolatile, false); } catch (Exception e) { helperClass.log.Error(e.Message); helperClass.log.Trace(e.StackTrace); throw; } if (formulaCell.Address == dataStartCell.Address) { answer = columns.Split(',')[0]; } if (helperClass.runFormula == "RunAutomatically = 1") { helperClass.log.Info("TEMarkets udf -> RunAutomatically = 1"); if (MyRibbon.refresh != true) { helperClass.setGlobalDict(formulaCell.Address[false, false], frmlaColumnsPair); } url = host + "markets/" + mrkt + "?client=" + key + "&excel=" + helperClass.Determine_OfficeVersion(); var jsnData = new requestData(url); var jsData = jsnData.getJSON(); if (jsData.Count == 0) { MessageBox.Show("No data provided for selected parameters"); } else { List <string> columnsFull = new List <string>(); foreach (var item in columns.Split(',')) { if (fullNames.ContainsKey(item)) { columnsFull.Add(fullNames[item]); } } columns = String.Join(",", columnsFull); try { helperClass.elseFunction(columns, jsData, key, dataStartCell, newFormula, formulaCell); } catch (Exception ex) { helperClass.log.Info(ex.Message); helperClass.log.Trace(ex.StackTrace); throw; } } } else { helperClass.log.Info("TEMarkets udf -> RunAutomatically != 1"); MyRibbon.sheet = MyRibbon.app.ActiveSheet; if (MyRibbon.myMainDict.ContainsKey(MyRibbon.sheet.Index.ToString())) { MyRibbon.myFormulasDict = MyRibbon.myMainDict[MyRibbon.sheet.Index.ToString()]; } else { MyRibbon.myFormulasDict = new Dictionary <string, formulaColumns>(); } foreach (var item in MyRibbon.myFormulasDict.Keys) { if (MyRibbon.myFormulasDict[item]._formula == newFormula && item == MyRibbon.myFormulasDict[item]._caller.Address[false, false]) { return(answer); } } Dictionary <string, formulaColumns> myNewDict = helperClass.getNewDict(); url = host + "markets/" + mrkt + "?client=" + key + "&excel=" + helperClass.Determine_OfficeVersion(); var jsnData = new requestData(url); var jsData = jsnData.getJSON(); if (jsData.Count == 0) { MessageBox.Show("No data provided for selected parameters"); } else { List <string> columnsFull = new List <string>(); foreach (var item in columns.Split(',')) { if (fullNames.ContainsKey(item)) { columnsFull.Add(fullNames[item]); } } columns = String.Join(",", columnsFull); try { helperClass.elseFunction(columns, jsData, key, dataStartCell, newFormula, formulaCell); } catch (Exception ex) { helperClass.log.Info(ex.Message); helperClass.log.Trace(ex.StackTrace); throw; } } helperClass.RemoveOldKey(myNewDict); } helperClass.runFormula = "RunAutomatically = 0"; helperClass.origin = true; helperClass.log.Info("Printing current cell value and finishing Markets process"); MyRibbon.refresh = false; return(answer); }