Chilkat HOME .NET Core C# Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi ActiveX Delphi DLL Go Java Lianja Mono C# Node.js Objective-C PHP ActiveX PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(SQL Server) Google Sheets - Create a New SpreadsheetDemonstrates how to create a new and empty spreadsheet.
-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls. -- CREATE PROCEDURE ChilkatSample AS BEGIN DECLARE @hr int DECLARE @iTmp0 int -- Important: Do not use nvarchar(max). See the warning about using nvarchar(max). DECLARE @sTmp0 nvarchar(4000) -- 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.. DECLARE @jsonToken int -- Use "Chilkat_9_5_0.JsonObject" for versions of Chilkat < 10.0.0 EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @jsonToken OUT IF @hr <> 0 BEGIN PRINT 'Failed to create ActiveX component' RETURN END DECLARE @success int EXEC sp_OAMethod @jsonToken, 'LoadFile', @success OUT, 'qa_data/tokens/googleSheets.json' EXEC sp_OAMethod @jsonToken, 'HasMember', @iTmp0 OUT, 'access_token' IF @iTmp0 = 0 BEGIN PRINT 'No access token found.' EXEC @hr = sp_OADestroy @jsonToken RETURN END DECLARE @http int -- Use "Chilkat_9_5_0.Http" for versions of Chilkat < 10.0.0 EXEC @hr = sp_OACreate 'Chilkat.Http', @http OUT EXEC sp_OAMethod @jsonToken, 'StringOf', @sTmp0 OUT, 'access_token' EXEC sp_OASetProperty @http, 'AuthToken', @sTmp0 -- 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: DECLARE @json int -- Use "Chilkat_9_5_0.JsonObject" for versions of Chilkat < 10.0.0 EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @json OUT EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'sheets[0].properties.title', 'Sample Tab' EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'properties.title', 'Create Spreadsheet using Sheets API v4' -- Send the POST to create the new Google spreadsheet. DECLARE @resp int EXEC sp_OAMethod @http, 'PostJson3', @resp OUT, 'https://sheets.googleapis.com/v4/spreadsheets', 'application/json', @json EXEC sp_OAGetProperty @http, 'LastMethodSuccess', @iTmp0 OUT IF @iTmp0 <> 1 BEGIN EXEC sp_OAGetProperty @http, 'LastErrorText', @sTmp0 OUT PRINT @sTmp0 EXEC @hr = sp_OADestroy @jsonToken EXEC @hr = sp_OADestroy @http EXEC @hr = sp_OADestroy @json RETURN END EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT PRINT 'response status code = ' + @iTmp0 PRINT 'response JSON:' EXEC sp_OAGetProperty @resp, 'BodyStr', @sTmp0 OUT EXEC sp_OAMethod @json, 'Load', @success OUT, @sTmp0 EXEC sp_OASetProperty @json, 'EmitCompact', 0 EXEC sp_OAMethod @json, 'Emit', @sTmp0 OUT PRINT @sTmp0 EXEC @hr = sp_OADestroy @resp -- 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" -- } -- DECLARE @i int DECLARE @count_i int DECLARE @spreadsheetId nvarchar(4000) EXEC sp_OAMethod @json, 'StringOf', @spreadsheetId OUT, 'spreadsheetId' DECLARE @propertiesTitle nvarchar(4000) EXEC sp_OAMethod @json, 'StringOf', @propertiesTitle OUT, 'properties.title' DECLARE @propertiesLocale nvarchar(4000) EXEC sp_OAMethod @json, 'StringOf', @propertiesLocale OUT, 'properties.locale' DECLARE @propertiesAutoRecalc nvarchar(4000) EXEC sp_OAMethod @json, 'StringOf', @propertiesAutoRecalc OUT, 'properties.autoRecalc' DECLARE @propertiesTimeZone nvarchar(4000) EXEC sp_OAMethod @json, 'StringOf', @propertiesTimeZone OUT, 'properties.timeZone' DECLARE @propertiesDefaultFormatBackgroundColorRed int EXEC sp_OAMethod @json, 'IntOf', @propertiesDefaultFormatBackgroundColorRed OUT, 'properties.defaultFormat.backgroundColor.red' DECLARE @propertiesDefaultFormatBackgroundColorGreen int EXEC sp_OAMethod @json, 'IntOf', @propertiesDefaultFormatBackgroundColorGreen OUT, 'properties.defaultFormat.backgroundColor.green' DECLARE @propertiesDefaultFormatBackgroundColorBlue int EXEC sp_OAMethod @json, 'IntOf', @propertiesDefaultFormatBackgroundColorBlue OUT, 'properties.defaultFormat.backgroundColor.blue' DECLARE @propertiesDefaultFormatPaddingTop int EXEC sp_OAMethod @json, 'IntOf', @propertiesDefaultFormatPaddingTop OUT, 'properties.defaultFormat.padding.top' DECLARE @propertiesDefaultFormatPaddingRight int EXEC sp_OAMethod @json, 'IntOf', @propertiesDefaultFormatPaddingRight OUT, 'properties.defaultFormat.padding.right' DECLARE @propertiesDefaultFormatPaddingBottom int EXEC sp_OAMethod @json, 'IntOf', @propertiesDefaultFormatPaddingBottom OUT, 'properties.defaultFormat.padding.bottom' DECLARE @propertiesDefaultFormatPaddingLeft int EXEC sp_OAMethod @json, 'IntOf', @propertiesDefaultFormatPaddingLeft OUT, 'properties.defaultFormat.padding.left' DECLARE @propertiesDefaultFormatVerticalAlignment nvarchar(4000) EXEC sp_OAMethod @json, 'StringOf', @propertiesDefaultFormatVerticalAlignment OUT, 'properties.defaultFormat.verticalAlignment' DECLARE @propertiesDefaultFormatWrapStrategy nvarchar(4000) EXEC sp_OAMethod @json, 'StringOf', @propertiesDefaultFormatWrapStrategy OUT, 'properties.defaultFormat.wrapStrategy' DECLARE @propertiesDefaultFormatTextFormatFontFamily nvarchar(4000) EXEC sp_OAMethod @json, 'StringOf', @propertiesDefaultFormatTextFormatFontFamily OUT, 'properties.defaultFormat.textFormat.fontFamily' DECLARE @propertiesDefaultFormatTextFormatFontSize int EXEC sp_OAMethod @json, 'IntOf', @propertiesDefaultFormatTextFormatFontSize OUT, 'properties.defaultFormat.textFormat.fontSize' DECLARE @propertiesDefaultFormatTextFormatBold int EXEC sp_OAMethod @json, 'BoolOf', @propertiesDefaultFormatTextFormatBold OUT, 'properties.defaultFormat.textFormat.bold' DECLARE @propertiesDefaultFormatTextFormatItalic int EXEC sp_OAMethod @json, 'BoolOf', @propertiesDefaultFormatTextFormatItalic OUT, 'properties.defaultFormat.textFormat.italic' DECLARE @propertiesDefaultFormatTextFormatStrikethrough int EXEC sp_OAMethod @json, 'BoolOf', @propertiesDefaultFormatTextFormatStrikethrough OUT, 'properties.defaultFormat.textFormat.strikethrough' DECLARE @propertiesDefaultFormatTextFormatUnderline int EXEC sp_OAMethod @json, 'BoolOf', @propertiesDefaultFormatTextFormatUnderline OUT, 'properties.defaultFormat.textFormat.underline' DECLARE @spreadsheetUrl nvarchar(4000) EXEC sp_OAMethod @json, 'StringOf', @spreadsheetUrl OUT, 'spreadsheetUrl' SELECT @i = 0 EXEC sp_OAMethod @json, 'SizeOfArray', @count_i OUT, 'sheets' WHILE (@i < @count_i) BEGIN EXEC sp_OASetProperty @json, 'I', @i DECLARE @propertiesSheetId int EXEC sp_OAMethod @json, 'IntOf', @propertiesSheetId OUT, 'sheets[i].properties.sheetId' EXEC sp_OAMethod @json, 'StringOf', @propertiesTitle OUT, 'sheets[i].properties.title' DECLARE @propertiesIndex int EXEC sp_OAMethod @json, 'IntOf', @propertiesIndex OUT, 'sheets[i].properties.index' DECLARE @propertiesSheetType nvarchar(4000) EXEC sp_OAMethod @json, 'StringOf', @propertiesSheetType OUT, 'sheets[i].properties.sheetType' DECLARE @propertiesGridPropertiesRowCount int EXEC sp_OAMethod @json, 'IntOf', @propertiesGridPropertiesRowCount OUT, 'sheets[i].properties.gridProperties.rowCount' DECLARE @propertiesGridPropertiesColumnCount int EXEC sp_OAMethod @json, 'IntOf', @propertiesGridPropertiesColumnCount OUT, 'sheets[i].properties.gridProperties.columnCount' SELECT @i = @i + 1 END EXEC @hr = sp_OADestroy @jsonToken EXEC @hr = sp_OADestroy @http EXEC @hr = sp_OADestroy @json END GO |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.