55using System . Windows ;
66using System . Diagnostics ;
77using AutoUpdaterDotNET ;
8+ using System . Windows . Input ;
89
910namespace AnalyzeInExcel
1011{
@@ -26,11 +27,33 @@ public void CheckUpdates(bool synchronous = true)
2627 AutoUpdater . Start ( $@ "https://cdn.sqlbi.com/updates/AnalyzeInExcelAutoUpdater.xml?random={ new Random ( ) . Next ( ) } ") ;
2728 }
2829
30+ /// <summary>
31+ /// THe user requested an options window instead of running the default action
32+ /// </summary>
33+ public bool OptionsRequested { get ; private set ; }
34+
35+ /// <summary>
36+ /// Check conditions to open Options window instead of directly opening Excel
37+ /// </summary>
38+ protected void InitializeOptionRequested ( )
39+ {
40+ // Pressing CTRL (left or right) when the external tool is launched activates the option window
41+ OptionsRequested = System . Windows . Input . Keyboard . IsKeyDown ( Key . LeftCtrl ) || System . Windows . Input . Keyboard . IsKeyDown ( Key . LeftCtrl ) ;
42+ }
43+
44+ public MessageBoxResult ShowMessage ( string message )
45+ {
46+ return MessageBox . Show ( message , "Analyze in Excel for Power BI Desktop" ) ;
47+ }
48+
2949 protected override void OnStartup ( StartupEventArgs e )
3050 {
51+ // Store request to access options
52+ InitializeOptionRequested ( ) ;
53+
3154 // Read configuration
3255 var result = Parser . Default . ParseArguments < Options > ( e . Args )
33- . WithNotParsed ( errors => MessageBox . Show ( "Invalid configuration, check the pbitool.json file\n " + String . Join ( ";" , from err in errors select err . ToString ( ) ) ) )
56+ . WithNotParsed ( errors => ShowMessage ( "Invalid configuration, check the pbitool.json file\n " + String . Join ( ";" , from err in errors select err . ToString ( ) ) ) )
3457 . WithParsed ( options => AppOptions = options ) ;
3558
3659 Microsoft . ApplicationInsights . Extensibility . TelemetryConfiguration configuration = Microsoft . ApplicationInsights . Extensibility . TelemetryConfiguration . CreateDefault ( ) ;
@@ -40,23 +63,49 @@ protected override void OnStartup(StartupEventArgs e)
4063
4164 string serverName = ( ( App ) Application . Current ) . AppOptions ? . Server ;
4265 string databaseName = ( ( App ) Application . Current ) . AppOptions ? . Database ;
43- string cubeName = "Model" ; // TODO get this from the model?
66+ string cubeName = ModelHelper . GetModelName ( serverName , databaseName ) ;
4467 if ( serverName != null && databaseName != null )
4568 {
4669 try
4770 {
48- // Create ODC file
49- OdcHelper . CreateOdcFile ( serverName , databaseName , cubeName ) ;
50- var fileName = OdcHelper . OdcFilePath ( ) ;
71+ if ( serverName . StartsWith ( "XXXpbiazure" ) )
72+ {
73+ ShowMessage ( "Power BI is connected to an external dataset on Power BI. You must connect Excel to the external dataset." ) ;
74+ tc . TrackEvent ( "External Power BI Dataset" ) ;
75+ }
76+ else if ( string . IsNullOrEmpty ( cubeName ) )
77+ {
78+ ShowMessage ( "Power BI has an empty model or it is connected to an external dataset. You must connect Excel to the external dataset." ) ;
79+ tc . TrackEvent ( "Model not available" ) ;
80+ }
81+ else if ( ExcelHelper . IsExcelAvailable ( ) )
82+ {
83+ // TODO: Manage options requested
84+ if ( OptionsRequested )
85+ {
86+ // TODO request action / configuration to users
87+ }
88+
89+ // Create ODC file
90+ OdcHelper . CreateOdcFile ( serverName , databaseName , cubeName ) ;
91+ var fileName = OdcHelper . OdcFilePath ( ) ;
5192
52- // Open ODC file
53- var p = new Process ( ) ;
54- p . StartInfo = new ProcessStartInfo ( fileName )
93+ // Open ODC file
94+ var p = new Process
95+ {
96+ StartInfo = new ProcessStartInfo ( fileName )
97+ {
98+ UseShellExecute = true
99+ }
100+ } ;
101+ tc . TrackEvent ( "Run Excel" ) ;
102+ p . Start ( ) ;
103+ }
104+ else
55105 {
56- UseShellExecute = true
57- } ;
58- tc . TrackEvent ( "Run Excel" ) ;
59- p . Start ( ) ;
106+ ShowMessage ( "Excel is not available. Please check whether Excel is correctly installed." ) ;
107+ tc . TrackEvent ( "Excel not available" ) ;
108+ }
60109 tc . Flush ( ) ;
61110 // Check updates synchronously when Excel starts, no wait for Excel
62111 CheckUpdates ( true ) ;
@@ -66,7 +115,7 @@ protected override void OnStartup(StartupEventArgs e)
66115 {
67116 tc . TrackException ( ex ) ;
68117 tc . Flush ( ) ;
69- MessageBox . Show ( "Error launching Excel: " + ex . Message ) ;
118+ ShowMessage ( "Error launching Excel: " + ex . Message ) ;
70119 }
71120 }
72121 else
0 commit comments