Rust
Rust
Google Sheets - Create a New Spreadsheet
See more Google Sheets Examples
Demonstrates how to create a new and empty spreadsheet.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;
}
let http = chilkat::Http::new();
http.set_auth_token(&json_token.string_of("access_token").unwrap_or_default());
// Create the following JSON:
// The JSON code can be generated using this online tool: Generate JSON create code
// {
// "sheets": [
// {
// "properties": {
// "title": "Sample Tab"
// }
// }
// ],
// "properties": {
// "title": "Create Spreadsheet using Sheets API v4"
// }
// }
// This code generates the above JSON:
let json = chilkat::JsonObject::new();
let _ = json.update_string("sheets[0].properties.title", "Sample Tab");
let _ = json.update_string("properties.title", "Create Spreadsheet using Sheets API v4");
// Send the POST to create the new Google spreadsheet.
let resp = chilkat::HttpResponse::new();
if http.http_json("POST", "https://sheets.googleapis.com/v4/spreadsheets", &json, "application/json", &resp).is_err() {
println!("{}", http.last_error_text());
return;
}
println!("response status code = {}", resp.status_code());
println!("response JSON:");
let _ = json.load(&resp.body_str());
json.set_emit_compact(false);
println!("{}", json.emit().unwrap_or_default());
// 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": "1ueEQu3WDBkIAOUhzLnY4zr6JO5SrJx0dQ-YkQlUVYD0",
// "properties": {
// "title": "Create Spreadsheet using Sheets API v4",
// "locale": "en_US",
// "autoRecalc": "ON_CHANGE",
// "timeZone": "Etc/GMT",
// "defaultFormat": {
// "backgroundColor": {
// "red": 1,
// "green": 1,
// "blue": 1
// },
// "padding": {
// "top": 2,
// "right": 3,
// "bottom": 2,
// "left": 3
// },
// "verticalAlignment": "BOTTOM",
// "wrapStrategy": "OVERFLOW_CELL",
// "textFormat": {
// "foregroundColor": {},
// "fontFamily": "arial,sans,sans-serif",
// "fontSize": 10,
// "bold": false,
// "italic": false,
// "strikethrough": false,
// "underline": false
// }
// }
// },
// "sheets": [
// {
// "properties": {
// "sheetId": 1629642057,
// "title": "Sample Tab",
// "index": 0,
// "sheetType": "GRID",
// "gridProperties": {
// "rowCount": 1000,
// "columnCount": 26
// }
// }
// }
// ],
// "spreadsheetUrl": "https://docs.google.com/spreadsheets/d/1ueEQu3WDBkIAOUhzLnY4zr6JO5SrJx0dQ-YkQlUVYD0/edit"
// }
//
let mut i: i32 = 0;
let spreadsheet_id = json.string_of("spreadsheetId").unwrap_or_default();
let mut properties_title = json.string_of("properties.title").unwrap_or_default();
let properties_locale = json.string_of("properties.locale").unwrap_or_default();
let properties_auto_recalc = json.string_of("properties.autoRecalc").unwrap_or_default();
let properties_time_zone = json.string_of("properties.timeZone").unwrap_or_default();
let properties_default_format_background_color_red = json.int_of("properties.defaultFormat.backgroundColor.red");
let properties_default_format_background_color_green = json.int_of("properties.defaultFormat.backgroundColor.green");
let properties_default_format_background_color_blue = json.int_of("properties.defaultFormat.backgroundColor.blue");
let properties_default_format_padding_top = json.int_of("properties.defaultFormat.padding.top");
let properties_default_format_padding_right = json.int_of("properties.defaultFormat.padding.right");
let properties_default_format_padding_bottom = json.int_of("properties.defaultFormat.padding.bottom");
let properties_default_format_padding_left = json.int_of("properties.defaultFormat.padding.left");
let properties_default_format_vertical_alignment = json.string_of("properties.defaultFormat.verticalAlignment").unwrap_or_default();
let properties_default_format_wrap_strategy = json.string_of("properties.defaultFormat.wrapStrategy").unwrap_or_default();
let properties_default_format_text_format_font_family = json.string_of("properties.defaultFormat.textFormat.fontFamily").unwrap_or_default();
let properties_default_format_text_format_font_size = json.int_of("properties.defaultFormat.textFormat.fontSize");
let properties_default_format_text_format_bold = json.bool_of("properties.defaultFormat.textFormat.bold");
let properties_default_format_text_format_italic = json.bool_of("properties.defaultFormat.textFormat.italic");
let properties_default_format_text_format_strikethrough = json.bool_of("properties.defaultFormat.textFormat.strikethrough");
let properties_default_format_text_format_underline = json.bool_of("properties.defaultFormat.textFormat.underline");
let spreadsheet_url = json.string_of("spreadsheetUrl").unwrap_or_default();
i = 0;
let count_i = json.size_of_array("sheets");
while i < count_i {
json.set_i(i);
let properties_sheet_id = json.int_of("sheets[i].properties.sheetId");
properties_title = json.string_of("sheets[i].properties.title").unwrap_or_default();
let properties_index = json.int_of("sheets[i].properties.index");
let properties_sheet_type = json.string_of("sheets[i].properties.sheetType").unwrap_or_default();
let properties_grid_properties_row_count = json.int_of("sheets[i].properties.gridProperties.rowCount");
let properties_grid_properties_column_count = json.int_of("sheets[i].properties.gridProperties.columnCount");
i = i + 1;
}