Sample code for 30+ languages & platforms
Unicode C

Google Sheets - Create a New Spreadsheet

See more Google Sheets Examples

Demonstrates how to create a new and empty spreadsheet.

Chilkat Unicode C Downloads

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

void ChilkatSample(void)
    {
    BOOL success;
    HCkJsonObjectW jsonToken;
    HCkHttpW http;
    HCkJsonObjectW json;
    HCkHttpResponseW resp;
    int i;
    int count_i;
    const wchar_t *spreadsheetId;
    const wchar_t *propertiesTitle;
    const wchar_t *propertiesLocale;
    const wchar_t *propertiesAutoRecalc;
    const wchar_t *propertiesTimeZone;
    int propertiesDefaultFormatBackgroundColorRed;
    int propertiesDefaultFormatBackgroundColorGreen;
    int propertiesDefaultFormatBackgroundColorBlue;
    int propertiesDefaultFormatPaddingTop;
    int propertiesDefaultFormatPaddingRight;
    int propertiesDefaultFormatPaddingBottom;
    int propertiesDefaultFormatPaddingLeft;
    const wchar_t *propertiesDefaultFormatVerticalAlignment;
    const wchar_t *propertiesDefaultFormatWrapStrategy;
    const wchar_t *propertiesDefaultFormatTextFormatFontFamily;
    int propertiesDefaultFormatTextFormatFontSize;
    BOOL propertiesDefaultFormatTextFormatBold;
    BOOL propertiesDefaultFormatTextFormatItalic;
    BOOL propertiesDefaultFormatTextFormatStrikethrough;
    BOOL propertiesDefaultFormatTextFormatUnderline;
    const wchar_t *spreadsheetUrl;
    int propertiesSheetId;
    int propertiesIndex;
    const wchar_t *propertiesSheetType;
    int propertiesGridPropertiesRowCount;
    int propertiesGridPropertiesColumnCount;

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

    http = CkHttpW_Create();
    CkHttpW_putAuthToken(http,CkJsonObjectW_stringOf(jsonToken,L"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:
    json = CkJsonObjectW_Create();
    CkJsonObjectW_UpdateString(json,L"sheets[0].properties.title",L"Sample Tab");
    CkJsonObjectW_UpdateString(json,L"properties.title",L"Create Spreadsheet using Sheets API v4");

    //  Send the POST to create the new Google spreadsheet.
    resp = CkHttpResponseW_Create();
    success = CkHttpW_HttpJson(http,L"POST",L"https://sheets.googleapis.com/v4/spreadsheets",json,L"application/json",resp);
    if (success == FALSE) {
        wprintf(L"%s\n",CkHttpW_lastErrorText(http));
        CkJsonObjectW_Dispose(jsonToken);
        CkHttpW_Dispose(http);
        CkJsonObjectW_Dispose(json);
        CkHttpResponseW_Dispose(resp);
        return;
    }

    wprintf(L"response status code = %d\n",CkHttpResponseW_getStatusCode(resp));
    wprintf(L"response JSON:\n");

    CkJsonObjectW_Load(json,CkHttpResponseW_bodyStr(resp));
    CkJsonObjectW_putEmitCompact(json,FALSE);
    wprintf(L"%s\n",CkJsonObjectW_emit(json));

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

    spreadsheetId = CkJsonObjectW_stringOf(json,L"spreadsheetId");
    propertiesTitle = CkJsonObjectW_stringOf(json,L"properties.title");
    propertiesLocale = CkJsonObjectW_stringOf(json,L"properties.locale");
    propertiesAutoRecalc = CkJsonObjectW_stringOf(json,L"properties.autoRecalc");
    propertiesTimeZone = CkJsonObjectW_stringOf(json,L"properties.timeZone");
    propertiesDefaultFormatBackgroundColorRed = CkJsonObjectW_IntOf(json,L"properties.defaultFormat.backgroundColor.red");
    propertiesDefaultFormatBackgroundColorGreen = CkJsonObjectW_IntOf(json,L"properties.defaultFormat.backgroundColor.green");
    propertiesDefaultFormatBackgroundColorBlue = CkJsonObjectW_IntOf(json,L"properties.defaultFormat.backgroundColor.blue");
    propertiesDefaultFormatPaddingTop = CkJsonObjectW_IntOf(json,L"properties.defaultFormat.padding.top");
    propertiesDefaultFormatPaddingRight = CkJsonObjectW_IntOf(json,L"properties.defaultFormat.padding.right");
    propertiesDefaultFormatPaddingBottom = CkJsonObjectW_IntOf(json,L"properties.defaultFormat.padding.bottom");
    propertiesDefaultFormatPaddingLeft = CkJsonObjectW_IntOf(json,L"properties.defaultFormat.padding.left");
    propertiesDefaultFormatVerticalAlignment = CkJsonObjectW_stringOf(json,L"properties.defaultFormat.verticalAlignment");
    propertiesDefaultFormatWrapStrategy = CkJsonObjectW_stringOf(json,L"properties.defaultFormat.wrapStrategy");
    propertiesDefaultFormatTextFormatFontFamily = CkJsonObjectW_stringOf(json,L"properties.defaultFormat.textFormat.fontFamily");
    propertiesDefaultFormatTextFormatFontSize = CkJsonObjectW_IntOf(json,L"properties.defaultFormat.textFormat.fontSize");
    propertiesDefaultFormatTextFormatBold = CkJsonObjectW_BoolOf(json,L"properties.defaultFormat.textFormat.bold");
    propertiesDefaultFormatTextFormatItalic = CkJsonObjectW_BoolOf(json,L"properties.defaultFormat.textFormat.italic");
    propertiesDefaultFormatTextFormatStrikethrough = CkJsonObjectW_BoolOf(json,L"properties.defaultFormat.textFormat.strikethrough");
    propertiesDefaultFormatTextFormatUnderline = CkJsonObjectW_BoolOf(json,L"properties.defaultFormat.textFormat.underline");
    spreadsheetUrl = CkJsonObjectW_stringOf(json,L"spreadsheetUrl");
    i = 0;
    count_i = CkJsonObjectW_SizeOfArray(json,L"sheets");
    while ((i < count_i)) {
        CkJsonObjectW_putI(json,i);
        propertiesSheetId = CkJsonObjectW_IntOf(json,L"sheets[i].properties.sheetId");
        propertiesTitle = CkJsonObjectW_stringOf(json,L"sheets[i].properties.title");
        propertiesIndex = CkJsonObjectW_IntOf(json,L"sheets[i].properties.index");
        propertiesSheetType = CkJsonObjectW_stringOf(json,L"sheets[i].properties.sheetType");
        propertiesGridPropertiesRowCount = CkJsonObjectW_IntOf(json,L"sheets[i].properties.gridProperties.rowCount");
        propertiesGridPropertiesColumnCount = CkJsonObjectW_IntOf(json,L"sheets[i].properties.gridProperties.columnCount");
        i = i + 1;
    }



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

    }