Sample code for 30+ languages & platforms
Xbase++

Google Sheets - Create a New Spreadsheet

See more Google Sheets Examples

Demonstrates how to create a new and empty spreadsheet.

Chilkat Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oJsonToken
LOCAL oHttp
LOCAL oJson
LOCAL oResp
LOCAL i
LOCAL nCount_i
LOCAL cSpreadsheetId
LOCAL cPropertiesTitle
LOCAL cPropertiesLocale
LOCAL cPropertiesAutoRecalc
LOCAL cPropertiesTimeZone
LOCAL nPropertiesDefaultFormatBackgroundColorRed
LOCAL nPropertiesDefaultFormatBackgroundColorGreen
LOCAL nPropertiesDefaultFormatBackgroundColorBlue
LOCAL nPropertiesDefaultFormatPaddingTop
LOCAL nPropertiesDefaultFormatPaddingRight
LOCAL nPropertiesDefaultFormatPaddingBottom
LOCAL nPropertiesDefaultFormatPaddingLeft
LOCAL cPropertiesDefaultFormatVerticalAlignment
LOCAL cPropertiesDefaultFormatWrapStrategy
LOCAL cPropertiesDefaultFormatTextFormatFontFamily
LOCAL nPropertiesDefaultFormatTextFormatFontSize
LOCAL nPropertiesDefaultFormatTextFormatBold
LOCAL nPropertiesDefaultFormatTextFormatItalic
LOCAL nPropertiesDefaultFormatTextFormatStrikethrough
LOCAL nPropertiesDefaultFormatTextFormatUnderline
LOCAL cSpreadsheetUrl
LOCAL nPropertiesSheetId
LOCAL nPropertiesIndex
LOCAL cPropertiesSheetType
LOCAL nPropertiesGridPropertiesRowCount
LOCAL nPropertiesGridPropertiesColumnCount

nSuccess := 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..
oJsonToken := CreateObject("Chilkat.JsonObject")
nSuccess := oJsonToken:LoadFile("qa_data/tokens/googleSheets.json")
IF (oJsonToken:HasMember("access_token") == 0)
    ? "No access token found."
    oJsonToken:destroy()
    RETURN
ENDIF

oHttp := CreateObject("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 := CreateObject("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 := CreateObject("Chilkat.HttpResponse")
nSuccess := oHttp:HttpJson("POST", "https://sheets.googleapis.com/v4/spreadsheets", oJson, "application/json", oResp)
IF (nSuccess == 0)
    ? oHttp:LastErrorText
    oJsonToken:destroy()
    oHttp:destroy()
    oJson:destroy()
    oResp:destroy()
    RETURN
ENDIF

? "response status code = " + Str(oResp:StatusCode)
? "response JSON:"

oJson:Load(oResp:BodyStr)
oJson:EmitCompact := 0
? oJson: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"
//  }
//  

cSpreadsheetId := oJson:StringOf("spreadsheetId")
cPropertiesTitle := oJson:StringOf("properties.title")
cPropertiesLocale := oJson:StringOf("properties.locale")
cPropertiesAutoRecalc := oJson:StringOf("properties.autoRecalc")
cPropertiesTimeZone := oJson:StringOf("properties.timeZone")
nPropertiesDefaultFormatBackgroundColorRed := oJson:IntOf("properties.defaultFormat.backgroundColor.red")
nPropertiesDefaultFormatBackgroundColorGreen := oJson:IntOf("properties.defaultFormat.backgroundColor.green")
nPropertiesDefaultFormatBackgroundColorBlue := oJson:IntOf("properties.defaultFormat.backgroundColor.blue")
nPropertiesDefaultFormatPaddingTop := oJson:IntOf("properties.defaultFormat.padding.top")
nPropertiesDefaultFormatPaddingRight := oJson:IntOf("properties.defaultFormat.padding.right")
nPropertiesDefaultFormatPaddingBottom := oJson:IntOf("properties.defaultFormat.padding.bottom")
nPropertiesDefaultFormatPaddingLeft := oJson:IntOf("properties.defaultFormat.padding.left")
cPropertiesDefaultFormatVerticalAlignment := oJson:StringOf("properties.defaultFormat.verticalAlignment")
cPropertiesDefaultFormatWrapStrategy := oJson:StringOf("properties.defaultFormat.wrapStrategy")
cPropertiesDefaultFormatTextFormatFontFamily := oJson:StringOf("properties.defaultFormat.textFormat.fontFamily")
nPropertiesDefaultFormatTextFormatFontSize := oJson:IntOf("properties.defaultFormat.textFormat.fontSize")
nPropertiesDefaultFormatTextFormatBold := oJson:BoolOf("properties.defaultFormat.textFormat.bold")
nPropertiesDefaultFormatTextFormatItalic := oJson:BoolOf("properties.defaultFormat.textFormat.italic")
nPropertiesDefaultFormatTextFormatStrikethrough := oJson:BoolOf("properties.defaultFormat.textFormat.strikethrough")
nPropertiesDefaultFormatTextFormatUnderline := oJson:BoolOf("properties.defaultFormat.textFormat.underline")
cSpreadsheetUrl := oJson:StringOf("spreadsheetUrl")
i := 0
nCount_i := oJson:SizeOfArray("sheets")
DO WHILE (i < nCount_i)
    oJson:I := i
    nPropertiesSheetId := oJson:IntOf("sheets[i].properties.sheetId")
    cPropertiesTitle := oJson:StringOf("sheets[i].properties.title")
    nPropertiesIndex := oJson:IntOf("sheets[i].properties.index")
    cPropertiesSheetType := oJson:StringOf("sheets[i].properties.sheetType")
    nPropertiesGridPropertiesRowCount := oJson:IntOf("sheets[i].properties.gridProperties.rowCount")
    nPropertiesGridPropertiesColumnCount := oJson:IntOf("sheets[i].properties.gridProperties.columnCount")
    i := i + 1
ENDDO

oJsonToken:destroy()
oHttp:destroy()
oJson:destroy()
oResp:destroy()