Sample code for 30+ languages & platforms
Delphi DLL

Google Sheets - Create a New Spreadsheet

See more Google Sheets Examples

Demonstrates how to create a new and empty spreadsheet.

Chilkat Delphi DLL Downloads

Delphi DLL
uses
    Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
    Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Http, HttpResponse, JsonObject;

...

procedure TForm1.Button1Click(Sender: TObject);
var
success: Boolean;
jsonToken: HCkJsonObject;
http: HCkHttp;
json: HCkJsonObject;
resp: HCkHttpResponse;
i: Integer;
count_i: Integer;
spreadsheetId: PWideChar;
propertiesTitle: PWideChar;
propertiesLocale: PWideChar;
propertiesAutoRecalc: PWideChar;
propertiesTimeZone: PWideChar;
propertiesDefaultFormatBackgroundColorRed: Integer;
propertiesDefaultFormatBackgroundColorGreen: Integer;
propertiesDefaultFormatBackgroundColorBlue: Integer;
propertiesDefaultFormatPaddingTop: Integer;
propertiesDefaultFormatPaddingRight: Integer;
propertiesDefaultFormatPaddingBottom: Integer;
propertiesDefaultFormatPaddingLeft: Integer;
propertiesDefaultFormatVerticalAlignment: PWideChar;
propertiesDefaultFormatWrapStrategy: PWideChar;
propertiesDefaultFormatTextFormatFontFamily: PWideChar;
propertiesDefaultFormatTextFormatFontSize: Integer;
propertiesDefaultFormatTextFormatBold: Boolean;
propertiesDefaultFormatTextFormatItalic: Boolean;
propertiesDefaultFormatTextFormatStrikethrough: Boolean;
propertiesDefaultFormatTextFormatUnderline: Boolean;
spreadsheetUrl: PWideChar;
propertiesSheetId: Integer;
propertiesIndex: Integer;
propertiesSheetType: PWideChar;
propertiesGridPropertiesRowCount: Integer;
propertiesGridPropertiesColumnCount: Integer;

begin
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..
jsonToken := CkJsonObject_Create();
success := CkJsonObject_LoadFile(jsonToken,'qa_data/tokens/googleSheets.json');
if (CkJsonObject_HasMember(jsonToken,'access_token') = False) then
  begin
    Memo1.Lines.Add('No access token found.');
    Exit;
  end;
http := CkHttp_Create();
CkHttp_putAuthToken(http,CkJsonObject__stringOf(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 := CkJsonObject_Create();
CkJsonObject_UpdateString(json,'sheets[0].properties.title','Sample Tab');
CkJsonObject_UpdateString(json,'properties.title','Create Spreadsheet using Sheets API v4');

// Send the POST to create the new Google spreadsheet.
resp := CkHttpResponse_Create();
success := CkHttp_HttpJson(http,'POST','https://sheets.googleapis.com/v4/spreadsheets',json,'application/json',resp);
if (success = False) then
  begin
    Memo1.Lines.Add(CkHttp__lastErrorText(http));
    Exit;
  end;

Memo1.Lines.Add('response status code = ' + IntToStr(CkHttpResponse_getStatusCode(resp)));
Memo1.Lines.Add('response JSON:');

CkJsonObject_Load(json,CkHttpResponse__bodyStr(resp));
CkJsonObject_putEmitCompact(json,False);
Memo1.Lines.Add(CkJsonObject__emit(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"
// }
// 

spreadsheetId := CkJsonObject__stringOf(json,'spreadsheetId');
propertiesTitle := CkJsonObject__stringOf(json,'properties.title');
propertiesLocale := CkJsonObject__stringOf(json,'properties.locale');
propertiesAutoRecalc := CkJsonObject__stringOf(json,'properties.autoRecalc');
propertiesTimeZone := CkJsonObject__stringOf(json,'properties.timeZone');
propertiesDefaultFormatBackgroundColorRed := CkJsonObject_IntOf(json,'properties.defaultFormat.backgroundColor.red');
propertiesDefaultFormatBackgroundColorGreen := CkJsonObject_IntOf(json,'properties.defaultFormat.backgroundColor.green');
propertiesDefaultFormatBackgroundColorBlue := CkJsonObject_IntOf(json,'properties.defaultFormat.backgroundColor.blue');
propertiesDefaultFormatPaddingTop := CkJsonObject_IntOf(json,'properties.defaultFormat.padding.top');
propertiesDefaultFormatPaddingRight := CkJsonObject_IntOf(json,'properties.defaultFormat.padding.right');
propertiesDefaultFormatPaddingBottom := CkJsonObject_IntOf(json,'properties.defaultFormat.padding.bottom');
propertiesDefaultFormatPaddingLeft := CkJsonObject_IntOf(json,'properties.defaultFormat.padding.left');
propertiesDefaultFormatVerticalAlignment := CkJsonObject__stringOf(json,'properties.defaultFormat.verticalAlignment');
propertiesDefaultFormatWrapStrategy := CkJsonObject__stringOf(json,'properties.defaultFormat.wrapStrategy');
propertiesDefaultFormatTextFormatFontFamily := CkJsonObject__stringOf(json,'properties.defaultFormat.textFormat.fontFamily');
propertiesDefaultFormatTextFormatFontSize := CkJsonObject_IntOf(json,'properties.defaultFormat.textFormat.fontSize');
propertiesDefaultFormatTextFormatBold := CkJsonObject_BoolOf(json,'properties.defaultFormat.textFormat.bold');
propertiesDefaultFormatTextFormatItalic := CkJsonObject_BoolOf(json,'properties.defaultFormat.textFormat.italic');
propertiesDefaultFormatTextFormatStrikethrough := CkJsonObject_BoolOf(json,'properties.defaultFormat.textFormat.strikethrough');
propertiesDefaultFormatTextFormatUnderline := CkJsonObject_BoolOf(json,'properties.defaultFormat.textFormat.underline');
spreadsheetUrl := CkJsonObject__stringOf(json,'spreadsheetUrl');
i := 0;
count_i := CkJsonObject_SizeOfArray(json,'sheets');
while (i < count_i) do
  begin
    CkJsonObject_putI(json,i);
    propertiesSheetId := CkJsonObject_IntOf(json,'sheets[i].properties.sheetId');
    propertiesTitle := CkJsonObject__stringOf(json,'sheets[i].properties.title');
    propertiesIndex := CkJsonObject_IntOf(json,'sheets[i].properties.index');
    propertiesSheetType := CkJsonObject__stringOf(json,'sheets[i].properties.sheetType');
    propertiesGridPropertiesRowCount := CkJsonObject_IntOf(json,'sheets[i].properties.gridProperties.rowCount');
    propertiesGridPropertiesColumnCount := CkJsonObject_IntOf(json,'sheets[i].properties.gridProperties.columnCount');
    i := i + 1;
  end;

CkJsonObject_Dispose(jsonToken);
CkHttp_Dispose(http);
CkJsonObject_Dispose(json);
CkHttpResponse_Dispose(resp);

end;