2017-10-10 119 views
0

我想顯示從29/5到30/6但是在間隔中的所有日子的工作時間。它包含星期六和星期日(非工作日)。我使用TimeScaleData,但它在看到非工作日時自動停止。我嘗試將(1)添加到結束日,但仍然停止,所以我無法正確顯示工作時間。如何在TimeScaleData中跳過日期(讀取mpp文件)?

Microsoft.Office.Interop.MSProject.Application app = new Microsoft.Office.Interop.MSProject.Application(); 
app.FileOpenEx(Path, false, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, 
    PjPoolOpen.pjPoolReadWrite, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing); 
Microsoft.Office.Interop.MSProject.Project pj = app.ActiveProject; 
foreach(Resource re in pj.Resources) 
{ 
    foreach (Assignment assign in re.Assignments) 
    { 
     double h = 0; 
     var tsvs = assign.TimeScaleData("5/29/2017", "7/1/2017", MSProject.PjAssignmentTimescaledData.pjAssignmentTimescaledWork, MSProject.PjTimescaleUnit.pjTimescaleDays, 1); 
     try 
     { 
      foreach (TimeScaleValue tsv in tsvs) 
      { 
       for (int i = 0; i < totalday ; i++) 
       { 
        h += Convert.ToDouble(tsv.Value); //sum all value 
       }  
      } 

     } 
     catch { } 
    } 
} 

enter image description here

回答

0

你的代碼遇到這條線運行時錯誤:h += tsv.Value因爲tsv.Value是天空字符串沒有工作。嘗試添加前請檢查數據類型tsv.Value

MSDN Documentation -note Value屬性返回Variant而不是double。

+0

它的工作原理。臭蟲和臭蟲,我感到壓力:v:v非常感謝瑞秋。 – gggg

+0

嗨@Rachel,我在VBA中研究和檢測函數Val。該帖子說:「」tsv.Value爲對象日期範圍外的時間片返回一個空字符串(「」)。 Val(tsv.Value)將空字符串轉換爲零,這更有用「」。我理解Val()類似於C#中的Convert.ToDouble,但是當我使用它時會看到上面的錯誤。 – gggg

+0

使用前檢查'tsv.Value'的數據類型。看到[這個SO帖子的類型檢查在C#](https://stackoverflow.com/questions/983030/type-checking-typeof-gettype-oris)。 –

0

在我的代碼tsv.value遍歷時間刻度值時返回一個空字符串。檢查IsNumeric工作:

Private Sub ProcessAssignments(T As Task) 

    Dim A As Assignment 
    Dim tsvsHours As TimeScaleValues 
    Dim tsvsCosts0 As TimeScaleValues 
    Dim dblWork As Double 
    Dim curCostClassA As Double 


    ' Process assignments 
    For Each A In T.Assignments 
     ' Get the timescale collection objects for Hours and Costs 
     tsvsHours = A.TimeScaleData(
      StartDate:=T.BaselineStart, 
      EndDate:=T.BaselineFinish, 
      Type:=PjAssignmentTimescaledData.pjAssignmentTimescaledBaselineWork, 
      TimeScaleUnit:=PjTimescaleUnit.pjTimescaleMonths, 
      Count:=1) 

     tsvsCosts0 = A.TimeScaleData(
      StartDate:=T.BaselneStart, 
      EndDate:=T.BaselineFinish, 
      Type:=PjAssignmentTimescaledData.pjAssignmentTimescaledBaselineCost, 
      TimeScaleUnit:=PjTimescaleUnit.pjTimescaleMonths, 
      Count:=1) 

     ' Iterate through the assignment timescalevalues 
     For i As Integer = 1 To tsvsCosts0.Count 
      If IsNumeric(tsvsCosts0(i).Value) = True Then 'Cannot process non-working times with an empty string value 

       ' Get the hours from the tsvsHours collection 
       If IsNumeric(tsvsHours(i).Value) Then 
        dblWork = CDbl(tsvsHours(i).Value/60) 
       Else 
        dblWork = 0 
       End If 

       ' Get the costs from the Baseline collection 
       If IsNumeric(tsvsCosts0(i).Value) Then 
        curCostClassA = tsvsCosts0(i).Value 
       Else 
        curCostClassA = 0 
       End If 


       ' Do stuff here 

      End If 
     Next i 

     tsvsHours = Nothing 
     tsvsCosts0 = Nothing 

    Next A 
End Sub 
+0

我檢查過,但我不知道它是否適用於incoorect。例如,2行有重複的開始,完成日期和日期的工作,但是row1是正確的,但no2不正確 – gggg

+0

我更新了例如提供一個完整的工作子集,也許在時間刻度集合中有些東西是關閉的 –

+0

Yah,謝謝Eric @@由於代碼讀取了9次以上的時間片,我添加了GC.Collect(),並且沒問題 – gggg