Sample code for 30+ languages & platforms
Tcl

Google Sheets - Create a New Spreadsheet

See more Google Sheets Examples

Demonstrates how to create a new and empty spreadsheet.

Chilkat Tcl Downloads

Tcl

load ./chilkat.dll

set success 0

# 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..
set jsonToken [new_CkJsonObject]

set success [CkJsonObject_LoadFile $jsonToken "qa_data/tokens/googleSheets.json"]
if {[CkJsonObject_HasMember $jsonToken "access_token"] == 0} then {
    puts "No access token found."
    delete_CkJsonObject $jsonToken
    exit
}

set http [new_CkHttp]

CkHttp_put_AuthToken $http [CkJsonObject_stringOf $jsonToken "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:
set json [new_CkJsonObject]

CkJsonObject_UpdateString $json "sheets[0].properties.title" "Sample Tab"
CkJsonObject_UpdateString $json "properties.title" "Create Spreadsheet using Sheets API v4"

# Send the POST to create the new Google spreadsheet.
set resp [new_CkHttpResponse]

set success [CkHttp_HttpJson $http "POST" "https://sheets.googleapis.com/v4/spreadsheets" $json "application/json" $resp]
if {$success == 0} then {
    puts [CkHttp_lastErrorText $http]
    delete_CkJsonObject $jsonToken
    delete_CkHttp $http
    delete_CkJsonObject $json
    delete_CkHttpResponse $resp
    exit
}

puts "response status code = [CkHttpResponse_get_StatusCode $resp]"
puts "response JSON:"

CkJsonObject_Load $json [CkHttpResponse_bodyStr $resp]
CkJsonObject_put_EmitCompact $json 0
puts [CkJsonObject_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"
# }
# 

set spreadsheetId [CkJsonObject_stringOf $json "spreadsheetId"]
set propertiesTitle [CkJsonObject_stringOf $json "properties.title"]
set propertiesLocale [CkJsonObject_stringOf $json "properties.locale"]
set propertiesAutoRecalc [CkJsonObject_stringOf $json "properties.autoRecalc"]
set propertiesTimeZone [CkJsonObject_stringOf $json "properties.timeZone"]
set propertiesDefaultFormatBackgroundColorRed [CkJsonObject_IntOf $json "properties.defaultFormat.backgroundColor.red"]
set propertiesDefaultFormatBackgroundColorGreen [CkJsonObject_IntOf $json "properties.defaultFormat.backgroundColor.green"]
set propertiesDefaultFormatBackgroundColorBlue [CkJsonObject_IntOf $json "properties.defaultFormat.backgroundColor.blue"]
set propertiesDefaultFormatPaddingTop [CkJsonObject_IntOf $json "properties.defaultFormat.padding.top"]
set propertiesDefaultFormatPaddingRight [CkJsonObject_IntOf $json "properties.defaultFormat.padding.right"]
set propertiesDefaultFormatPaddingBottom [CkJsonObject_IntOf $json "properties.defaultFormat.padding.bottom"]
set propertiesDefaultFormatPaddingLeft [CkJsonObject_IntOf $json "properties.defaultFormat.padding.left"]
set propertiesDefaultFormatVerticalAlignment [CkJsonObject_stringOf $json "properties.defaultFormat.verticalAlignment"]
set propertiesDefaultFormatWrapStrategy [CkJsonObject_stringOf $json "properties.defaultFormat.wrapStrategy"]
set propertiesDefaultFormatTextFormatFontFamily [CkJsonObject_stringOf $json "properties.defaultFormat.textFormat.fontFamily"]
set propertiesDefaultFormatTextFormatFontSize [CkJsonObject_IntOf $json "properties.defaultFormat.textFormat.fontSize"]
set propertiesDefaultFormatTextFormatBold [CkJsonObject_BoolOf $json "properties.defaultFormat.textFormat.bold"]
set propertiesDefaultFormatTextFormatItalic [CkJsonObject_BoolOf $json "properties.defaultFormat.textFormat.italic"]
set propertiesDefaultFormatTextFormatStrikethrough [CkJsonObject_BoolOf $json "properties.defaultFormat.textFormat.strikethrough"]
set propertiesDefaultFormatTextFormatUnderline [CkJsonObject_BoolOf $json "properties.defaultFormat.textFormat.underline"]
set spreadsheetUrl [CkJsonObject_stringOf $json "spreadsheetUrl"]
set i 0
set count_i [CkJsonObject_SizeOfArray $json "sheets"]
while {$i < $count_i} {
    CkJsonObject_put_I $json $i
    set propertiesSheetId [CkJsonObject_IntOf $json "sheets[i].properties.sheetId"]
    set propertiesTitle [CkJsonObject_stringOf $json "sheets[i].properties.title"]
    set propertiesIndex [CkJsonObject_IntOf $json "sheets[i].properties.index"]
    set propertiesSheetType [CkJsonObject_stringOf $json "sheets[i].properties.sheetType"]
    set propertiesGridPropertiesRowCount [CkJsonObject_IntOf $json "sheets[i].properties.gridProperties.rowCount"]
    set propertiesGridPropertiesColumnCount [CkJsonObject_IntOf $json "sheets[i].properties.gridProperties.columnCount"]
    set i [expr $i + 1]
}

delete_CkJsonObject $jsonToken
delete_CkHttp $http
delete_CkJsonObject $json
delete_CkHttpResponse $resp