Smrf.NodeXL.ExcelTemplate.TableColumnAdder.AddColumnPair C# (CSharp) Method

AddColumnPair() public method

public AddColumnPair ( Microsoft workbook, String worksheetName, String tableName, String column1NameBase, Double column1WidthChars, String column2NameBase, Double column2WidthChars ) : void
workbook Microsoft
worksheetName String
tableName String
column1NameBase String
column1WidthChars Double
column2NameBase String
column2WidthChars Double
return void
    AddColumnPair
    (
        Microsoft.Office.Interop.Excel.Workbook workbook,
        String worksheetName,
        String tableName,
        String column1NameBase,
        Double column1WidthChars,
        String column2NameBase,
        Double column2WidthChars
    )
    {
        Debug.Assert(workbook != null);
        Debug.Assert( !String.IsNullOrEmpty(worksheetName) );
        Debug.Assert( !String.IsNullOrEmpty(tableName) );
        Debug.Assert( !String.IsNullOrEmpty(column1NameBase) );

        Debug.Assert(column1WidthChars == ExcelTableUtil.AutoColumnWidth ||
            column1WidthChars >= 0);

        Debug.Assert( !String.IsNullOrEmpty(column2NameBase) );

        Debug.Assert(column2WidthChars == ExcelTableUtil.AutoColumnWidth ||
            column2WidthChars >= 0);

        AssertValid();

        ListObject oTable;

        if (!ExcelTableUtil.TryGetTable(workbook, worksheetName, tableName,
            out oTable) )
        {
            throw new WorkbookFormatException(String.Format(

                "To use this feature, there must be a worksheet named \"{0}\""
                + " that contains a table named \"{1}\"."
                + "\r\n\r\n"
                + "{2}"
                ,
                worksheetName,
                tableName,
                ErrorUtil.GetTemplateMessage()
                ) );
        }

        Int32 iMaximumAppendedNumber = Math.Max(
            GetMaximumAppendedNumber(oTable, column1NameBase),
            GetMaximumAppendedNumber(oTable, column2NameBase)
            );

        if (iMaximumAppendedNumber != 0)
        {
            String sStringToAppend =
                " " + (iMaximumAppendedNumber + 1).ToString();

            column1NameBase += sStringToAppend;
            column2NameBase += sStringToAppend;
        }

        ListColumn oListColumn1, oListColumn2;

        if (
            !ExcelTableUtil.TryAddTableColumn(oTable, column1NameBase,
                column1WidthChars, null, out oListColumn1)
            ||
            !ExcelTableUtil.TryAddTableColumn(oTable, column2NameBase,
                column2WidthChars, null, out oListColumn2)
            )
        {
            FormUtil.ShowWarning("The columns weren't added.");
        }

        ExcelUtil.ActivateWorksheet(oTable);
    }

Usage Example

    btnCustomizeVertexMenu_Click
    (
        object sender,
        EventArgs e
    )
    {
        AssertValid();

        const String Message =
            "Use this to add custom menu items to the menu that appears when"
            + " you right-click a vertex in the graph pane."
            + "\r\n\r\n"
            + "Clicking \"Yes\" below will add a pair of columns to the"
            + " Vertices worksheet -- one for menu item text and another for"
            + " the action to take when the menu item is selected."
            + "\r\n\r\n"
            + "For example, if you add the column pair and enter \"Send Mail"
            + " To\" for a vertex's menu item text and \"mailto:[email protected]\""
            + " for the action, then right-clicking the vertex in the NodeXL"
            + " graph and selecting \"Send Mail To\" from the right-click menu"
            + " will open a new email message addressed to [email protected]."
            + "\r\n\r\n"
            + "If you want to open a Web page when the menu item is selected,"
            + " enter an URL for the action."
            + "\r\n\r\n"
            + "If you want to add more than one custom menu item to a vertex's"
            + " right-click menu, run this again to add another pair of"
            + " columns."
            + "\r\n\r\n"
            + "Do you want to add a pair of columns to the Vertices worksheet?"
            ;

        if (MessageBox.Show(Message, this.ApplicationName,
                MessageBoxButtons.YesNo, MessageBoxIcon.Information) !=
                DialogResult.Yes)
        {
            return;
        }

        // Create and use the object that adds the columns to the vertex
        // table.

        TableColumnAdder oTableColumnAdder = new TableColumnAdder();

        this.UseWaitCursor = true;

        try
        {
            oTableColumnAdder.AddColumnPair(m_oWorkbook,
                WorksheetNames.Vertices, TableNames.Vertices,
                VertexTableColumnNames.CustomMenuItemTextBase,
                CustomMenuItemTextColumnWidth,
                VertexTableColumnNames.CustomMenuItemActionBase,
                CustomMenuItemActionColumnWidth
                );

            this.UseWaitCursor = false;
        }
        catch (Exception oException)
        {
            this.UseWaitCursor = false;

            ErrorUtil.OnException(oException);
        }
    }
All Usage Examples Of Smrf.NodeXL.ExcelTemplate.TableColumnAdder::AddColumnPair