範囲のアドレスとシート名を指定する

最終更新日: 2010年3月24日

適用対象: SharePoint Server 2010

この例では、範囲の座標、名前付き範囲、行、および列によって範囲のアドレスを指定する方法を示します。また、シート名とシート名および範囲のアドレス間のリレーションシップを指定する方法も示します。

範囲の座標は、連続した範囲の選択に使用する 4 つの整数座標です。範囲の座標では、"A1" 式の代わりに直接整数インデックス処理を使用して Excel の範囲を指定することができます。上行、左列、高さ、および幅の座標を指定できます。一連のセルをループで繰り返すコードを使用する場合、またはアルゴリズムの一部として範囲の座標が動的に計算される場合は、範囲の座標を使用する方が簡単です。

Excel Web Services は "現在のシート" を認識しないので、範囲の指定にはシート名を含める必要があります。シート名を指定する方法には次のような方法があります。

  • "Sheet3!B12:D18" のように、範囲のアドレスの一部として指定します。この場合、シート名の引数は空になります。

    object[] rangeResult1 = xlservice.GetRangeA1(sessionId, String.Empty, "Sheet2!A12:G18", true, out outStatus);
    
    Dim rangeResult1() As Object = xlservice.GetRangeA1(sessionId, String.Empty, "Sheet2!A12:G18", True, outStatus)
    
  • 独立したシート名の引数として指定します。この場合、範囲アドレスの引数にシート名を含める必要はありません。

    xlservice.SetCell(sessionId, "Sheet3", 0, 11, 1000);
    
    xlservice.SetCell(sessionId, "Sheet3", 0, 11, 1000)
    
  • シート名と範囲アドレスの両方で指定します。この場合、シートの名前は一致していなければなりません。

    object[] rangeResult = xlservice.GetCellA1(sessionId, "Sheet3", "Sheet3!G18", true, out outStatus);
    
    Dim rangeResult() As Object = xlservice.GetCellA1(sessionId, "Sheet3", "Sheet3!G18", True, outStatus)
    

名前付き範囲にはブックの範囲がある場合があるので、シート名を指定する必要がないのは名前付き範囲だけです。たとえば、次のようにシート名の引数を指定せずに名前付き範囲を参照することができます。

xlServices.SetCellA1(sessionId, String.Empty, "MyNamedRange", 8);
xlServices.SetCellA1(sessionId, String.Empty, "MyNamedRange", 8)

シート名を指定する場合、参照する範囲は指定したシート上に存在する必要があります。存在しないシートを指定した場合、呼び出しが失敗し、シートが存在しないことを示す簡易オブジェクト アクセス プロトコル (SOAP) の例外が表示されます。

注意

既に SharePoint ドキュメント ライブラリを作成し、それを信頼できる場所にしたことを前提としています。詳細については、「[方法] 場所を信頼する」と「[方法] スクリプトを使用してブックの場所を信頼する」を参照してください。

using System;
using System.Text;
using System.Web.Services.Protocols;
using ExcelWebService.myserver02;
namespace ExcelWebService
{
/// <summary>
/// Summary description for Class1.
/// </summary>
    class MyExcelWebService
    {
        [STAThread]
        static void Main(string[] args)
        {
            // Instantiate the Web service 
            // and range coordinate array object.
            ExcelService xlservice = new ExcelService();
            Status[] outStatus;
            RangeCoordinates rangeCoordinates = new RangeCoordinates();
            string sheetName = "MySheet1";

            // TODO: Change the path to the workbook
            // to point to a workbook you have access to.
            // The workbook must be in a trusted location.
            // Using the workbook path this way will allow 
            // you to call the workbook remotely.
            string targetWorkbookPath = 
       "http://myserver02/example/Shared%20Documents/MyWorkbook1.xlsx";

            // Set Credentials for requests
            xlservice.Credentials = 
                System.Net.CredentialCache.DefaultCredentials;

            try
            {
                // Call the open workbook, and point to    
                // the workbook to open.
                string sessionId = 
                    xlservice.OpenWorkbook(targetWorkbookPath, 
                        String.Empty, String.Empty, out outStatus);
                // Prepare object to define range coordinates
                // and call the GetRange method.
                // startCol, startRow, startHeight, and startWidth
                // get their values from user input.
                rangeCoordinates.Column = (int)startCol.Value;
                rangeCoordinates.Row = (int)startRow.Value;
                rangeCoordinates.Height = (int)startHeight.Value;
                rangeCoordinates.Width = (int)startWidth.Value;

                object[] rangeResult1 = xlservice.GetRange(sessionId, 
                    sheetName, rangeCoordinates, false, out outStatus);
                Console.WriteLine("Total rows in range: " + 
                    rangeResult1.Length);
                Console.WriteLine("Sum in last column is: " + 
                    ((object[])rangeResult1[18])[11]);

                // Call the SetCell method, which invokes 
                // the Calculate method.
                // Set first row in last column cell to 1000.
                xlservice.SetCell(sessionId, sheetName, 0, 11, 1000);

                // Call the GetRange method again to see if 
                // the Sum total in the last column changed.
                object[] rangeResult2 = xlservice.GetRange(sessionId, 
                    sheetName, rangeCoordinates, false, out outStatus);    
                Console.WriteLine("Sum in the last column after SetCell 
                    is: " + ((object[])rangeResult2[18])[11]); 

                // Close workbook. This also closes the session.
                xlservice.CloseWorkbook(sessionId);
            }

            catch (SoapException e)
            {
                Console.WriteLine("Exception Message: {0}", e.Message);
            }
            catch (Exception e)
            {
                Console.WriteLine("Exception Message: {0}", e.Message);
            }
            Console.ReadLine();
        }
    }
}
Imports System
Imports System.Text
Imports System.Web.Services.Protocols
Imports ExcelWebService.myserver02
Namespace ExcelWebService
''' <summary>
''' Summary description for Class1.
''' </summary>
    Friend Class MyExcelWebService
        <STAThread> _
        Shared Sub Main(ByVal args() As String)
            ' Instantiate the Web service 
            ' and range coordinate array object.
            Dim xlservice As New ExcelService()
            Dim outStatus() As Status
            Dim rangeCoordinates As New RangeCoordinates()
            Dim sheetName As String = "MySheet1"

            ' TODO: Change the path to the workbook
            ' to point to a workbook you have access to.
            ' The workbook must be in a trusted location.
            ' Using the workbook path this way will allow 
            ' you to call the workbook remotely.
            Dim targetWorkbookPath As String = "http://myserver02/example/Shared%20Documents/MyWorkbook1.xlsx"

            ' Set Credentials for requests
            xlservice.Credentials = System.Net.CredentialCache.DefaultCredentials

            Try
                ' Call the open workbook, and point to    
                ' the workbook to open.
                Dim sessionId As String = xlservice.OpenWorkbook(targetWorkbookPath, String.Empty, String.Empty, outStatus)
                ' Prepare object to define range coordinates
                ' and call the GetRange method.
                ' startCol, startRow, startHeight, and startWidth
                ' get their values from user input.
                rangeCoordinates.Column = CInt(Fix(startCol.Value))
                rangeCoordinates.Row = CInt(Fix(startRow.Value))
                rangeCoordinates.Height = CInt(Fix(startHeight.Value))
                rangeCoordinates.Width = CInt(Fix(startWidth.Value))

                Dim rangeResult1() As Object = xlservice.GetRange(sessionId, sheetName, rangeCoordinates, False, outStatus)
                Console.WriteLine("Total rows in range: " & rangeResult1.Length)
                Console.WriteLine("Sum in last column is: " & (CType(rangeResult1(18), Object()))(11))

                ' Call the SetCell method, which invokes 
                ' the Calculate method.
                ' Set first row in last column cell to 1000.
                xlservice.SetCell(sessionId, sheetName, 0, 11, 1000)

                ' Call the GetRange method again to see if 
                ' the Sum total in the last column changed.
                Dim rangeResult2() As Object = xlservice.GetRange(sessionId, sheetName, rangeCoordinates, False, outStatus)
                Console.WriteLine("Sum in the last column after SetCell is: " & (CType(rangeResult2(18), Object()))(11))

                ' Close workbook. This also closes the session.
                xlservice.CloseWorkbook(sessionId)

            Catch e As SoapException
                Console.WriteLine("Exception Message: {0}", e.Message)
            Catch e As Exception
                Console.WriteLine("Exception Message: {0}", e.Message)
            End Try
            Console.ReadLine()
        End Sub
    End Class
End Namespace

堅牢なプログラミング

アクセス権のある Excel Web Services サイトに Web 参照が追加されていることを確認します。次のように変更します。

  • 参照する Web サービスを指すように using ExcelWebService.myserver02; ステートメントを変更します。

  • アクセス権のあるブックを指すように string targetWorkbookPath = "http://myserver02/example/Shared%20Documents/Book1.xlsx"; を変更します。ブックは信頼できる場所になければなりません。

関連項目

タスク

[方法] 範囲から値を取得する

[方法] 範囲の値を設定する

[ウォークスルー] Excel Web Services を使用してカスタム アプリケーションを開発する

概念

SOAP API にアクセスする