2017-06-05 92 views
1

一般來說,我有一個問題,我無法從表中讀取數據。 我遇到的問題非常簡單,我將在Excel電子表格的示例中對其進行描述。通過API + c從Google電子表格提取數據#

從狀態表中獲取單元格值,如果此單元格顯示「未發送」或單元格爲空,則我從另一個單元格獲取同一行中的數據並開始處理這些數據。 成功完成任務後,我將狀態「已發送」設置爲接收數據的單元。這裏一切都很簡單,但是使用API​​不理解。已經掌握了授權,即使用標準代碼,我可以將數據寫入表格,但不能獲取請求,編輯代碼。 下面是代碼:

using Google.Apis.Auth.OAuth2; 
 
using Google.Apis.Sheets.v4; 
 
using Google.Apis.Sheets.v4.Data; 
 
using Google.Apis.Services; 
 
using Google.Apis.Util.Store; 
 
using System; 
 
using System.Collections.Generic; 
 
using System.IO; 
 
using System.Linq; 
 
using System.Text; 
 
using System.Threading; 
 
using System.Threading.Tasks; 
 

 
namespace SheetsQuickstart 
 
{ 
 
    class Program 
 
    { 
 
     // If modifying these scopes, delete your previously saved credentials 
 
     // at ~/.credentials/sheets.googleapis.com-dotnet-quickstart.json 
 
     static string[] Scopes = { SheetsService.Scope.SpreadsheetsReadonly }; 
 
     static string ApplicationName = "Google Sheets API .NET Quickstart"; 
 

 
     static void Main(string[] args) 
 
     { 
 
      UserCredential credential; 
 

 
      using (var stream = 
 
       new FileStream("client_secret.json", FileMode.Open, FileAccess.Read)) 
 
      { 
 
       string credPath = System.Environment.GetFolderPath(
 
        System.Environment.SpecialFolder.Personal); 
 
       credPath = Path.Combine(credPath, ".credentials/sheets.googleapis.com-dotnet-quickstart.json"); 
 

 
       credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
 
        GoogleClientSecrets.Load(stream).Secrets, 
 
        Scopes, 
 
        "user", 
 
        CancellationToken.None, 
 
        new FileDataStore(credPath, true)).Result; 
 
       Console.WriteLine("Credential file saved to: " + credPath); 
 
      } 
 

 
      // Create Google Sheets API service. 
 
      var service = new SheetsService(new BaseClientService.Initializer() 
 
       { 
 
        HttpClientInitializer = credential, 
 
        ApplicationName = ApplicationName, 
 
       }); 
 

 
      // Define request parameters. 
 
      String spreadsheetId = "1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms"; 
 
      String range = "Class Data!A2:E"; 
 
      SpreadsheetsResource.ValuesResource.GetRequest request = 
 
        service.Spreadsheets.Values.Get(spreadsheetId, range); 
 

 
      // Prints the names and majors of students in a sample spreadsheet: 
 
      // https://docs.google.com/spreadsheets/d/1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms/edit 
 
      ValueRange response = request.Execute(); 
 
      IList<IList<Object>> values = response.Values; 
 
      if (values != null && values.Count > 0) 
 
      { 
 
       Console.WriteLine("Name, Major"); 
 
       foreach (var row in values) 
 
       { 
 
        // Print columns A and E, which correspond to indices 0 and 4. 
 
        Console.WriteLine("{0}, {1}", row[0], row[4]); 
 
       } 
 
      } 
 
      else 
 
      { 
 
       Console.WriteLine("No data found."); 
 
      } 
 
      Console.Read(); 
 

 

 
     } 
 
    } 
 
}

回答

相關問題