我已經開始使用 Lua 探索 Google Sheets API,雖然我可以檢索一系列值,但我只能弄清楚如何返回一列值,但我也想返回一行。
這是我用來檢索 Google 工作表中單元格 A1 到 D5 中的值的 Lua 代碼示例。(4列4行)
為了幫助我跟踪來自何處的內容,我將每個單元格的名稱添加到單元格本身中。
local socket = require 'socket'
local ssl = require 'ssl'
local https = require 'ssl.https'
local body, code, headers, status = https.request("https://sheets.googleapis.com/v4/spreadsheets/[mysheetID]/values/A1:D5?key=[MyAPIkey]")
print(body)
print("---------------------")
--[[ Which returns the following…
{
"range":"Sheet1!A1:D5",
"majorDimension":"ROWS",
"values":[
[
"Cell A1",
"Cell B1",
"Cell C1",
"Cell D1"
],
[
"Cell A2",
"Cell B2",
"Cell C2",
"Cell D2"
],
[
"Cell A3",
"Cell B3",
"Cell C3",
"Cell D3"
],
[
"Cell A4",
"Cell B4",
"Cell C4",
"Cell D4"
]
]
}
]]--
要顯示一列,我執行以下操作..
local json = require "dkjson"
local j = json.decode(body)
for k,v in pairs(j.values[1]) do
print(k,v)
end
它返回第一列(如下),而 j.values[2] 會給我第二列,依此類推..
1 Cell A1
2 Cell B1
3 Cell C1
4 Cell D1
我無法解決的是如何返回一行,即 A1、B1、C1、D1
請有人解釋我如何從列表中檢索任何行,並能夠始終如一地執行此操作,而不管我決定檢索的單元格範圍如何?
雖然我不確定您正在尋找什麼(您提到您正在獲取A1
, B1
, C1
, D1
,它對應於一行),但如果您想檢索按列而不是行排序的轉置結果,您可以添加查詢參數majorDimension=COLUMNS
(如果未設置此參數,則默認為ROWS
,正如您在ValueRange 中看到的那樣)。
因此,您必須從以下位置更改請求的 URL:
https://sheets.googleapis.com/v4/spreadsheets/[mysheetID]/values/A1:D5?key=[MyAPIkey]
到:
https://sheets.googleapis.com/v4/spreadsheets/[mysheetID]/values/A1:D5?majorDimension=COLUMNS&key=[MyAPIkey]
Dieser Artikel stammt aus dem Internet. Bitte geben Sie beim Nachdruck die Quelle an.
Bei Verstößen wenden Sie sich bitte [email protected] Löschen.
Lass mich ein paar Worte sagen