示例#1
0
        /// <summary>
        /// Will read entries from a specific view in Glasshouse. For all matching columns names, entries will be updated or added at the end of excel list
        /// </summary>
        public void ReadGH()
        {
            if (_curview.Equals("__GHALLDATA__"))
            {
                // not supported
                System.Windows.Forms.DialogResult dlg2 = System.Windows.Forms.MessageBox.Show("We do not support reading from " + _curviewname + " in project " + _curprojname, "Read from Glasshouse", System.Windows.Forms.MessageBoxButtons.OK);
                return;
            }

            System.Windows.Forms.DialogResult dlg = System.Windows.Forms.MessageBox.Show("Are you sure you want to read from view " + _curviewname + " in project " + _curprojname,
                                                                                         "Read from Glasshouse", System.Windows.Forms.MessageBoxButtons.YesNo);
            if (dlg == System.Windows.Forms.DialogResult.No)
            {
                return;
            }

            // allways read  - glasshousejournalguid, short description
            Range rngid = FindGUIDCell();

            if (rngid == null)
            {
                System.Windows.Forms.MessageBox.Show("glasshousejournalguid not found in the first 10 by 10 cells");
                return;
            }
            int idrow = rngid.Row;
            int idcol = rngid.Column;

            var removehcols = new[] { "glasshousejournalguid", "BIM Objects count", "BIM Objects quantity" };
            //            var removehcols = new[] { "glasshousejournalguid", "short description" };

            var   activeSheet = _excel.ActiveSheet as Worksheet;
            Range usedRange   = activeSheet.UsedRange;
            int   maxr        = usedRange.Rows[1].Row + usedRange.Rows.Count - 1;
            int   maxc        = usedRange.Columns[1].Column + usedRange.Columns.Count - 1;
            // Make dictinary of columns
            List <gColumns> headers = new List <gColumns>();

            for (int c = idcol; c <= maxc; c++)
            {
                if (activeSheet.Cells[idrow, c].Value2 == null)
                {
                    continue;
                }
                string sc = activeSheet.Cells[idrow, c].Value2 as string;
                if (sc.Length > 0)
                {
                    gColumns gc = new gColumns();
                    gc.headerName   = sc.Trim();
                    gc.headerNameLC = gc.headerName.ToLower();
                    gc.colNo        = c;
                    gc.sync2gh      = false;

                    //
                    if (removehcols.Any(gc.headerNameLC.Contains))
                    {
                        headers.Add(gc);
                        continue;
                    }

                    if (activeSheet.Cells[idrow + 1, c].Value2 == null)
                    {
                        headers.Add(gc);
                        continue;
                    }

                    string syncway = (activeSheet.Cells[idrow + 1, c].Value2 as string).ToLower().Trim();
                    if (syncway.Equals("update"))
                    {
                        gc.sync2gh = true;
                    }

                    headers.Add(gc);
                }
            }

            System.Data.DataTable table = null;

            string s       = "Contacting server...";
            string caption = "Getting View Entries";

            using (View.WaitingForm pf = new View.WaitingForm(caption, s))
            {
                table = JournalEntries.GetViewEntries(Utils.apiKey, _curproj, _curview);
            }

            var removecols = new[] { "BIM Objects count", "BIM Objects quantity" };

            int updateno = 0;
            int newno    = 0;

            maxr = Math.Max(maxr, idrow + 2);

            int n = table.Rows.Count;

            s       = "{0} of " + n.ToString() + " rows processed...";
            caption = "Getting Data From Glasshouse";

            List <string> guidsexist = new List <string>();


            //write to excel
            // Initialize the array.
            Range range   = activeSheet.Range(activeSheet.Cells[idrow, idcol], activeSheet.Cells[maxr, maxc]);
            var   myArray = (object[, ])range.Value2;

            int resultRows = myArray.GetLength(0);
            int resultCols = myArray.GetLength(1);

            using (ProgressForm pf = new ProgressForm(caption, s, n))
            {
                foreach (System.Data.DataRow row in table.Rows)
                {
                    string rguid = (string)row[0];
                    guidsexist.Add(rguid);
                    int foundrow = -1;
                    for (int r = 3; r <= resultRows; r++)
                    {
                        //var guid = activeSheet.Cells[r, idcol].Value2;
                        var guid = myArray[r, 1];

                        if (guid == null)
                        {
                            continue;
                        }
                        string sguid = guid as string;
                        if (sguid.Length == 0)
                        {
                            continue;
                        }


                        if (rguid.Equals(sguid) == true)
                        {
                            foundrow = r;
                            break;
                        }
                    }

                    int colno     = 0;
                    int activerow = foundrow;
                    if (foundrow == -1)
                    {
                        maxr++; // new line
                        newno++;
                        resultRows++;
                        activerow = resultRows;
                        myArray   = AddRow(myArray);
                    }
                    else
                    {
                        updateno++;
                    }
                    foreach (object col in row.ItemArray)
                    {
                        string colname = table.Columns[colno].ColumnName.ToLower().Trim();
                        colno++;
                        //if (removecols.Any(colname.Contains)) continue;

                        gColumns match = headers.Find(v => v.headerNameLC.Equals(colname));

                        if (match == null)
                        {
                            continue;
                        }
                        if (match.sync2gh == true)
                        {
                            continue;
                        }
                        //activeSheet.Cells[activerow, match.colNo].Value = col;
                        myArray[activerow, match.colNo - idcol + 1] = col;
                    }
                    pf.Increment();
                }
            }


            //int resultRows = myArray.GetLength(0);
            //int resultCols = myArray.GetLength(1);

            ExcelReference sheet2 = (ExcelReference)XlCall.Excel(XlCall.xlSheetId, activeSheet.Name);
            ExcelReference target = new ExcelReference(idrow - 1, maxr - 1, idcol - 1, maxc - 1, sheet2.SheetId);

            _excel.ScreenUpdating = false;
            _excel.EnableEvents   = false;
            ExcelAsyncUtil.QueueAsMacro(() => { target.SetValue(myArray); });
            //_excel.ScreenUpdating = true;
            //_excel.EnableEvents = true;


            //_excel.Interactive = false;
            //_excel.ScreenUpdating = false;
            //_excel.EnableEvents = false;


            // check not valid rows
            List <int> notfoundrow = new List <int>();
            int        notfound    = 0;

            for (int r = idrow + 2; r <= maxr; r++)
            {
                var guid = activeSheet.Cells[r, idcol].Value2;
                if (guid == null)
                {
                    continue;
                }
                string sguid = guid as string;
                if (sguid.Length == 0)
                {
                    continue;
                }

                if (guidsexist.Contains(sguid) == false)
                {
                    activeSheet.Cells[r, idcol].Font.Strikethrough = true;
                    notfound++;
                }
                else
                {
                    activeSheet.Cells[r, idcol].Font.Strikethrough = false;
                }
            }

            //_excel.Interactive = true;
            _excel.ScreenUpdating = true;
            _excel.EnableEvents   = true;

            table = null;

            System.Windows.Forms.MessageBox.Show("Updated " + updateno + " entries, " + notfound + " obsolete and added " + newno + " new entries ", "Read From Glasshouse");
        }
示例#2
0
        public void GetViewEntries()
        {
            /*
             * if (_curview.Equals("__GHALLDATA__"))
             * {
             #if DEBUG
             *
             *      System.Data.DataTable table2 = JournalEntries.GetAllViewEntries(Utils.apiKey, _curproj);
             *
             #endif
             *  // not supported
             *  System.Windows.Forms.DialogResult dlg = System.Windows.Forms.MessageBox.Show("We do not support reading from " + _curviewname + " in project " + _curprojname,
             *  "Read from Glasshouse", System.Windows.Forms.MessageBoxButtons.OK);
             *  return;
             * }
             */
            var activeSheet = _excel.ActiveSheet as Worksheet;
            //activeSheet.Range("A1").Value = "Hello, World!";

            var activeCell = _excel.ActiveCell as Range;

            System.Data.DataTable table = null;

            string s       = "Contacting server...";
            string caption = "Getting View Entries";

            using (View.WaitingForm wf = new View.WaitingForm(caption, s))
            {
                if (_curview.Equals("__GHALLDATA__"))
                {
                    table = JournalEntries.GetAllViewEntries(Utils.apiKey, _curproj);
                }
                else
                {
                    table = JournalEntries.GetViewEntries(Utils.apiKey, _curproj, _curview);
                }
            }



            int c = activeCell.Column;

            //var removecols = new[] { "BIM Objects count", "BIM Objects quantity" };
            //#if DEBUG
            //var removehcols = new[] { "glasshousejournalguid"  };
            var removehcols = new[] { "glasshousejournalguid", "BIM Objects count", "BIM Objects quantity" };
            //#else
            //            var removehcols = new[] { "glasshousejournalguid", "short description" };
            //#endif
            var allowedValues = new List <string> {
                "---", "Update"
            };

            int n = table.Rows.Count;

            s       = "{0} of " + n.ToString() + " rows processed...";
            caption = "Getting View Entries";

            /*
             * _excel.Interactive = false;
             * _excel.ScreenUpdating = false;
             * _excel.EnableEvents = false;
             *
             * using (ProgressForm pf = new ProgressForm(caption, s, n))
             * {
             *  foreach (System.Data.DataColumn col in table.Columns)
             *  {
             *      //if (removecols.Any(col.ColumnName.Contains)) continue;
             *
             *      int r = activeCell.Row;
             *      activeSheet.Cells[r, c].Value = col.ColumnName;
             *      r++;
             *      // add update keyword etc
             *      if (!removehcols.Any(col.ColumnName.ToLower().Contains))
             *      {
             *          activeSheet.Cells[r, c].AddCellListValidation(allowedValues);
             *      }
             *      //
             *      r++;
             *      foreach (System.Data.DataRow row in table.Rows)
             *      {
             *          activeSheet.Cells[r, c].Value = ((string)row[col]);
             *          r++;
             *      }
             *
             *      c++;
             *      pf.Increment();
             *  }
             *
             *  table = null;
             * }
             * _excel.Interactive = true;
             * _excel.ScreenUpdating = true;
             * _excel.EnableEvents = true;
             *
             */



            //write to excel
            int startRow    = activeCell.Row - 1;
            int startCol    = activeCell.Column - 1;
            int rowCount    = table.Rows.Count + 2;
            int columnCount = table.Columns.Count;

            int[] lowerBounds = new int[] { 1, 1 };
            int[] lengths     = new int[] { rowCount, columnCount };
            var   myArray     = (object[, ])Array.CreateInstance(typeof(object), lengths, lowerBounds);

            //var myArray = new object[rowCount, columnCount];
            // Initialize the array.
            for (int i = 1; i <= table.Columns.Count; i++)
            {
                myArray[1, i] = table.Columns[i - 1].ColumnName;
                myArray[2, i] = null;
            }


            using (ProgressForm pf = new ProgressForm(caption, s, n))
            {
                int activeRow = 3;
                foreach (System.Data.DataRow row in table.Rows)
                {
                    int activeCol = 1;
                    foreach (object col in row.ItemArray)
                    {
                        myArray[activeRow, activeCol] = col.ToString();
                        activeCol++;
                    }
                    activeRow++;
                }
                pf.Increment();
            }


            int resultRows = myArray.GetLength(0);
            int resultCols = myArray.GetLength(1);

            ExcelReference sheet2 = (ExcelReference)XlCall.Excel(XlCall.xlSheetId, activeSheet.Name);
            ExcelReference target = new ExcelReference(startRow, startRow + resultRows - 1, startCol, startCol + resultCols - 1, sheet2.SheetId);

            _excel.ScreenUpdating = false;
            _excel.EnableEvents   = false;
            ExcelAsyncUtil.QueueAsMacro(() => { target.SetValue(myArray); });
            _excel.ScreenUpdating = true;
            _excel.EnableEvents   = true;
        }