Sample code for 30+ languages & platforms
Zig Requires Chilkat v11.0.0+

Google Sheets - Update (Set Values in a Range)

See more Google Sheets Examples

Sets values in a range of a spreadsheet. This example will demonstrate by first getting a range, then changing some values in the JSON, and then HTTPS PUT the changes back to the Google Sheet.

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"));

    // First get the cells in the range A1:B5
    http.setUrlVar("range", "Sheet1!A1:B5") catch {};
    http.setUrlVar("spreadsheetId", "1_SO2L-Y6nCayNpNppJLF0r9yHB2UnaCleGCKeE4O0SA") catch {};
    const json_response = http.quickGetStr(alloc, "https://sheets.googleapis.com/v4/spreadsheets/{$spreadsheetId}/values/{$range}") catch {
        std.debug.print("{s}\n", .{try http.getLastErrorText(alloc)});
        return;
    };

    std.debug.print("{s}\n", .{json_response});

    const json = try chilkat.JsonObject.init();
    defer json.deinit();
    json.setEmitCompact(false);
    json.load(json_response) catch {};

    // A sample response is shown below.

    // {
    //   "range": "Sheet1!A1:B5",
    //   "majorDimension": "ROWS",
    //   "values": [
    //     [
    //       "Item",
    //       "Cost"
    //     ],
    //     [
    //       "Wheel",
    //       "$20.50"
    //     ],
    //     [
    //       "Door",
    //       "$15"
    //     ],
    //     [
    //       "Engine",
    //       "$100"
    //     ],
    //     [
    //       "Totals",
    //       "$135.50"
    //     ]
    //   ]
    // }

    // We're going to change the cost of the Engine to $120, and the Totals to $155.50

    json.setI(3);
    json.setJ(1);
    json.updateString("values[i][j]", "$120") catch {};
    json.setI(4);
    json.updateString("values[i][j]", "$155.50") catch {};

    // Show the updated JSON.
    std.debug.print("{s}\n", .{try json.emit(alloc)});

    // Update the Google Sheet using a PUT request.
    json.setEmitCompact(true);
    const url_to_update = "https://sheets.googleapis.com/v4/spreadsheets/{$spreadsheetId}/values/{$range}?valueInputOption=USER_ENTERED";
    _ = try http.quickGetStr(alloc, url_to_update);
    const resp = try chilkat.HttpResponse.init();
    defer resp.deinit();
    http.httpJson("PUT", url_to_update, json, "application/json", resp) catch {
        std.debug.print("{s}\n", .{try http.getLastErrorText(alloc)});
        return;
    };

    // Examine the response..
    std.debug.print("response status code = {d}\n", .{resp.getStatusCode()});
    std.debug.print("response body:\n", .{});
    std.debug.print("{s}\n", .{try resp.getBodyStr(alloc)});

    // A sample response body:

    // {
    //   "spreadsheetId": "1_SO2L-Y6nCayNpNppJLF0r9yHB2UnaCleGCKeE4O0SA",
    //   "updatedRange": "Sheet1!A1:B5",
    //   "updatedRows": 5,
    //   "updatedColumns": 2,
    //   "updatedCells": 10
    // }
}