PureBasic
PureBasic
Google Sheets - Create a New Spreadsheet
See more Google Sheets Examples
Demonstrates how to create a new and empty spreadsheet.Chilkat PureBasic Downloads
IncludeFile "CkHttpResponse.pb"
IncludeFile "CkHttp.pb"
IncludeFile "CkJsonObject.pb"
Procedure ChilkatExample()
success.i = 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..
jsonToken.i = CkJsonObject::ckCreate()
If jsonToken.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
success = CkJsonObject::ckLoadFile(jsonToken,"qa_data/tokens/googleSheets.json")
If CkJsonObject::ckHasMember(jsonToken,"access_token") = 0
Debug "No access token found."
CkJsonObject::ckDispose(jsonToken)
ProcedureReturn
EndIf
http.i = CkHttp::ckCreate()
If http.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkHttp::setCkAuthToken(http, CkJsonObject::ckStringOf(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:
json.i = CkJsonObject::ckCreate()
If json.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkJsonObject::ckUpdateString(json,"sheets[0].properties.title","Sample Tab")
CkJsonObject::ckUpdateString(json,"properties.title","Create Spreadsheet using Sheets API v4")
; Send the POST to create the new Google spreadsheet.
resp.i = CkHttpResponse::ckCreate()
If resp.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
success = CkHttp::ckHttpJson(http,"POST","https://sheets.googleapis.com/v4/spreadsheets",json,"application/json",resp)
If success = 0
Debug CkHttp::ckLastErrorText(http)
CkJsonObject::ckDispose(jsonToken)
CkHttp::ckDispose(http)
CkJsonObject::ckDispose(json)
CkHttpResponse::ckDispose(resp)
ProcedureReturn
EndIf
Debug "response status code = " + Str(CkHttpResponse::ckStatusCode(resp))
Debug "response JSON:"
CkJsonObject::ckLoad(json,CkHttpResponse::ckBodyStr(resp))
CkJsonObject::setCkEmitCompact(json, 0)
Debug CkJsonObject::ckEmit(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"
; }
;
i.i
count_i.i
spreadsheetId.s = CkJsonObject::ckStringOf(json,"spreadsheetId")
propertiesTitle.s = CkJsonObject::ckStringOf(json,"properties.title")
propertiesLocale.s = CkJsonObject::ckStringOf(json,"properties.locale")
propertiesAutoRecalc.s = CkJsonObject::ckStringOf(json,"properties.autoRecalc")
propertiesTimeZone.s = CkJsonObject::ckStringOf(json,"properties.timeZone")
propertiesDefaultFormatBackgroundColorRed.i = CkJsonObject::ckIntOf(json,"properties.defaultFormat.backgroundColor.red")
propertiesDefaultFormatBackgroundColorGreen.i = CkJsonObject::ckIntOf(json,"properties.defaultFormat.backgroundColor.green")
propertiesDefaultFormatBackgroundColorBlue.i = CkJsonObject::ckIntOf(json,"properties.defaultFormat.backgroundColor.blue")
propertiesDefaultFormatPaddingTop.i = CkJsonObject::ckIntOf(json,"properties.defaultFormat.padding.top")
propertiesDefaultFormatPaddingRight.i = CkJsonObject::ckIntOf(json,"properties.defaultFormat.padding.right")
propertiesDefaultFormatPaddingBottom.i = CkJsonObject::ckIntOf(json,"properties.defaultFormat.padding.bottom")
propertiesDefaultFormatPaddingLeft.i = CkJsonObject::ckIntOf(json,"properties.defaultFormat.padding.left")
propertiesDefaultFormatVerticalAlignment.s = CkJsonObject::ckStringOf(json,"properties.defaultFormat.verticalAlignment")
propertiesDefaultFormatWrapStrategy.s = CkJsonObject::ckStringOf(json,"properties.defaultFormat.wrapStrategy")
propertiesDefaultFormatTextFormatFontFamily.s = CkJsonObject::ckStringOf(json,"properties.defaultFormat.textFormat.fontFamily")
propertiesDefaultFormatTextFormatFontSize.i = CkJsonObject::ckIntOf(json,"properties.defaultFormat.textFormat.fontSize")
propertiesDefaultFormatTextFormatBold.i = CkJsonObject::ckBoolOf(json,"properties.defaultFormat.textFormat.bold")
propertiesDefaultFormatTextFormatItalic.i = CkJsonObject::ckBoolOf(json,"properties.defaultFormat.textFormat.italic")
propertiesDefaultFormatTextFormatStrikethrough.i = CkJsonObject::ckBoolOf(json,"properties.defaultFormat.textFormat.strikethrough")
propertiesDefaultFormatTextFormatUnderline.i = CkJsonObject::ckBoolOf(json,"properties.defaultFormat.textFormat.underline")
spreadsheetUrl.s = CkJsonObject::ckStringOf(json,"spreadsheetUrl")
i = 0
count_i = CkJsonObject::ckSizeOfArray(json,"sheets")
While (i < count_i)
CkJsonObject::setCkI(json, i)
propertiesSheetId.i = CkJsonObject::ckIntOf(json,"sheets[i].properties.sheetId")
propertiesTitle = CkJsonObject::ckStringOf(json,"sheets[i].properties.title")
propertiesIndex.i = CkJsonObject::ckIntOf(json,"sheets[i].properties.index")
propertiesSheetType.s = CkJsonObject::ckStringOf(json,"sheets[i].properties.sheetType")
propertiesGridPropertiesRowCount.i = CkJsonObject::ckIntOf(json,"sheets[i].properties.gridProperties.rowCount")
propertiesGridPropertiesColumnCount.i = CkJsonObject::ckIntOf(json,"sheets[i].properties.gridProperties.columnCount")
i = i + 1
Wend
CkJsonObject::ckDispose(jsonToken)
CkHttp::ckDispose(http)
CkJsonObject::ckDispose(json)
CkHttpResponse::ckDispose(resp)
ProcedureReturn
EndProcedure