Results 1 to 3 of 3

Thread: [RESOLVED] Google Spreadsheet API

  1. #1

    Thread Starter
    Join Date
    Oct 2016

    Resolved [RESOLVED] Google Spreadsheet API

    My tool is used to submit information from user to my database, and I'm trying to use Google API to also sync my database information with a Google spreadsheet so other colleagues can access the information too.

    However, I'm unable to set that up, can't find any samples for that online either (at least for Visual Basic).

    Anyone dealt with Google API before? Any tips would be appreciated, thanks.

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Chelmsford UK

    Re: Google Spreadsheet API

    Here's a C# console application example that i converted to VB...

    Option Infer On
    Imports Google.Apis.Auth.OAuth2
    Imports Google.Apis.Sheets.v4
    Imports Google.Apis.Sheets.v4.Data
    Imports Google.Apis.Services
    Imports Google.Apis.Util.Store
    Imports System
    Imports System.Collections.Generic
    Imports System.IO
    Imports System.Threading
    Namespace SheetsQuickstart
        Friend Class Program
            ' If modifying these scopes, delete your previously saved credentials
            ' at ~/.credentials/sheets.googleapis.com-dotnet-quickstart.json
            Private Shared Scopes() As String = {SheetsService.Scope.SpreadsheetsReadonly}
            Private Shared ApplicationName As String = "Google Sheets API .NET Quickstart"
            Shared Sub Main(ByVal args() As String)
                Dim credential As UserCredential
                Using stream = New FileStream("credentials.json", FileMode.Open, FileAccess.Read)
                    Dim credPath As String = "token.json"
                    credential = GoogleWebAuthorizationBroker.AuthorizeAsync(GoogleClientSecrets.Load(stream).Secrets, Scopes, "user", CancellationToken.None, New FileDataStore(credPath, True)).Result
                    Console.WriteLine("Credential file saved to: " & credPath)
                End Using
                ' Create Google Sheets API service.
                Dim service = New SheetsService(New BaseClientService.Initializer() With { _
                         .HttpClientInitializer = credential, _
                         .ApplicationName = ApplicationName _
                ' Define request parameters.
                Dim spreadsheetId As String = "1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms"
                Dim range As String = "Class Data!A2:E"
                Dim request As SpreadsheetsResource.ValuesResource.GetRequest = 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
                Dim response As ValueRange = request.Execute()
                Dim values As IList(Of IList(Of Object)) = response.Values
                If values IsNot Nothing AndAlso values.Count > 0 Then
                    Console.WriteLine("Name, Major")
                    For Each row In values
                        ' Print columns A and E, which correspond to indices 0 and 4.
                        Console.WriteLine("{0}, {1}", row(0), row(4))
                    Next row
                    Console.WriteLine("No data found.")
                End If
            End Sub
        End Class
    End Namespace

  3. #3

    Thread Starter
    Join Date
    Oct 2016

    Re: Google Spreadsheet API

    Thank you, that'll help me a lot getting started!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts


Click Here to Expand Forum to Full Width