Background
Recently, I was working on a proof of concept to SharePoint to GSuite data/content migration. As part of this POC, I was required to use Google API to create a spreadsheet and add data to the spreadsheet using .NET. The source data was to be read from SharePoint List items using CSOM.
The Generic Idea here is to create a new spreadsheet using Drive API and then use the Sheet API to update data in the spreadsheet.
If you don't have the basic understanding of Drive and Sheet API, I would request you to go through the below links.
- https://developers.google.com/drive/api/
- https://developers.google.com/sheets/api/
Prerequisites
- Enable Google Drive and Spreadsheet API to generate the client Id and client secret (crendentials.json) which will be used later. There are many articles already available on how to do this. So, I won't explain it here. Please refer to the below links for quick reference.
https://developers.google.com/drive/api/v3/quickstart/dotnet
https://developers.google.com/sheets/api/quickstart/dotnet
Please note that if you are doing this in ASP.NET, you have to generate the client id and secret for the web application.
- Create a Windows Console application or web application (using Visual Studio)
- Add a reference to Google API dll via NuGet package. Alternatively, you can download it manually from nuget.org link and add references.
- Below is a screenshot of DLLs required.

Now, let us start with code snippets.
Create a Spreadsheet
Call the below method to create a spreadsheet. I have added comments to explain.
- public Google.Apis.Drive.v3.Data.File CreateSheet()
- {
- string[] scopes = new string[] { DriveService.Scope.Drive,
- DriveService.Scope.DriveFile,};
- var clientId = "123456337-wajklowlksflmxiowerasjdflsl.apps.googleusercontent.com"; // From https://console.developers.google.com
- var clientSecret = "kkslfdkiwowol_ssdwerss"; // From https://console.developers.google.com
- // here is where we Request the user to give us access, or use the Refresh Token that was previously stored in %AppData%
- var credential = GoogleWebAuthorizationBroker.AuthorizeAsync(new ClientSecrets
- {
- ClientId = clientId,
- ClientSecret = clientSecret
- },scopes,
- Environment.UserName,CancellationToken.None,new FileDataStore("MyAppsToken")).Result;
- //Once consent is recieved, your token will be stored locally on the AppData directory, so that next time you wont be prompted for consent.
- DriveService _service = new DriveService(new BaseClientService.Initializer()
- {
- HttpClientInitializer = credential,
- ApplicationName = "MyAppName",
- });
- var _parent = "";//ID of folder if you want to create spreadsheet in specific folder
- var filename = "helloworld";
- var fileMetadata = new Google.Apis.Drive.v3.Data.File()
- {
- Name = filename,
- MimeType = "application/vnd.google-apps.spreadsheet",
- //TeamDriveId = teamDriveID, // IF you want to add to specific team drive
- };
- FilesResource.CreateRequest request = _service.Files.Create(fileMetadata);
- request.SupportsTeamDrives = true;
- fileMetadata.Parents = new List<string> { _parent }; // Parent folder id or TeamDriveID
- request.Fields = "id";
- System.Net.ServicePointManager.ServerCertificateValidationCallback = delegate (object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { return true; };
- var file = request.Execute();
- MessageBox.Show("File ID: " + file.Id);
- return file;
- }
Now, let us see what will happen when the above method is executed, assuming we are calling this method on button click of a Windows application like the below 'Create Spreadsheet'.

A new browser window would open asking to authenticate the user. Enter your Google credentials.

The User Consent screen will be displayed. Provide access to the Google Drive.

A success message will be displayed with the file's id.

Go to Google drive; a file with name 'helloworld' will be created of type spreadsheet.



Faisal zubiarPosted Aug 29, 2022, 12:37 PM
Siddharth can you please post article on BatchUpdateByDataFilter? it will be very helpful for us.
Faisal zubiarPosted Aug 29, 2022, 11:44 AM
Siddharth is this an example of BatchUpdateByDataFilter?
Faj'run Ni'amPosted Nov 18, 2021, 2:44 AM
Thanks for this. can you help me? coding to read and write google spreadsheets via google api net library ? thanks
vimal rajPosted Jan 28, 2021, 8:39 AM
Thanks for this. The local system is working fine. my web application, after deploying Into production, ive a error like like ‘Access to the path 'C:\Windows\system32\config\systemprofile' is denied.’
José Roberto García MartínezPosted Dec 24, 2019, 3:05 AM
Hello Siddharth, I am trying to create a spreadsheet, but this error is thrown: Google.GoogleApiException: 'Google.Apis.Requests.RequestErrorFile not found: . [404] Errors [ Message[File not found: .] Location[fileId - parameter] Reason[notFound] Domain[global]
Dinesh GabhanePosted Nov 6, 2019, 1:33 AM
Nice Article