Rust
Rust
batchGet (Read Multiple Ranges)
See more Google Sheets Examples
Reads multiple ranges from a Google Sheets spreadsheet in one GET request.Chilkat Rust Downloads
// 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..
let json_token = chilkat::JsonObject::new();
let _ = json_token.load_file("qa_data/tokens/googleSheets.json").is_ok();
if !json_token.has_member("access_token") {
println!("No access token found.");
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"
let req = chilkat::HttpRequest::new();
req.set_path("/v4/spreadsheets/spreadsheetId/values:batchGet");
req.set_http_verb("GET");
// Add each range to fetch.
req.add_param("ranges", "Sheet1!A1:A2");
req.add_param("ranges", "Sheet1!B1:B2");
let http = chilkat::Http::new();
http.set_auth_token(&json_token.string_of("access_token").unwrap_or_default());
// 443 is the SSL/TLS port for HTTPS.
let resp = chilkat::HttpResponse::new();
if http.http_s_req("sheets.googleapis.com", 443, true, &req, &resp).is_err() {
println!("{}", http.last_error_text());
return;
}
println!("{}", resp.body_str());
let json = chilkat::JsonObject::new();
let _ = json.load(&resp.body_str());
// 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"
// ]
// ]
// }
// ]
// }
let mut i: i32 = 0;
let mut j: i32 = 0;
let mut count_j: i32 = 0;
let mut k: i32 = 0;
let mut count_k: i32 = 0;
let spreadsheet_id = json.string_of("spreadsheetId").unwrap_or_default();
i = 0;
let count_i = json.size_of_array("valueRanges");
while i < count_i {
json.set_i(i);
let range = json.string_of("valueRanges[i].range").unwrap_or_default();
let major_dimension = json.string_of("valueRanges[i].majorDimension").unwrap_or_default();
j = 0;
count_j = json.size_of_array("valueRanges[i].values");
while j < count_j {
json.set_j(j);
k = 0;
count_k = json.size_of_array("valueRanges[i].values[j]");
while k < count_k {
json.set_k(k);
let str_val = json.string_of("valueRanges[i].values[j][k]").unwrap_or_default();
k = k + 1;
}
j = j + 1;
}
i = i + 1;
}