Xbase++
Xbase++
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 Xbase++ Downloads
LOCAL nSuccess
LOCAL oJsonToken
LOCAL oHttp
LOCAL cJsonResponse
LOCAL oJson
LOCAL i
LOCAL nCount_i
LOCAL j
LOCAL nCount_j
LOCAL cRange
LOCAL cMajorDimension
LOCAL cStrVal
nSuccess := 0
// 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..
oJsonToken := CreateObject("Chilkat.JsonObject")
nSuccess := oJsonToken:LoadFile("qa_data/tokens/googleSheets.json")
IF (oJsonToken:HasMember("access_token") == 0)
? "No access token found."
oJsonToken:destroy()
RETURN
ENDIF
oHttp := CreateObject("Chilkat.Http")
oHttp:AuthToken := oJsonToken:StringOf("access_token")
oHttp:SessionLogFilename := "qa_output/sessionLog.txt"
// Get the cells defined by the range A1:B5
cJsonResponse := oHttp:QuickGetStr("https://sheets.googleapis.com/v4/spreadsheets/spreadsheetId/values/Sheet1!A1:B5")
IF (oHttp:LastMethodSuccess != 1)
? oHttp:LastErrorText
oJsonToken:destroy()
oHttp:destroy()
RETURN
ENDIF
? cJsonResponse
oJson := CreateObject("Chilkat.JsonObject")
oJson:Load(cJsonResponse)
// 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"
// ]
// ]
// }
cRange := oJson:StringOf("range")
cMajorDimension := oJson:StringOf("majorDimension")
i := 0
nCount_i := oJson:SizeOfArray("values")
DO WHILE i < nCount_i
oJson:I := i
j := 0
nCount_j := oJson:SizeOfArray("values[i]")
DO WHILE j < nCount_j
oJson:J := j
cStrVal := oJson:StringOf("values[i][j]")
j := j + 1
ENDDO
i := i + 1
ENDDO
oJsonToken:destroy()
oHttp:destroy()
oJson:destroy()