Zig
Zig
Google Sheets - Append Values to an Existing Spreadsheet
See more Google Sheets Examples
Appends values to an existing Google spreadsheet.Chilkat Zig Downloads
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"));
// To append values to an existing spreadsheet, our HTTP request body will
// contain JSON in the format of a "ValueRange". For example, the spreadsheet we'll be
// adding to in this example looks like this:
//
// The JSON ValueRange for the cells in the above spreadsheet is:
// {
// "range": "Sheet1!A1:B5",
// "majorDimension": "ROWS",
// "values": [
// [
// "Item",
// "Cost"
// ],
// [
// "Wheel",
// "$20.50"
// ],
// [
// "Door",
// "$15"
// ],
// [
// "Engine",
// "$100"
// ],
// [
// "Totals",
// "$135.50"
// ]
// ]
// }
// This example will append 6 cells (3 rows / 2 columns).
// We'll be appending the following:
//
// "Paint", "$100"
// "Brakes", "$100"
// "New Total", "$335.50"
//
// The range of cells we'll be appending is A1:B5
// Therefore, the ValueRange JSON we'll be sending in our POST body is:
// {
// "range": "Sheet1!A1:B5",
// "majorDimension": "ROWS",
// "values": [
// [
// "Paint",
// "$100"
// ],
// [
// "Brakes",
// "$100"
// ],
// [
// "New Total",
// "$335.50"
// ]
// ]
// }
const json = try chilkat.JsonObject.init();
defer json.deinit();
json.updateString("range", "Sheet1!A1:B5") catch {};
json.updateString("majorDimension", "ROWS") catch {};
json.setI(0);
json.setJ(1);
json.updateString("values[i][j]", "Paint") catch {};
json.setJ(1);
json.updateString("values[i][j]", "$100") catch {};
json.setI(1);
json.setJ(0);
json.updateString("values[i][j]", "Brakes") catch {};
json.setJ(1);
json.updateString("values[i][j]", "$100") catch {};
json.setI(2);
json.setJ(0);
json.updateString("values[i][j]", "Totals") catch {};
json.setJ(1);
json.updateString("values[i][j]", "$335.50") catch {};
json.setEmitCompact(false);
std.debug.print("{s}\n", .{try json.emit(alloc)});
// Send the POST to:
// https://sheets.googleapis.com/v4/spreadsheets/{spreadsheetId}/values/{range}:append?valueInputOption=USER_ENTERED
http.setUrlVar("spreadsheetId", "1_SO2L-Y6nCayNpNppJLF0r9yHB2UnaCleGCKeE4O0SA") catch {};
http.setUrlVar("range", "Sheet1!A1:B5") catch {};
const resp = try chilkat.HttpResponse.init();
defer resp.deinit();
http.httpJson("POST", "https://sheets.googleapis.com/v4/spreadsheets/{$spreadsheetId}/values/{$range}:append?valueInputOption=USER_ENTERED", 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 = {s}\n", .{try resp.getBodyStr(alloc)});
// Sample output:
//
// response status code = 200
// response JSON = {
// "spreadsheetId": "1_SO2L-Y6nCayNpNppJLF0r9yHB2UnaCleGCKeE4O0SA",
// "tableRange": "Sheet1!A1:B5",
// "updates": {
// "spreadsheetId": "1_SO2L-Y6nCayNpNppJLF0r9yHB2UnaCleGCKeE4O0SA",
// "updatedRange": "Sheet1!A6:B8",
// "updatedRows": 3,
// "updatedColumns": 2,
// "updatedCells": 6
// }
// }
//
// Our Google Spreadsheet now looks like this:
//
}