/// <summary> /// Executes the script. If no instance of the script object exists, a error message will be stored and the return value is false. /// If the script object exists, the Execute function of this script object is called. /// </summary> /// <param name="myTable">The data table this script is working on.</param> /// <returns>True if executed without exceptions, otherwise false.</returns> /// <remarks>If exceptions were thrown during execution, the exception messages are stored /// inside the column script and can be recalled by the Errors property.</remarks> public bool Execute(Altaxo.Data.DataTable myTable) { if (null == m_ScriptObject) { m_Errors = new string[1] { "Script Object is null" }; return(false); } DataTable clonedTable = (DataTable)myTable.Clone(); clonedTable.DataColumns.RemoveRowsAll(); Altaxo.Collections.AscendingIntegerCollection rowsToCopy = new Altaxo.Collections.AscendingIntegerCollection(); int len = myTable.DataRowCount; try { Altaxo.Calc.ExtractTableValuesExeBase scriptObject = (Altaxo.Calc.ExtractTableValuesExeBase)m_ScriptObject; for (int i = 0; i < len; i++) { if (scriptObject.IsRowIncluded(myTable, i)) { rowsToCopy.Add(i); } } } catch (Exception ex) { m_Errors = new string[1]; m_Errors[0] = ex.ToString(); return(false); } for (int i = myTable.DataColumns.ColumnCount - 1; i >= 0; i--) { for (int j = rowsToCopy.Count - 1; j >= 0; j--) { clonedTable.DataColumns[i][j] = myTable.DataColumns[i][rowsToCopy[j]]; } } Current.Project.DataTableCollection.Add(clonedTable); Current.ProjectService.OpenOrCreateWorksheetForTable(clonedTable); return(true); }
/// <summary> /// Executes the script. If no instance of the script object exists, a error message will be stored and the return value is false. /// If the script object exists, the Execute function of this script object is called. /// </summary> /// <param name="myTable">The data table this script is working on.</param> /// <returns>True if executed without exceptions, otherwise false.</returns> /// <remarks>If exceptions were thrown during execution, the exception messages are stored /// inside the column script and can be recalled by the Errors property.</remarks> public bool Execute(Altaxo.Data.DataTable myTable) { if (null == _scriptObject) { _errors = ImmutableArray.Create(new CompilerDiagnostic(null, null, DiagnosticSeverity.Error, "Script Object is null")); return(false); } var clonedTable = (DataTable)myTable.Clone(); clonedTable.DataColumns.RemoveRowsAll(); var rowsToCopy = new Altaxo.Collections.AscendingIntegerCollection(); int len = myTable.DataRowCount; try { var scriptObject = (Altaxo.Calc.ExtractTableValuesExeBase)_scriptObject; for (int i = 0; i < len; i++) { if (scriptObject.IsRowIncluded(myTable, i)) { rowsToCopy.Add(i); } } } catch (Exception ex) { _errors = ImmutableArray.Create(new CompilerDiagnostic(null, null, DiagnosticSeverity.Error, ex.ToString())); return(false); } for (int i = myTable.DataColumns.ColumnCount - 1; i >= 0; i--) { for (int j = rowsToCopy.Count - 1; j >= 0; j--) { clonedTable.DataColumns[i][j] = myTable.DataColumns[i][rowsToCopy[j]]; } } Current.Project.DataTableCollection.Add(clonedTable); Current.ProjectService.OpenOrCreateWorksheetForTable(clonedTable); return(true); }
public static void PutTable(this OriginConnection conn, Altaxo.Data.DataTable srcTable, bool appendRows) { if (IsColumnReorderingNeccessaryForPuttingTableToOrigin(srcTable)) { srcTable = (DataTable)srcTable.Clone(); ReorderColumnsInTableForCompatibilityWithOrigin(srcTable); } var stb = new System.Text.StringBuilder(); string strWksName = srcTable.ShortName; strWksName.Trim(); // Validate worksheet name: if (0 == strWksName.Length) { ShowErrorMessage("Please specify a worksheet name first."); return; } int nColumns = srcTable.DataColumnCount; int nRows = srcTable.DataRowCount; // Validate the number of columns and the number of rows: if (nColumns <= 0 || nRows <= 0) { ShowErrorMessage("Data table is empty, thus nothing needs to be copyied to Origin!"); return; } var app = conn.Application; var originFolder = GetOrCreateFullFolderPath(app, srcTable.FolderName); var wbk = GetOrCreateWorksheetPage(originFolder, strWksName); // for every group in our worksheet, make a separate origin worksheet var wks = wbk.Layers[0] as Origin.Worksheet; wks.ClearData(); wks.Cols = 0; wks.set_LabelVisible(Origin.LABELTYPEVALS.LT_LONG_NAME, true); // Set the column names for (int i = 0; i < srcTable.DataColumnCount; ++i) { var srcCol = srcTable.DataColumns[i]; var srcGroup = srcTable.DataColumns.GetColumnGroup(srcCol); Origin.Column col = wks.Columns.Add(srcTable.DataColumns.GetColumnName(i)); col.LongName = srcTable.DataColumns.GetColumnName(i); if (srcCol is DoubleColumn) { col.DataFormat = COLDATAFORMAT.DF_DOUBLE; col.SetData((srcCol as DoubleColumn).Array); } else if (srcCol is DateTimeColumn) { col.DataFormat = COLDATAFORMAT.DF_DATE; col.SetData((srcCol as DateTimeColumn).Array); } else if (srcCol is TextColumn) { col.DataFormat = COLDATAFORMAT.DF_TEXT; col.SetData((srcCol as TextColumn).Array); } else { throw new NotImplementedException("Type of column not implemented"); } col.Type = AltaxoToOriginColumnType(srcTable.DataColumns.GetColumnKind(srcCol)); } // end of loop for all data columns // now put the property columns to ORIGIN // note that ORIGIN has only special property columns // LongName (but this is set already), Units, Comments // and more generic property columns accessible by Parameter(0), Parameter(1) and so on var usedPropColIndices = new HashSet <int>(); // Longname for (int i = 0; i < srcTable.PropCols.ColumnCount; ++i) { if (usedPropColIndices.Contains(i)) { continue; } if (IsLikeAnyOf(srcTable.PropCols.GetColumnName(i), LongNamePropertyColumnNames)) { usedPropColIndices.Add(i); for (int j = 0; j < srcTable.DataColumnCount; ++j) { wks.Columns[j].LongName = srcTable.PropCols[i][j].ToString(); } wks.set_LabelVisible(Origin.LABELTYPEVALS.LT_LONG_NAME, true); } } // Units for (int i = 0; i < srcTable.PropCols.ColumnCount; ++i) { if (usedPropColIndices.Contains(i)) { continue; } if (IsLikeAnyOf(srcTable.PropCols.GetColumnName(i), UnitPropertyColumnNames)) { usedPropColIndices.Add(i); for (int j = 0; j < srcTable.DataColumnCount; ++j) { wks.Columns[j].Units = srcTable.PropCols[i][j].ToString(); } wks.set_LabelVisible(Origin.LABELTYPEVALS.LT_UNIT, true); } } // Comments for (int i = 0; i < srcTable.PropCols.ColumnCount; ++i) { if (usedPropColIndices.Contains(i)) { continue; } if (IsLikeAnyOf(srcTable.PropCols.GetColumnName(i), CommentPropertyColumnNames)) { usedPropColIndices.Add(i); for (int j = 0; j < srcTable.DataColumnCount; ++j) { wks.Columns[j].Comments = srcTable.PropCols[i][j].ToString(); } wks.set_LabelVisible(Origin.LABELTYPEVALS.LT_COMMENT, true); } } // other property columns for (int i = 0, k = 0; i < srcTable.PropCols.ColumnCount; ++i) { if (usedPropColIndices.Contains(i)) { continue; } usedPropColIndices.Add(i); for (int j = 0; j < srcTable.DataColumnCount; ++j) { wks.Columns[j].Parameter[k] = srcTable.PropCols[i][j].ToString(); } wks.set_LabelVisible(Origin.LABELTYPEVALS.LT_PARAM, true); ++k; } }