获取'对象引用未设置为对象的实例。'在request.Execute()之后

时间:2018-04-23 12:00:26

标签: c# visual-studio google-spreadsheet-api google-sheets-api

所以,我试图通过C#项目使用Sheets API v4附加到Google表格中。但是,我得到一个例外 - 在我的回复中对象引用没有设置为对象的实例

我正在传递请求方法的所有必需参数。

class Program
{
    static string[] Scopes = { SheetsService.Scope.Spreadsheets, SheetsService.Scope.DriveFile, SheetsService.Scope.Drive };
    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 = "1sxMw83gkCDvRpzTYXQOKg2VMI-LetU7INrtXgsLZ0uo";
        String range = "Sheet1";

        ValueRange vr = new ValueRange();
        IList<Object> c = new List<Object> { "TESTING CELL" };
        IList<IList<Object>> abc = new List<IList<Object>>();
        abc.Add(c);
        vr.Values = abc;

        SpreadsheetsResource.ValuesResource.AppendRequest request = service.Spreadsheets.Values.Append(vr, spreadsheetId, range);
        SpreadsheetsResource.ValuesResource.AppendRequest.ValueInputOptionEnum valueInputOption = (SpreadsheetsResource.ValuesResource.AppendRequest.ValueInputOptionEnum) 2;
        SpreadsheetsResource.ValuesResource.AppendRequest.InsertDataOptionEnum insertDataOption = (SpreadsheetsResource.ValuesResource.AppendRequest.InsertDataOptionEnum) 2;
        request.ValueInputOption = valueInputOption;
        request.InsertDataOption = insertDataOption;

        AppendValuesResponse response = request.Execute();
        Console.WriteLine(Newtonsoft.Json.JsonConvert.SerializeObject(response));
    }
}

更新

只需删除insertDataOption,即可使用附加功能。它不是必填字段。

0 个答案:

没有答案