Sample code for 30+ languages & platforms
Xojo Plugin

Google Sheets - Create a New Spreadsheet

See more Google Sheets Examples

Demonstrates how to create a new and empty spreadsheet.

Chilkat Xojo Plugin Downloads

Xojo Plugin
Dim success As Boolean
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..
Dim jsonToken As New Chilkat.JsonObject
success = jsonToken.LoadFile("qa_data/tokens/googleSheets.json")
If (jsonToken.HasMember("access_token") = False) Then
    System.DebugLog("No access token found.")
    Return
End If

Dim http As New Chilkat.Http
http.AuthToken = jsonToken.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:
Dim json As New Chilkat.JsonObject
success = json.UpdateString("sheets[0].properties.title","Sample Tab")
success = json.UpdateString("properties.title","Create Spreadsheet using Sheets API v4")

// Send the POST to create the new Google spreadsheet.
Dim resp As New Chilkat.HttpResponse
success = http.HttpJson("POST","https://sheets.googleapis.com/v4/spreadsheets",json,"application/json",resp)
If (success = False) Then
    System.DebugLog(http.LastErrorText)
    Return
End If

System.DebugLog("response status code = " + Str(resp.StatusCode))
System.DebugLog("response JSON:")

success = json.Load(resp.BodyStr)
json.EmitCompact = False
System.DebugLog(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"
// }
// 

Dim i As Int32
Dim count_i As Int32

Dim spreadsheetId As String
spreadsheetId = json.StringOf("spreadsheetId")
Dim propertiesTitle As String
propertiesTitle = json.StringOf("properties.title")
Dim propertiesLocale As String
propertiesLocale = json.StringOf("properties.locale")
Dim propertiesAutoRecalc As String
propertiesAutoRecalc = json.StringOf("properties.autoRecalc")
Dim propertiesTimeZone As String
propertiesTimeZone = json.StringOf("properties.timeZone")
Dim propertiesDefaultFormatBackgroundColorRed As Int32
propertiesDefaultFormatBackgroundColorRed = json.IntOf("properties.defaultFormat.backgroundColor.red")
Dim propertiesDefaultFormatBackgroundColorGreen As Int32
propertiesDefaultFormatBackgroundColorGreen = json.IntOf("properties.defaultFormat.backgroundColor.green")
Dim propertiesDefaultFormatBackgroundColorBlue As Int32
propertiesDefaultFormatBackgroundColorBlue = json.IntOf("properties.defaultFormat.backgroundColor.blue")
Dim propertiesDefaultFormatPaddingTop As Int32
propertiesDefaultFormatPaddingTop = json.IntOf("properties.defaultFormat.padding.top")
Dim propertiesDefaultFormatPaddingRight As Int32
propertiesDefaultFormatPaddingRight = json.IntOf("properties.defaultFormat.padding.right")
Dim propertiesDefaultFormatPaddingBottom As Int32
propertiesDefaultFormatPaddingBottom = json.IntOf("properties.defaultFormat.padding.bottom")
Dim propertiesDefaultFormatPaddingLeft As Int32
propertiesDefaultFormatPaddingLeft = json.IntOf("properties.defaultFormat.padding.left")
Dim propertiesDefaultFormatVerticalAlignment As String
propertiesDefaultFormatVerticalAlignment = json.StringOf("properties.defaultFormat.verticalAlignment")
Dim propertiesDefaultFormatWrapStrategy As String
propertiesDefaultFormatWrapStrategy = json.StringOf("properties.defaultFormat.wrapStrategy")
Dim propertiesDefaultFormatTextFormatFontFamily As String
propertiesDefaultFormatTextFormatFontFamily = json.StringOf("properties.defaultFormat.textFormat.fontFamily")
Dim propertiesDefaultFormatTextFormatFontSize As Int32
propertiesDefaultFormatTextFormatFontSize = json.IntOf("properties.defaultFormat.textFormat.fontSize")
Dim propertiesDefaultFormatTextFormatBold As Boolean
propertiesDefaultFormatTextFormatBold = json.BoolOf("properties.defaultFormat.textFormat.bold")
Dim propertiesDefaultFormatTextFormatItalic As Boolean
propertiesDefaultFormatTextFormatItalic = json.BoolOf("properties.defaultFormat.textFormat.italic")
Dim propertiesDefaultFormatTextFormatStrikethrough As Boolean
propertiesDefaultFormatTextFormatStrikethrough = json.BoolOf("properties.defaultFormat.textFormat.strikethrough")
Dim propertiesDefaultFormatTextFormatUnderline As Boolean
propertiesDefaultFormatTextFormatUnderline = json.BoolOf("properties.defaultFormat.textFormat.underline")
Dim spreadsheetUrl As String
spreadsheetUrl = json.StringOf("spreadsheetUrl")
i = 0
count_i = json.SizeOfArray("sheets")
While (i < count_i)
    json.I = i
    Dim propertiesSheetId As Int32
    propertiesSheetId = json.IntOf("sheets[i].properties.sheetId")
    propertiesTitle = json.StringOf("sheets[i].properties.title")
    Dim propertiesIndex As Int32
    propertiesIndex = json.IntOf("sheets[i].properties.index")
    Dim propertiesSheetType As String
    propertiesSheetType = json.StringOf("sheets[i].properties.sheetType")
    Dim propertiesGridPropertiesRowCount As Int32
    propertiesGridPropertiesRowCount = json.IntOf("sheets[i].properties.gridProperties.rowCount")
    Dim propertiesGridPropertiesColumnCount As Int32
    propertiesGridPropertiesColumnCount = json.IntOf("sheets[i].properties.gridProperties.columnCount")
    i = i + 1
Wend