Runs multiple tasks on one workbook or a folder full of workbooks.
Call AutomateOneWorkbook or to run a specified set of tasks on one NodeXL workbook, or AutomateFolder to run them on every unopened NodeXL workbook in a folder.

All methods are static.

Inheritance: Object
        AggregateGraphMetricsInternal
        (
            AggregateGraphMetricsAsyncArgs oAggregateGraphMetricsAsyncArgs,
            BackgroundWorker oBackgroundWorker,
            DoWorkEventArgs oDoWorkEventArgs
        )
        {
            Debug.Assert(oAggregateGraphMetricsAsyncArgs != null);
            Debug.Assert(oBackgroundWorker != null);
            Debug.Assert(oDoWorkEventArgs != null);
            AssertValid();

            List <OverallMetricsInfo> oOverallMetricsInfos =
                new List <OverallMetricsInfo>();

            OverallMetricsReader oOverallMetricsReader =
                new OverallMetricsReader();

            foreach (String sFilePath in Directory.GetFiles(
                         oAggregateGraphMetricsAsyncArgs.SourceFolderPath, "*.xlsx"))
            {
                if (oBackgroundWorker.CancellationPending)
                {
                    oDoWorkEventArgs.Cancel = true;
                    return;
                }

                try
                {
                    if (!NodeXLWorkbookUtil.FileIsNodeXLWorkbook(sFilePath))
                    {
                        continue;
                    }
                }
                catch (IOException)
                {
                    // Skip any workbooks that are already open, or that have any
                    // other problems that prevent them from being opened.

                    continue;
                }

                oBackgroundWorker.ReportProgress(0,
                                                 String.Format(
                                                     "Reading \"{0}\"."
                                                     ,
                                                     Path.GetFileName(sFilePath)
                                                     ));

                OverallMetricsInfo oOverallMetricsInfo;

                for (Int32 iAttempt = 0; iAttempt < 2; iAttempt++)
                {
                    // Have overall metrics already been calculated for the
                    // workbook?

                    if (TryGetGraphMetricsForOneNodeXLWorkbook(sFilePath,
                                                               out oOverallMetricsInfo))
                    {
                        // Yes.

                        oOverallMetricsInfos.Add(oOverallMetricsInfo);
                        break;
                    }

                    if (iAttempt == 0)
                    {
                        // No.  Calculate them.

                        TaskAutomator.AutomateOneWorkbookIndirect(sFilePath,
                                                                  oAggregateGraphMetricsAsyncArgs.WorkbookSettings);
                    }
                }
            }

            if (oOverallMetricsInfos.Count > 0)
            {
                WriteOverallMetricsToNewWorkbook(
                    oAggregateGraphMetricsAsyncArgs.Workbook.Application,
                    oOverallMetricsInfos);
            }

            oBackgroundWorker.ReportProgress(0,
                                             String.Format(
                                                 "Done.  NodeXL workbooks aggregated: {0}."
                                                 ,
                                                 oOverallMetricsInfos.Count
                                                 ));
        }
Exemple #2
0
        btnOK_Click
        (
            object sender,
            System.EventArgs e
        )
        {
            if (!DoDataExchange(true))
            {
                return;
            }

            m_oAutomateTasksUserSettings.Save();

            try
            {
                if (m_eMode == DialogMode.EditOnly)
                {
                    // (Just close the dialog.)
                }
                else if (m_oAutomateTasksUserSettings.AutomateThisWorkbookOnly)
                {
                    Debug.Assert(m_oNodeXLControl != null);

                    TaskAutomator.AutomateOneWorkbook(m_oThisWorkbook,
                                                      m_oNodeXLControl, m_oAutomateTasksUserSettings.TasksToRun,
                                                      m_oAutomateTasksUserSettings.FolderToSaveWorkbookTo);
                }
                else
                {
                    // The user settings for this workbook will be used for and
                    // stored in each workbook in the specified folder.

                    CommandDispatcher.SendCommand(this,
                                                  new RunNoParamCommandEventArgs(
                                                      NoParamCommand.SaveUserSettings));

                    String sWorkbookSettings = (new PerWorkbookSettings(
                                                    m_oThisWorkbook.InnerObject)).WorkbookSettings;

                    TaskAutomator.AutomateFolder(
                        m_oAutomateTasksUserSettings.FolderToAutomate,
                        sWorkbookSettings);
                }
            }
            catch (UnauthorizedAccessException oUnauthorizedAccessException)
            {
                // This occurs when a workbook is read-only.

                this.ShowWarning(
                    "A problem occurred while running tasks.  Details:"
                    + "\r\n\r\n"
                    + oUnauthorizedAccessException.Message
                    );

                return;
            }
            catch (Exception oException)
            {
                ErrorUtil.OnException(oException);
                return;
            }

            this.DialogResult = DialogResult.OK;
            this.Close();
        }