Sample code for 30+ languages & platforms
Rust

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 Rust Downloads

Rust

// 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;
}

let http = chilkat::Http::new();
http.set_auth_token(&json_token.string_of("access_token").unwrap_or_default());
http.set_session_log_filename("qa_output/sessionLog.txt");

// Get the cells defined by the range A1:B5
let Ok(json_response) = http.quick_get_str("https://sheets.googleapis.com/v4/spreadsheets/spreadsheetId/values/Sheet1!A1:B5") else {
    println!("{}", http.last_error_text());
    return;
};

println!("{}", json_response);

let json = chilkat::JsonObject::new();
let _ = json.load(&json_response);

// 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"
//     ]
//   ]
// }

let mut i: i32 = 0;
let mut j: i32 = 0;
let mut count_j: i32 = 0;

let range = json.string_of("range").unwrap_or_default();
let major_dimension = json.string_of("majorDimension").unwrap_or_default();
i = 0;
let count_i = json.size_of_array("values");
while i < count_i {
    json.set_i(i);
    j = 0;
    count_j = json.size_of_array("values[i]");
    while j < count_j {
        json.set_j(j);
        let str_val = json.string_of("values[i][j]").unwrap_or_default();
        j = j + 1;
    }

    i = i + 1;
}