Sample code for 30+ languages & platforms
Unicode C

batchGet (Read Multiple Ranges)

See more Google Sheets Examples

Reads multiple ranges from a Google Sheets spreadsheet in one GET request.

Chilkat Unicode C Downloads

Unicode C
#include <C_CkJsonObjectW.h>
#include <C_CkHttpRequestW.h>
#include <C_CkHttpW.h>
#include <C_CkHttpResponseW.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkJsonObjectW jsonToken;
    HCkHttpRequestW req;
    HCkHttpW http;
    HCkHttpResponseW resp;
    HCkJsonObjectW json;
    int i;
    int count_i;
    int j;
    int count_j;
    int k;
    int count_k;
    const wchar_t *spreadsheetId;
    const wchar_t *range;
    const wchar_t *majorDimension;
    const wchar_t *strVal;

    success = FALSE;

    // 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..
    jsonToken = CkJsonObjectW_Create();
    success = CkJsonObjectW_LoadFile(jsonToken,L"qa_data/tokens/googleSheets.json");
    if (CkJsonObjectW_HasMember(jsonToken,L"access_token") == FALSE) {
        wprintf(L"No access token found.\n");
        CkJsonObjectW_Dispose(jsonToken);
        return;
    }

    // We'll be sending a GET request with query params to this URL:  https://sheets.googleapis.com/v4/spreadsheets/spreadsheetId/values:batchGet?ranges=Sheet1!A1:A2&ranges=Sheet1!B1:B2
    // The domain is "sheets.googleapis.com"
    // The path is "/v4/spreadsheets/spreadsheetId/values:batchGet"
    req = CkHttpRequestW_Create();
    CkHttpRequestW_putPath(req,L"/v4/spreadsheets/spreadsheetId/values:batchGet");
    CkHttpRequestW_putHttpVerb(req,L"GET");
    // Add each range to fetch.
    CkHttpRequestW_AddParam(req,L"ranges",L"Sheet1!A1:A2");
    CkHttpRequestW_AddParam(req,L"ranges",L"Sheet1!B1:B2");

    http = CkHttpW_Create();
    CkHttpW_putAuthToken(http,CkJsonObjectW_stringOf(jsonToken,L"access_token"));

    // 443 is the SSL/TLS port for HTTPS.
    resp = CkHttpResponseW_Create();
    success = CkHttpW_HttpSReq(http,L"sheets.googleapis.com",443,TRUE,req,resp);
    if (success == FALSE) {
        wprintf(L"%s\n",CkHttpW_lastErrorText(http));
        CkJsonObjectW_Dispose(jsonToken);
        CkHttpRequestW_Dispose(req);
        CkHttpW_Dispose(http);
        CkHttpResponseW_Dispose(resp);
        return;
    }

    wprintf(L"%s\n",CkHttpResponseW_bodyStr(resp));

    json = CkJsonObjectW_Create();
    CkJsonObjectW_Load(json,CkHttpResponseW_bodyStr(resp));

    // 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": "1_SO2L-Y6nCayNpNppJLF0r9yHB2UnaCleGCKeE4O0SA",
    //   "valueRanges": [
    //     {
    //       "range": "Sheet1!A1:A2",
    //       "majorDimension": "ROWS",
    //       "values": [
    //         [
    //           "Item"
    //         ],
    //         [
    //           "Wheel"
    //         ]
    //       ]
    //     },
    //     {
    //       "range": "Sheet1!B1:B2",
    //       "majorDimension": "ROWS",
    //       "values": [
    //         [
    //           "Cost"
    //         ],
    //         [
    //           "$20.50"
    //         ]
    //       ]
    //     }
    //   ]
    // }

    spreadsheetId = CkJsonObjectW_stringOf(json,L"spreadsheetId");
    i = 0;
    count_i = CkJsonObjectW_SizeOfArray(json,L"valueRanges");
    while (i < count_i) {
        CkJsonObjectW_putI(json,i);
        range = CkJsonObjectW_stringOf(json,L"valueRanges[i].range");
        majorDimension = CkJsonObjectW_stringOf(json,L"valueRanges[i].majorDimension");
        j = 0;
        count_j = CkJsonObjectW_SizeOfArray(json,L"valueRanges[i].values");
        while (j < count_j) {
            CkJsonObjectW_putJ(json,j);
            k = 0;
            count_k = CkJsonObjectW_SizeOfArray(json,L"valueRanges[i].values[j]");
            while (k < count_k) {
                CkJsonObjectW_putK(json,k);
                strVal = CkJsonObjectW_stringOf(json,L"valueRanges[i].values[j][k]");
                k = k + 1;
            }

            j = j + 1;
        }

        i = i + 1;
    }



    CkJsonObjectW_Dispose(jsonToken);
    CkHttpRequestW_Dispose(req);
    CkHttpW_Dispose(http);
    CkHttpResponseW_Dispose(resp);
    CkJsonObjectW_Dispose(json);

    }