Chilkat Examples

ChilkatHOMEAndroid™Classic ASPCC++C#Mono C#.NET Core C#C# UWP/WinRTDataFlexDelphi ActiveXDelphi DLLVisual FoxProJavaLianjaMFCObjective-CPerlPHP ActiveXPHP ExtensionPowerBuilderPowerShellPureBasicCkPythonChilkat2-PythonRubySQL ServerSwift 2Swift 3/4TclUnicode CUnicode C++Visual Basic 6.0VB.NETVB.NET UWP/WinRTVBScriptXojo PluginNode.jsExcelGo

Swift 3/4 Examples

ASN.1
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
Async
Azure Cloud Storage
Azure Service Bus
Base64
Bounced Email
Box
CSR
CSV
Certificates
Compression
DKIM / DomainKey
DSA
Diffie-Hellman
Digital Signatures
Dropbox
Dynamics CRM
ECC
Email Object
Encryption
FTP
Facebook
FileAccess
Firebase
GMail REST API
GeoOp
Google APIs
Google Calendar
Google Cloud Storage
Google Drive
Google Sheets
Gzip
HTML-to-XML/Text
HTTP
HTTP Misc
IMAP
JSON
JSON Web Encryption (JWE)
JSON Web Signatures (JWS)
JSON Web Token (JWT)

Java KeyStore (JKS)
Jira
MHT / HTML Email
MIME
Microsoft Graph
NTLM
OAuth1
OAuth2
OneDrive
OpenSSL
Outlook
PEM
PFX/P12
POP3
PRNG
PayPal
Peoplevox
QuickBooks
REST
REST Misc
RSA Encryption
SCP
SFTP
SMTP
SSH
SSH Key
SSH Tunnel
SharePoint
Shopify
Socket/SSL/TLS
Spider
Stream
Stripe
SugarCRM
Tar Archive
Twitter
Upload
VoiceBase
Walmart
WebSocket
XAdES
XML
XML Digital Signatures
XMP
Xero
Zip
curl
eBay

 

 

 

(Swift 3/4) Google Sheets - Create a New Spreadsheet

Demonstrates how to create a new and empty spreadsheet.

Chilkat Downloads for the Swift Programming Language

MAC OS X (Cocoa) Objective-C/Swift Libs

iOS Objective-C/Swift Libs

func chilkatTest() {
    //  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..
    let jsonToken = CkoJsonObject()!
    var success: Bool = jsonToken.loadFile("qa_data/tokens/googleSheets.json")
    if jsonToken.hasMember("access_token") == false {
        print("No access token found.")
        return
    }

    let http = CkoHttp()!
    http.authToken = jsonToken.string(of: "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:
    let json = CkoJsonObject()!
    json.update("sheets[0].properties.title", value: "Sample Tab")
    json.update("properties.title", value: "Create Spreadsheet using Sheets API v4")

    //  Send the POST to create the new Google spreadsheet.
    var resp: CkoHttpResponse? = http.postJson3("https://sheets.googleapis.com/v4/spreadsheets", contentType: "application/json", json: json)
    if http.lastMethodSuccess != true {
        print("\(http.lastErrorText!)")
        return
    }

    print("response status code = \(resp!.statusCode.intValue)")
    print("response JSON:")

    json.load(resp!.bodyStr)
    json.emitCompact = false
    print("\(json.emit()!)")

    resp = nil

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

    var i: Int
    var count_i: Int

    var spreadsheetId: String? = json.string(of: "spreadsheetId")
    var propertiesTitle: String? = json.string(of: "properties.title")
    var propertiesLocale: String? = json.string(of: "properties.locale")
    var propertiesAutoRecalc: String? = json.string(of: "properties.autoRecalc")
    var propertiesTimeZone: String? = json.string(of: "properties.timeZone")
    var propertiesDefaultFormatBackgroundColorRed: Int = json.int(of: "properties.defaultFormat.backgroundColor.red").intValue
    var propertiesDefaultFormatBackgroundColorGreen: Int = json.int(of: "properties.defaultFormat.backgroundColor.green").intValue
    var propertiesDefaultFormatBackgroundColorBlue: Int = json.int(of: "properties.defaultFormat.backgroundColor.blue").intValue
    var propertiesDefaultFormatPaddingTop: Int = json.int(of: "properties.defaultFormat.padding.top").intValue
    var propertiesDefaultFormatPaddingRight: Int = json.int(of: "properties.defaultFormat.padding.right").intValue
    var propertiesDefaultFormatPaddingBottom: Int = json.int(of: "properties.defaultFormat.padding.bottom").intValue
    var propertiesDefaultFormatPaddingLeft: Int = json.int(of: "properties.defaultFormat.padding.left").intValue
    var propertiesDefaultFormatVerticalAlignment: String? = json.string(of: "properties.defaultFormat.verticalAlignment")
    var propertiesDefaultFormatWrapStrategy: String? = json.string(of: "properties.defaultFormat.wrapStrategy")
    var propertiesDefaultFormatTextFormatFontFamily: String? = json.string(of: "properties.defaultFormat.textFormat.fontFamily")
    var propertiesDefaultFormatTextFormatFontSize: Int = json.int(of: "properties.defaultFormat.textFormat.fontSize").intValue
    var propertiesDefaultFormatTextFormatBold: Bool = json.bool(of: "properties.defaultFormat.textFormat.bold")
    var propertiesDefaultFormatTextFormatItalic: Bool = json.bool(of: "properties.defaultFormat.textFormat.italic")
    var propertiesDefaultFormatTextFormatStrikethrough: Bool = json.bool(of: "properties.defaultFormat.textFormat.strikethrough")
    var propertiesDefaultFormatTextFormatUnderline: Bool = json.bool(of: "properties.defaultFormat.textFormat.underline")
    var spreadsheetUrl: String? = json.string(of: "spreadsheetUrl")
    i = 0
    count_i = json.size(ofArray: "sheets").intValue
    while (i < count_i) {
        json.i = i
        var propertiesSheetId: Int = json.int(of: "sheets[i].properties.sheetId").intValue
        propertiesTitle = json.string(of: "sheets[i].properties.title")
        var propertiesIndex: Int = json.int(of: "sheets[i].properties.index").intValue
        var propertiesSheetType: String? = json.string(of: "sheets[i].properties.sheetType")
        var propertiesGridPropertiesRowCount: Int = json.int(of: "sheets[i].properties.gridProperties.rowCount").intValue
        var propertiesGridPropertiesColumnCount: Int = json.int(of: "sheets[i].properties.gridProperties.columnCount").intValue
        i = i + 1
    }


}

 

© 2000-2016 Chilkat Software, Inc. All Rights Reserved.