Sample code for 30+ languages & platforms
Zig

Google Sheets - Create a New Spreadsheet

See more Google Sheets Examples

Demonstrates how to create a new and empty spreadsheet.

Chilkat Zig Downloads

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

    const http = try chilkat.Http.init();
    defer http.deinit();
    http.setAuthToken(try json_token.stringOf(alloc, "access_token"));

    // 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:
    const json = try chilkat.JsonObject.init();
    defer json.deinit();
    json.updateString("sheets[0].properties.title", "Sample Tab") catch {};
    json.updateString("properties.title", "Create Spreadsheet using Sheets API v4") catch {};

    // Send the POST to create the new Google spreadsheet.
    const resp = try chilkat.HttpResponse.init();
    defer resp.deinit();
    http.httpJson("POST", "https://sheets.googleapis.com/v4/spreadsheets", json, "application/json", resp) catch {
        std.debug.print("{s}\n", .{try http.getLastErrorText(alloc)});
        return;
    };

    std.debug.print("response status code = {d}\n", .{resp.getStatusCode()});
    std.debug.print("response JSON:\n", .{});

    json.load(try resp.getBodyStr(alloc)) catch {};
    json.setEmitCompact(false);
    std.debug.print("{s}\n", .{try json.emit(alloc)});

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

    var i: i32 = 0;

    _ = try json.stringOf(alloc, "spreadsheetId");
    var properties_title: [:0]const u8 = try json.stringOf(alloc, "properties.title");
    _ = try json.stringOf(alloc, "properties.locale");
    _ = try json.stringOf(alloc, "properties.autoRecalc");
    _ = try json.stringOf(alloc, "properties.timeZone");
    _ = json.intOf("properties.defaultFormat.backgroundColor.red");
    _ = json.intOf("properties.defaultFormat.backgroundColor.green");
    _ = json.intOf("properties.defaultFormat.backgroundColor.blue");
    _ = json.intOf("properties.defaultFormat.padding.top");
    _ = json.intOf("properties.defaultFormat.padding.right");
    _ = json.intOf("properties.defaultFormat.padding.bottom");
    _ = json.intOf("properties.defaultFormat.padding.left");
    _ = try json.stringOf(alloc, "properties.defaultFormat.verticalAlignment");
    _ = try json.stringOf(alloc, "properties.defaultFormat.wrapStrategy");
    _ = try json.stringOf(alloc, "properties.defaultFormat.textFormat.fontFamily");
    _ = json.intOf("properties.defaultFormat.textFormat.fontSize");
    _ = json.boolOf("properties.defaultFormat.textFormat.bold");
    _ = json.boolOf("properties.defaultFormat.textFormat.italic");
    _ = json.boolOf("properties.defaultFormat.textFormat.strikethrough");
    _ = json.boolOf("properties.defaultFormat.textFormat.underline");
    _ = try json.stringOf(alloc, "spreadsheetUrl");
    i = 0;
    const count_i = json.sizeOfArray("sheets");
    while (i < count_i) {
        json.setI(i);
        _ = json.intOf("sheets[i].properties.sheetId");
        properties_title = try json.stringOf(alloc, "sheets[i].properties.title");
        _ = json.intOf("sheets[i].properties.index");
        _ = try json.stringOf(alloc, "sheets[i].properties.sheetType");
        _ = json.intOf("sheets[i].properties.gridProperties.rowCount");
        _ = json.intOf("sheets[i].properties.gridProperties.columnCount");
        i = i + 1;
    }
}