Sample code for 30+ languages & platforms
Lianja

Google Sheets - Read a Single Range

See more Google Sheets Examples

Reads the values stored in the range Sheet1!A1:B5 and returns them in the response.

Chilkat Lianja Downloads

Lianja
llSuccess = .F.

// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.

// This example uses a previously obtained access token having permission for the 
// Google Sheets scope.

// In this example, Get Google Sheets OAuth2 Access Token, the access
// token was saved to a JSON file.  This example fetches the access token from the file..
loJsonToken = createobject("CkJsonObject")
llSuccess = loJsonToken.LoadFile("qa_data/tokens/googleSheets.json")
if (loJsonToken.HasMember("access_token") = .F.) then
    ? "No access token found."
    release loJsonToken
    return
endif

loHttp = createobject("CkHttp")
loHttp.AuthToken = loJsonToken.StringOf("access_token")
loHttp.SessionLogFilename = "qa_output/sessionLog.txt"

// Get the cells defined by the range A1:B5
lcJsonResponse = loHttp.QuickGetStr("https://sheets.googleapis.com/v4/spreadsheets/spreadsheetId/values/Sheet1!A1:B5")
if (loHttp.LastMethodSuccess <> .T.) then
    ? loHttp.LastErrorText
    release loJsonToken
    release loHttp
    return
endif

? lcJsonResponse

loJson = createobject("CkJsonObject")
loJson.Load(lcJsonResponse)

// A sample response is shown below.
// To generate the parsing source code for a JSON response, paste
// the JSON into this online tool: Generate JSON parsing code

// {
//   "range": "Sheet1!A1:B5",
//   "majorDimension": "ROWS",
//   "values": [
//     [
//       "Item",
//       "Cost"
//     ],
//     [
//       "Wheel",
//       "$20.50"
//     ],
//     [
//       "Door",
//       "$15"
//     ],
//     [
//       "Engine",
//       "$100"
//     ],
//     [
//       "Totals",
//       "$135.50"
//     ]
//   ]
// }

lcRange = loJson.StringOf("range")
lcMajorDimension = loJson.StringOf("majorDimension")
i = 0
lnCount_i = loJson.SizeOfArray("values")
do while i < lnCount_i
    loJson.I = i
    j = 0
    lnCount_j = loJson.SizeOfArray("values[i]")
    do while j < lnCount_j
        loJson.J = j
        lcStrVal = loJson.StringOf("values[i][j]")
        j = j + 1
    enddo
    i = i + 1
enddo


release loJsonToken
release loHttp
release loJson