Lua
Lua
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 Lua Downloads
local chilkat = require("chilkat")
-- 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..
local jsonToken = chilkat.JsonObject.new()
local success = jsonToken:load_file("qa_data/tokens/googleSheets.json")
if not jsonToken:has_member("access_token") then
print("No access token found.")
return
end
local http = chilkat.Http.new()
http.auth_token = jsonToken:string_of("access_token")
http.session_log_filename = "qa_output/sessionLog.txt"
-- Get the cells defined by the range A1:B5
local jsonResponse = http:quick_get_str("https://sheets.googleapis.com/v4/spreadsheets/spreadsheetId/values/Sheet1!A1:B5")
if not http.last_method_success then
print(http.last_error_text)
return
end
print(jsonResponse)
local json = chilkat.JsonObject.new()
json:load(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"
-- ]
-- ]
-- }
local j = 0
local count_j = 0
local range = json:string_of("range")
local majorDimension = json:string_of("majorDimension")
local i = 0
local count_i = json:size_of_array("values")
while i < count_i do
json.i = i
j = 0
count_j = json:size_of_array("values[i]")
while j < count_j do
json.j = j
local strVal = json:string_of("values[i][j]")
j = j + 1
end
i = i + 1
end