PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/ExcelPoSH/ExcelPoSH/ExcelPSHost.cs

#
C# | 436 lines | 353 code | 66 blank | 17 comment | 10 complexity | cda4d37b4e184c95c3e841cf9b970f2a MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.Globalization;
  5. using System.Management.Automation;
  6. using System.Management.Automation.Host;
  7. using Excel = Microsoft.Office.Interop.Excel;
  8. using System.Reflection;
  9. namespace ExcelPoSH
  10. {
  11. // PShost derived class
  12. // needed to add it for custom Write-Host support
  13. public sealed class ExcelPSHost: PSHost
  14. {
  15. private Guid mInstanceId;
  16. private Version mVersion;
  17. private string mName;
  18. private CultureInfo mCurrentCulture;
  19. private CultureInfo mCurrentUICulture;
  20. private const string mPrivateData = "Private data.";
  21. private ExcelPSHostUserInterface mUserInterface;
  22. private Excel.Application app;
  23. public ExcelPSHost(Excel.Application app): base()
  24. {
  25. mInstanceId = Guid.NewGuid();
  26. mVersion = new Version(0, 1, 0, 0);
  27. mName = "Excel.Host";
  28. mCurrentCulture = new CultureInfo("en-US");
  29. mCurrentUICulture = new CultureInfo("en-US");
  30. mUserInterface = new ExcelPSHostUserInterface(app);
  31. this.app = app;
  32. }
  33. public override System.Globalization.CultureInfo CurrentCulture
  34. {
  35. get { return mCurrentCulture; }
  36. }
  37. public override System.Globalization.CultureInfo CurrentUICulture
  38. {
  39. get { return mCurrentUICulture; }
  40. }
  41. public override void EnterNestedPrompt()
  42. {
  43. throw new NotImplementedException();
  44. }
  45. public override void ExitNestedPrompt()
  46. {
  47. throw new NotImplementedException();
  48. }
  49. public override Guid InstanceId
  50. {
  51. get { return mInstanceId; }
  52. }
  53. public override string Name
  54. {
  55. get { return mName; }
  56. }
  57. public override void NotifyBeginApplication()
  58. {
  59. throw new NotImplementedException();
  60. }
  61. public override void NotifyEndApplication()
  62. {
  63. throw new NotImplementedException();
  64. }
  65. public override PSObject PrivateData
  66. {
  67. get
  68. {
  69. return PSObject.AsPSObject(mPrivateData);
  70. }
  71. }
  72. public override void SetShouldExit(int exitCode)
  73. {
  74. throw new NotImplementedException();
  75. }
  76. public override PSHostUserInterface UI
  77. {
  78. get { return mUserInterface; }
  79. }
  80. public override Version Version
  81. {
  82. get { return mVersion; }
  83. }
  84. }
  85. // PSHostUserInterface derived class
  86. // contains custom UI methods
  87. public sealed class ExcelPSHostUserInterface : PSHostUserInterface
  88. {
  89. // Write-Host will write to Excel.Application.ActiveCell
  90. private Excel.Application app;
  91. private ExcelPSHostRawUserInterface mRawUI;
  92. public ExcelPSHostUserInterface(Excel.Application app)
  93. {
  94. this.app = app;
  95. mRawUI = new ExcelPSHostRawUserInterface();
  96. }
  97. public override Dictionary<string, PSObject> Prompt(
  98. string caption, string message,
  99. Collection<FieldDescription> descriptions)
  100. {
  101. if (app != null)
  102. {
  103. Dictionary<string, PSObject> dict =
  104. new Dictionary<string, PSObject>();
  105. foreach (FieldDescription fieldDescription in descriptions)
  106. {
  107. Object retVal = app.InputBox(
  108. fieldDescription.Name, "Parameter missing",
  109. Missing.Value, Missing.Value, Missing.Value,
  110. Missing.Value, Missing.Value, 2);
  111. // If user clicks Cancel InputBox returns bool
  112. // and it is always false
  113. // else it returns the entered value
  114. if (retVal is bool)
  115. {
  116. dict.Add(fieldDescription.Name, null);
  117. }
  118. else
  119. {
  120. dict.Add(fieldDescription.Name,
  121. new PSObject(retVal.ToString()));
  122. }
  123. }
  124. return dict;
  125. }
  126. else
  127. {
  128. return null;
  129. }
  130. }
  131. public override int PromptForChoice(
  132. string caption, string message,
  133. Collection<ChoiceDescription> choices,
  134. int defaultChoice)
  135. {
  136. throw new NotImplementedException();
  137. }
  138. public override PSCredential PromptForCredential(
  139. string caption, string message, string userName,
  140. string targetName, PSCredentialTypes allowedCredentialTypes,
  141. PSCredentialUIOptions options)
  142. {
  143. throw new NotImplementedException();
  144. }
  145. public override PSCredential PromptForCredential(
  146. string caption, string message,
  147. string userName, string targetName)
  148. {
  149. throw new NotImplementedException();
  150. }
  151. public override PSHostRawUserInterface RawUI
  152. {
  153. get { return mRawUI; }
  154. }
  155. public override string ReadLine()
  156. {
  157. if (app != null)
  158. {
  159. object retVal = app.InputBox(
  160. "Enter a line of text", "Read Line",
  161. Missing.Value, Missing.Value, Missing.Value,
  162. Missing.Value, Missing.Value, 2);
  163. // If user clicks Cancel InputBox returns bool
  164. // and it is always false
  165. // else it returns the entered string
  166. if (retVal is bool)
  167. {
  168. return null;
  169. }
  170. else
  171. {
  172. return retVal.ToString();
  173. }
  174. }
  175. else
  176. {
  177. return null;
  178. }
  179. }
  180. public override System.Security.SecureString ReadLineAsSecureString()
  181. {
  182. System.Security.SecureString retVal =
  183. new System.Security.SecureString();
  184. foreach (char c in ReadLine())
  185. {
  186. retVal.AppendChar(c);
  187. }
  188. return retVal;
  189. }
  190. // use the colorless write method
  191. public override void Write(
  192. ConsoleColor foregroundColor,
  193. ConsoleColor backgroundColor, string value)
  194. {
  195. Write(value);
  196. }
  197. // Write the string to console output textbox
  198. public override void Write(string value)
  199. {
  200. if (value != "\n")
  201. {
  202. Globals.ThisAddIn.console.SetText(value, true);
  203. }
  204. }
  205. public override void WriteDebugLine(string message)
  206. {
  207. throw new NotImplementedException();
  208. }
  209. public override void WriteErrorLine(string value)
  210. {
  211. throw new NotImplementedException();
  212. }
  213. // Write-Host uses this method adding '\n' to value
  214. // so call Write(value)
  215. public override void WriteLine(string value)
  216. {
  217. if (app.ActiveCell != null)
  218. {
  219. Write(value);
  220. }
  221. }
  222. public override void WriteProgress(
  223. long sourceId, ProgressRecord record)
  224. {
  225. throw new NotImplementedException();
  226. }
  227. public override void WriteVerboseLine(string message)
  228. {
  229. throw new NotImplementedException();
  230. }
  231. public override void WriteWarningLine(string message)
  232. {
  233. throw new NotImplementedException();
  234. }
  235. }
  236. // Write-Host didn't work without adding this class
  237. // and setting BufferSize, BackgroundColor and ForegroundColor
  238. public sealed class ExcelPSHostRawUserInterface : PSHostRawUserInterface
  239. {
  240. private Size mBufferSize = new Size(80, 80);
  241. public override ConsoleColor BackgroundColor
  242. {
  243. get
  244. {
  245. return ConsoleColor.White;
  246. }
  247. set
  248. {
  249. throw new NotImplementedException();
  250. }
  251. }
  252. public override Size BufferSize
  253. {
  254. get
  255. {
  256. return mBufferSize;
  257. }
  258. set
  259. {
  260. mBufferSize = value;
  261. }
  262. }
  263. public override Coordinates CursorPosition
  264. {
  265. get
  266. {
  267. throw new NotImplementedException();
  268. }
  269. set
  270. {
  271. throw new NotImplementedException();
  272. }
  273. }
  274. public override int CursorSize
  275. {
  276. get
  277. {
  278. throw new NotImplementedException();
  279. }
  280. set
  281. {
  282. throw new NotImplementedException();
  283. }
  284. }
  285. public override void FlushInputBuffer()
  286. {
  287. throw new NotImplementedException();
  288. }
  289. public override ConsoleColor ForegroundColor
  290. {
  291. get
  292. {
  293. return ConsoleColor.Black;
  294. }
  295. set
  296. {
  297. throw new NotImplementedException();
  298. }
  299. }
  300. public override BufferCell[,] GetBufferContents(Rectangle rectangle)
  301. {
  302. throw new NotImplementedException();
  303. }
  304. public override bool KeyAvailable
  305. {
  306. get { throw new NotImplementedException(); }
  307. }
  308. public override Size MaxPhysicalWindowSize
  309. {
  310. get { throw new NotImplementedException(); }
  311. }
  312. public override Size MaxWindowSize
  313. {
  314. get { throw new NotImplementedException(); }
  315. }
  316. public override KeyInfo ReadKey(ReadKeyOptions options)
  317. {
  318. throw new NotImplementedException();
  319. }
  320. public override void ScrollBufferContents(
  321. Rectangle source, Coordinates destination,
  322. Rectangle clip, BufferCell fill)
  323. {
  324. throw new NotImplementedException();
  325. }
  326. public override void SetBufferContents(
  327. Rectangle rectangle, BufferCell fill)
  328. {
  329. throw new NotImplementedException();
  330. }
  331. public override void SetBufferContents(
  332. Coordinates origin, BufferCell[,] contents)
  333. {
  334. throw new NotImplementedException();
  335. }
  336. public override Coordinates WindowPosition
  337. {
  338. get
  339. {
  340. throw new NotImplementedException();
  341. }
  342. set
  343. {
  344. throw new NotImplementedException();
  345. }
  346. }
  347. public override Size WindowSize
  348. {
  349. get
  350. {
  351. throw new NotImplementedException();
  352. }
  353. set
  354. {
  355. throw new NotImplementedException();
  356. }
  357. }
  358. public override string WindowTitle
  359. {
  360. get
  361. {
  362. throw new NotImplementedException();
  363. }
  364. set
  365. {
  366. throw new NotImplementedException();
  367. }
  368. }
  369. }
  370. }