Sample code for 30+ languages & platforms
AutoIt

Google Sheets - Create a New Spreadsheet

See more Google Sheets Examples

Demonstrates how to create a new and empty spreadsheet.

Chilkat AutoIt Downloads

AutoIt
Local $bSuccess = 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..
$oJsonToken = ObjCreate("Chilkat.JsonObject")
$bSuccess = $oJsonToken.LoadFile("qa_data/tokens/googleSheets.json")
If ($oJsonToken.HasMember("access_token") = False) Then
    ConsoleWrite("No access token found." & @CRLF)
    Exit
EndIf

$oHttp = ObjCreate("Chilkat.Http")
$oHttp.AuthToken = $oJsonToken.StringOf("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:
$oJson = ObjCreate("Chilkat.JsonObject")
$oJson.UpdateString("sheets[0].properties.title","Sample Tab")
$oJson.UpdateString("properties.title","Create Spreadsheet using Sheets API v4")

; Send the POST to create the new Google spreadsheet.
$oResp = ObjCreate("Chilkat.HttpResponse")
$bSuccess = $oHttp.HttpJson("POST","https://sheets.googleapis.com/v4/spreadsheets",$oJson,"application/json",$oResp)
If ($bSuccess = False) Then
    ConsoleWrite($oHttp.LastErrorText & @CRLF)
    Exit
EndIf

ConsoleWrite("response status code = " & $oResp.StatusCode & @CRLF)
ConsoleWrite("response JSON:" & @CRLF)

$oJson.Load($oResp.BodyStr)
$oJson.EmitCompact = False
ConsoleWrite($oJson.Emit() & @CRLF)

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

Local $i
Local $iCount_i

Local $spreadsheetId = $oJson.StringOf("spreadsheetId")
Local $sPropertiesTitle = $oJson.StringOf("properties.title")
Local $sPropertiesLocale = $oJson.StringOf("properties.locale")
Local $sPropertiesAutoRecalc = $oJson.StringOf("properties.autoRecalc")
Local $sPropertiesTimeZone = $oJson.StringOf("properties.timeZone")
Local $iPropertiesDefaultFormatBackgroundColorRed = $oJson.IntOf("properties.defaultFormat.backgroundColor.red")
Local $iPropertiesDefaultFormatBackgroundColorGreen = $oJson.IntOf("properties.defaultFormat.backgroundColor.green")
Local $iPropertiesDefaultFormatBackgroundColorBlue = $oJson.IntOf("properties.defaultFormat.backgroundColor.blue")
Local $iPropertiesDefaultFormatPaddingTop = $oJson.IntOf("properties.defaultFormat.padding.top")
Local $iPropertiesDefaultFormatPaddingRight = $oJson.IntOf("properties.defaultFormat.padding.right")
Local $iPropertiesDefaultFormatPaddingBottom = $oJson.IntOf("properties.defaultFormat.padding.bottom")
Local $iPropertiesDefaultFormatPaddingLeft = $oJson.IntOf("properties.defaultFormat.padding.left")
Local $sPropertiesDefaultFormatVerticalAlignment = $oJson.StringOf("properties.defaultFormat.verticalAlignment")
Local $sPropertiesDefaultFormatWrapStrategy = $oJson.StringOf("properties.defaultFormat.wrapStrategy")
Local $sPropertiesDefaultFormatTextFormatFontFamily = $oJson.StringOf("properties.defaultFormat.textFormat.fontFamily")
Local $iPropertiesDefaultFormatTextFormatFontSize = $oJson.IntOf("properties.defaultFormat.textFormat.fontSize")
Local $bPropertiesDefaultFormatTextFormatBold = $oJson.BoolOf("properties.defaultFormat.textFormat.bold")
Local $bPropertiesDefaultFormatTextFormatItalic = $oJson.BoolOf("properties.defaultFormat.textFormat.italic")
Local $bPropertiesDefaultFormatTextFormatStrikethrough = $oJson.BoolOf("properties.defaultFormat.textFormat.strikethrough")
Local $bPropertiesDefaultFormatTextFormatUnderline = $oJson.BoolOf("properties.defaultFormat.textFormat.underline")
Local $spreadsheetUrl = $oJson.StringOf("spreadsheetUrl")
$i = 0
$iCount_i = $oJson.SizeOfArray("sheets")
While ($i < $iCount_i)
    $oJson.I = $i
Local $iPropertiesSheetId = $oJson.IntOf("sheets[i].properties.sheetId")
    $sPropertiesTitle = $oJson.StringOf("sheets[i].properties.title")
Local $iPropertiesIndex = $oJson.IntOf("sheets[i].properties.index")
Local $sPropertiesSheetType = $oJson.StringOf("sheets[i].properties.sheetType")
Local $iPropertiesGridPropertiesRowCount = $oJson.IntOf("sheets[i].properties.gridProperties.rowCount")
Local $iPropertiesGridPropertiesColumnCount = $oJson.IntOf("sheets[i].properties.gridProperties.columnCount")
    $i = $i + 1
Wend