Search code examples
c#.netminitab

Minitab Automation and Skipping Columns


My main code is below:

Mtb.Application MtbApp = new Mtb.Application();
MtbApp.UserInterface.Visible = true;
MtbApp.UserInterface.DisplayAlerts = false;

Mtb.Project MtbProj = MtbApp.ActiveProject;

Mtb.Columns MtbColumns;
Mtb.Column MtbColumn1;

Double[] data1;
Hashtable htSingleColumn;
List<double> listSingleColumn;

int i = 1 ;

foreach (DictionaryEntry de in htDataTable)
{
    htSingleColumn = (Hashtable)de.Value;

    listSingleColumn = (List<double>)htSingleColumn["listSingleData"];

    data1 = listSingleColumn.ToArray();

    MtbColumns = MtbProj.ActiveWorksheet.Columns;
    MtbColumn1 = MtbColumns.Add(null, null, i);

    MtbColumn1.SetData(data1);

    // strLowlim and strUpplim have no influence on this issue here 
    strCommand = "Capa C" + i+" 1;" + ((strLowlim == "NA") ? "" : ("  Lspec " + strLowlim + ";")) +((strUpplim == "NA") ? "" : ("  Uspec " + strUpplim + ";"))+ "  Pooled;  AMR;  UnBiased;  OBiased;  Toler 6;  Within;   Percent; CStat.";

    // The program is crashing here as a result of the columns not being created sequentially
    MtbProj.ExecuteCommand(strCommand);

    Mtb.Graph MtbGraph = MtbProj.Commands.Item(i).Outputs.Item(1).Graph;

    MtbGraph.SaveAs("C:\\MyGraph" + DateTime.Now.ToString("yyyy-MM-dd HHmmss"), true, Mtb.MtbGraphFileTypes.GFPNGHighColor);

    i++;
}

MtbApp.Quit();

When running this code (with the crashing section commented out), I get the following output:

enter image description here

It should look like this:

enter image description here

I am really puzzled about this result. The variable i is right, but what is affecting the column number?

I can't find much information on the Web about Minitab. I just read the start guide here.


Solution

  • This line is the problem.

    MtbColumn1 = MtbColumns.Add(null, null, i);
    

    The third parameter Quantity specifies the number of columns to add. On the first iteration of the loop, you add i = 1 column, but on the second iteration of the loop you add i = 2 columns. Each iteration of the loop will add an additional i columns, when what you really want is to add one column each time.

    Change the line to:

    MtbColumn1 = MtbColumns.Add();