// 新規ワークブックの作成 var workbook = new GrapeCity.Documents.Excel.Workbook(); #region カスタムクラスの定義 //public static class FetchService //{ // public static DownloadFile FetchData = new DownloadFile(); //} //public class DownloadFile //{ // private volatile int _percentage = 0; // public int FetchPercentage // { // get // { // return _percentage; // } // set // { // _percentage = value; // } // } //} //public class FetchDataFunction : AsyncCustomFunction //{ // public FetchDataFunction() // : base("FetchData", FunctionValueType.Text, new Parameter[] { }) // { // } // async protected override Task<object> EvaluateAsync(object[] arguments, ICalcContext context) // { // var file = FetchService.FetchData; // for (int i = 0; i < 100; i++) // { // // 0.1秒ごとに1%ずつデータを取得するシミュレーションを実行 // await Task.Delay(100); // file.FetchPercentage++; // } // return "Fetch complete"; // } //} //public class FetchPercentageFunction : CustomFunction //{ // public FetchPercentageFunction() // : base("FetchPercentage", FunctionValueType.Text, new Parameter[] { }) // { // } // public override object Evaluate(object[] arguments, ICalcContext context) // { // var fetchPercentage = FetchService.FetchData.FetchPercentage; // return fetchPercentage; // } //} #endregion var ws = workbook.ActiveSheet; Excel.Workbook.AddCustomFunction(new FetchDataFunction()); Excel.Workbook.AddCustomFunction(new FetchPercentageFunction()); ws.Range["A1"].Formula = "=FetchData()"; ws.Range["B1"].Formula = "=FetchPercentage()"; workbook.Calculate(); while (ws.Range["A1"].Value is CalcError) { // 現在の進行状況を1秒ごとに取得 System.Threading.Thread.Sleep(1000); // セル「B1」の式を再計算するよう指定 ws.Range["B1"].Dirty(); var FetchState = ws.Range["A1"].Value; var FetchPercentage = ws.Range["B1"].Value; if (FetchState is CalcError) { FetchState = "Fetching"; } else { // FetchState が CalcError.Busy でなければ、FetchPercentage を更新 ws.Range["B1"].Dirty(); FetchPercentage = ws.Range["B1"].Value; } Console.WriteLine(FetchState + "\t" + FetchPercentage + "%"); }
' 新規ワークブックの作成 Dim workbook As New Workbook #Region "カスタムクラスの定義" 'Public Module FetchService ' Public FetchData As New FetchData() 'End Module 'Public Class FetchData ' Private _percentage As Integer = 0 ' Public Property FetchPercentage() As Integer ' Get ' Return _percentage ' End Get ' Set(ByVal value As Integer) ' _percentage = value ' End Set ' End Property 'End Class 'Public Class FetchDataFunction ' Inherits AsyncCustomFunction ' Public Sub New() ' MyBase.New("FetchData", FunctionValueType.Text, New Parameter() {}) ' End Sub ' Protected Overrides Async Function EvaluateAsync(ByVal arguments() As Object, ByVal context As ICalcContext) As Task(Of Object) ' Dim file = FetchService.FetchData ' For i As Integer = 0 To 99 ' ' 0.1秒ごとに1%ずつデータを取得するシミュレーションを実行 ' Await Task.Delay(100) ' file.FetchPercentage += 1 ' Next i ' Return "Fetch complete" ' End Function 'End Class 'Public Class FetchPercentageFunction ' Inherits CustomFunction ' Public Sub New() ' MyBase.New("FetchPercentage", FunctionValueType.Text, New Parameter() {}) ' End Sub ' Public Overrides Function Evaluate(ByVal arguments() As Object, ByVal context As ICalcContext) As Object ' Dim fetchPercentage = FetchService.FetchData.FetchPercentage ' Return fetchPercentage ' End Function 'End Class #End Region Dim ws = workbook.ActiveSheet Excel.Workbook.AddCustomFunction(New FetchDataFunction()) Excel.Workbook.AddCustomFunction(New FetchPercentageFunction()) ws.Range("A1").Formula = "=FetchData()" ws.Range("B1").Formula = "=FetchPercentage()" workbook.Calculate() Do While TypeOf ws.Range("A1").Value Is CalcError ' 現在の進行状況を1秒ごとに取得 System.Threading.Thread.Sleep(1000) ' セル「B1」の式を再計算するよう指定 ws.Range("B1").Dirty() Dim FetchState = ws.Range("A1").Value Dim FetchPercentage = ws.Range("B1").Value If TypeOf FetchState Is CalcError Then FetchState = "Fetching" Else ' FetchState が CalcError.Busy でなければ、FetchPercentage を更新 ws.Range("B1").Dirty() FetchPercentage = ws.Range("B1").Value End If Console.WriteLine(FetchState.ToString & vbTab & FetchPercentage.ToString & "%") Loop