예제 #1
0
        public void readGroupUsersMembershipIntoWorksheet(Excel.Application xls, string strDoWhat)
        {
            #region [Declare and instantiate variables for process]
            myData = myData.LoadMyData();               // read data from settings file

            bool boolDisplayInitialMessage  = myData.ProduceInitialMessageBox;
            bool boolDisplayCompleteMessage = myData.ProduceCompleteMessageBox;
            bool booltimeTaken     = myData.DisplayTimeTaken;
            bool boolTurnOffScreen = myData.TurnOffScreenValidation;
            bool boolTestCode      = myData.TestCode;

            #endregion

            #region [Declare and instantiate variables for worksheet/book]
            // get worksheet name
            Excel.Workbook  Wkb = xls.ActiveWorkbook;
            Excel.Worksheet Wks;   // get current sheet

            Wks = Wkb.ActiveSheet;
            string strMessage   = "Get Membership of Active Directory Group: ";
            string strGroupName = Wks.Name;

            if (strDoWhat == "ActiveSheet")
            {
                strGroupName = Wks.Name;
                strMessage   = strMessage + strGroupName + LF + "into this worksheet";
            }
            else
            {
                Excel.Range xlCell = xls.ActiveCell;
                strGroupName = xlCell.Value.ToString();
                strMessage   = strMessage + strGroupName + LF + "into new worksheet";
            }

            #endregion

            #region [Ask to display a Message?]
            DialogResult dlgResult = DialogResult.Yes;


            if (boolDisplayInitialMessage)
            {
                if (booltimeTaken)
                {
                    strMessage = strMessage + LF + " and display the time taken";
                }

                dlgResult = MessageBox.Show(strMessage + "?", "Active Directory", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
            }

            #endregion


            #region [Start of work]
            if (dlgResult == DialogResult.Yes)
            {
                if (boolTurnOffScreen)
                {
                    CommonExcelClasses.turnAppSettings("Off", xls, myData.TestCode);
                }

                DateTime dteStart = DateTime.Now;


                if (strDoWhat == "SheetName")
                {
                    CommonExcelClasses.zapWorksheet(Wks);
                }
                else
                {
                    int i = 1;

                    string strSheetName = strGroupName;
                    while (CommonExcelClasses.WorksheetExist(Wkb, strSheetName))
                    {
                        // for (int i = 1; i <= 10; i++)

                        strSheetName = strGroupName + "-0" + i.ToString();
                        i++;
                    }
                    if (!CommonExcelClasses.WorksheetExist(Wkb, strSheetName))
                    {
                        // do a loop here checking for a free or unused name
                        Wks      = Wkb.Worksheets.Add(Type.Missing, Wkb.Worksheets[Wkb.Worksheets.Count], 1, XlSheetType.xlWorksheet);
                        Wks.Name = strSheetName;
                        Wks.Select(true);
                    }
                }


                getGroupUserMembership(Wks, strGroupName);

                writeHeaders(Wks, "ADUsers", false);
                CommonExcelClasses.sortSheet(Wks, 2);

                if (boolTurnOffScreen)
                {
                    CommonExcelClasses.turnAppSettings("On", xls, myData.TestCode);
                }


                #region [Display Complete Message]
                if (boolDisplayCompleteMessage)
                {
                    strMessage = "";
                    strMessage = strMessage + "Complete ...";

                    if (booltimeTaken)
                    {
                        DateTime dteEnd       = DateTime.Now;
                        int      milliSeconds = (int)((TimeSpan)(dteEnd - dteStart)).TotalMilliseconds;

                        strMessage = strMessage + "that took {TotalMilliseconds} " + milliSeconds;
                    }

                    CommonExcelClasses.MsgBox(strMessage);          // localisation?
                }
                #endregion
            }
            #endregion
        }