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 <CkJsonObjectW.h>
#include <CkHttpW.h>
#include <CkHttpResponseW.h>

void ChilkatSample(void)
    {
    bool 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..
    CkJsonObjectW jsonToken;
    success = jsonToken.LoadFile(L"qa_data/tokens/googleSheets.json");
    if (jsonToken.HasMember(L"access_token") == false) {
        wprintf(L"No access token found.\n");
        return;
    }

    CkHttpW http;
    http.put_AuthToken(jsonToken.stringOf(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:
    CkJsonObjectW json;
    json.UpdateString(L"sheets[0].properties.title",L"Sample Tab");
    json.UpdateString(L"properties.title",L"Create Spreadsheet using Sheets API v4");

    //  Send the POST to create the new Google spreadsheet.
    CkHttpResponseW resp;
    success = http.HttpJson(L"POST",L"https://sheets.googleapis.com/v4/spreadsheets",json,L"application/json",resp);
    if (success == false) {
        wprintf(L"%s\n",http.lastErrorText());
        return;
    }

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

    json.Load(resp.bodyStr());
    json.put_EmitCompact(false);
    wprintf(L"%s\n",json.emit());

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

    int i;
    int count_i;

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