Neolitz
Neolitz

Reputation: 127

C# Google API (SheetsServices) Insert new row

How i can insert new row to my google spreadsheet.

using Google API (SheetsServices).

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

        public GoogleAPI()
        {
            UserCredential credential;

            using (var stream =
                new FileStream("Secret.json", FileMode.Open, FileAccess.Read))
            {
                string credPath = System.Environment.GetFolderPath(
                    System.Environment.SpecialFolder.Personal);
                credPath = Path.Combine(credPath, ".credentials/Secret.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 Plus API service.
            var plusService = new PlusService(new BaseClientService.Initializer()
            {
                HttpClientInitializer = credential,
                ApplicationName = ApplicationName,
            });

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

            var me = plusService.People.Get("me").Execute();
            var useremail = me.Emails.FirstOrDefault().Value;
        }
    }

Up to this part, i have able to

The problem: How i can insert new row, in the sheets.

Additional Question: Is it possible to bind LINQ to my spreadsheet?

Upvotes: 2

Views: 3698

Answers (1)

Neolitz
Neolitz

Reputation: 127

After trying a little bit while waiting someone answer, i suddenly realize the answer. So i'm gonna post it here for others.

            IList<Object> obj = new List<Object>();
            obj.Add("A2");
            obj.Add("B2");
            IList<IList<Object>> values = new List<IList<Object>>();
            values.Add(obj);

            SpreadsheetsResource.ValuesResource.AppendRequest request =
                    service.Spreadsheets.Values.Append(new ValueRange() { Values = values }, spreadsheetId, range);
            request.InsertDataOption = SpreadsheetsResource.ValuesResource.AppendRequest.InsertDataOptionEnum.INSERTROWS;
            request.ValueInputOption = SpreadsheetsResource.ValuesResource.AppendRequest.ValueInputOptionEnum.RAW;
            var response = request.Execute();

The .InsertDataOption is the one allowing it to added as new row.

The .ValueInputOption is required, At first i didn't set it and error comes out.

Upvotes: 7

Related Questions