Zig
Zig
batchGet (Read Multiple Ranges)
See more Google Sheets Examples
Reads multiple ranges from a Google Sheets spreadsheet in one GET request.Chilkat Zig Downloads
const std = @import("std");
const chilkat = @import("chilkat");
pub fn main(init: std.process.Init) !void {
const alloc = init.arena.allocator();
// 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..
const json_token = try chilkat.JsonObject.init();
defer json_token.deinit();
try json_token.loadFile("qa_data/tokens/googleSheets.json");
if (!json_token.hasMember("access_token")) {
std.debug.print("No access token found.\n", .{});
return;
}
// We'll be sending a GET request with query params to this URL: https://sheets.googleapis.com/v4/spreadsheets/spreadsheetId/values:batchGet?ranges=Sheet1!A1:A2&ranges=Sheet1!B1:B2
// The domain is "sheets.googleapis.com"
// The path is "/v4/spreadsheets/spreadsheetId/values:batchGet"
const req = try chilkat.HttpRequest.init();
defer req.deinit();
req.setPath("/v4/spreadsheets/spreadsheetId/values:batchGet");
req.setHttpVerb("GET");
// Add each range to fetch.
req.addParam("ranges", "Sheet1!A1:A2");
req.addParam("ranges", "Sheet1!B1:B2");
const http = try chilkat.Http.init();
defer http.deinit();
http.setAuthToken(try json_token.stringOf(alloc, "access_token"));
// 443 is the SSL/TLS port for HTTPS.
const resp = try chilkat.HttpResponse.init();
defer resp.deinit();
http.httpSReq("sheets.googleapis.com", 443, true, req, resp) catch {
std.debug.print("{s}\n", .{try http.getLastErrorText(alloc)});
return;
};
std.debug.print("{s}\n", .{try resp.getBodyStr(alloc)});
const json = try chilkat.JsonObject.init();
defer json.deinit();
json.load(try resp.getBodyStr(alloc)) catch {};
// 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
// {
// "spreadsheetId": "1_SO2L-Y6nCayNpNppJLF0r9yHB2UnaCleGCKeE4O0SA",
// "valueRanges": [
// {
// "range": "Sheet1!A1:A2",
// "majorDimension": "ROWS",
// "values": [
// [
// "Item"
// ],
// [
// "Wheel"
// ]
// ]
// },
// {
// "range": "Sheet1!B1:B2",
// "majorDimension": "ROWS",
// "values": [
// [
// "Cost"
// ],
// [
// "$20.50"
// ]
// ]
// }
// ]
// }
var i: i32 = 0;
var j: i32 = 0;
var count_j: i32 = 0;
var k: i32 = 0;
var count_k: i32 = 0;
_ = try json.stringOf(alloc, "spreadsheetId");
i = 0;
const count_i = json.sizeOfArray("valueRanges");
while (i < count_i) {
json.setI(i);
_ = try json.stringOf(alloc, "valueRanges[i].range");
_ = try json.stringOf(alloc, "valueRanges[i].majorDimension");
j = 0;
count_j = json.sizeOfArray("valueRanges[i].values");
while (j < count_j) {
json.setJ(j);
k = 0;
count_k = json.sizeOfArray("valueRanges[i].values[j]");
while (k < count_k) {
json.setK(k);
_ = try json.stringOf(alloc, "valueRanges[i].values[j][k]");
k = k + 1;
}
j = j + 1;
}
i = i + 1;
}
}