Tcl
Tcl
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 Tcl Downloads
load ./chilkat.dll
set success 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..
set jsonToken [new_CkJsonObject]
set success [CkJsonObject_LoadFile $jsonToken "qa_data/tokens/googleSheets.json"]
if {[CkJsonObject_HasMember $jsonToken "access_token"] == 0} then {
puts "No access token found."
delete_CkJsonObject $jsonToken
exit
}
set http [new_CkHttp]
CkHttp_put_AuthToken $http [CkJsonObject_stringOf $jsonToken "access_token"]
CkHttp_put_SessionLogFilename $http "qa_output/sessionLog.txt"
# Get the cells defined by the range A1:B5
set jsonResponse [CkHttp_quickGetStr $http "https://sheets.googleapis.com/v4/spreadsheets/spreadsheetId/values/Sheet1!A1:B5"]
if {[CkHttp_get_LastMethodSuccess $http] != 1} then {
puts [CkHttp_lastErrorText $http]
delete_CkJsonObject $jsonToken
delete_CkHttp $http
exit
}
puts "$jsonResponse"
set json [new_CkJsonObject]
CkJsonObject_Load $json $jsonResponse
# 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"
# ]
# ]
# }
set range [CkJsonObject_stringOf $json "range"]
set majorDimension [CkJsonObject_stringOf $json "majorDimension"]
set i 0
set count_i [CkJsonObject_SizeOfArray $json "values"]
while {$i < $count_i} {
CkJsonObject_put_I $json $i
set j 0
set count_j [CkJsonObject_SizeOfArray $json "values[i]"]
while {$j < $count_j} {
CkJsonObject_put_J $json $j
set strVal [CkJsonObject_stringOf $json "values[i][j]"]
set j [expr $j + 1]
}
set i [expr $i + 1]
}
delete_CkJsonObject $jsonToken
delete_CkHttp $http
delete_CkJsonObject $json