1
votes

I've been reading up on how to get my C# Windows Form to read from Google Sheets.

I think that I have a good grasp on how to read and write, but I'm incredibly lost as to how to get started.

I assume that the steps are as follows:
1. (Done) Get the API set up: https://developers.google.com/sheets/api/quickstart/dotnet

2. (Stuck here) Load up the sheet. I have no idea how to do this. I'm stuck on this. I've been unable to find a guide on the API page on how to actually load a sheet, and have been unable to find an updated code sample/guide online on how to accomplish the same thing.

3. Read/Write/Update values.





On this page: https://developers.google.com/sheets/api/guides/concepts at the bottom it seems to suggest using GET and then the sheet URL to get the sheet. However, when I use GET, I get this error: The type or namespace 'GET' could not be found.


Here's my code so far:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            GET https://sheets.googleapis.com/v4/spreadsheets/spreadsheetId?ranges=A1:C10&fields=properties.title,sheets(sheetProperties,data.rowData.values(effectiveValue,effectiveFormat))

        }
    }
1
This has nothing to do with Google Sheets. The compiler error says that you forgot to create a GetCredential() method in your form - Panagiotis Kanavos
@PJvG Thanks for your reply! I've actually just scrapped the entire application since I was starting to get really confused and started from scratch. I'll update the original post to reflect what step I'm actually on. - lolikols
@lolikols not to sound rude, but what you're stuck on is not anything specific to reading/writing Google Sheets, but rather a deficiency in basic C# syntax. It would probably make more sense for you to spend some time learning the language more through tutorials and books, otherwise you're likely to run into the same problems again and again. - DrewJordan
@DrewJordan You're absolutely right :( - lolikols

1 Answers

1
votes

So, the line GET https://sheets... is a HTTP request and not valid C# code.

What you need to do is to get a SheetsService first:

SheetsService sheetsService = new SheetsService(...) // (this will need some arguments)

And with this you can do the following to do a GET request in C#:

sheetsService.Spreadsheets.Values.Get(spreadsheetId, range);

Where spreadsheetId and range are strings. See the documentation of GET for more information.