Perl
Perl
Google Sheets - Create a New Spreadsheet
See more Google Sheets Examples
Demonstrates how to create a new and empty spreadsheet.Chilkat Perl Downloads
use strict;
use warnings;
use Chilkat2;
# 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..
my $jsonToken = Chilkat2::JsonObject->new;
my $success = $jsonToken->LoadFile('qa_data/tokens/googleSheets.json');
if (!$jsonToken->HasMember('access_token')) {
print "No access token found.\n";
exit;
}
my $http = Chilkat2::Http->new;
$http->AuthToken($jsonToken->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:
my $json = Chilkat2::JsonObject->new;
$json->UpdateString('sheets[0].properties.title', 'Sample Tab');
$json->UpdateString('properties.title', 'Create Spreadsheet using Sheets API v4');
# Send the POST to create the new Google spreadsheet.
my $resp = Chilkat2::HttpResponse->new;
$success = $http->HttpJson('POST', 'https://sheets.googleapis.com/v4/spreadsheets', $json, 'application/json', $resp);
if (!$success) {
print $http->LastErrorText, "\n";
exit;
}
print 'response status code = ', $resp->StatusCode, "\n";
print "response JSON:\n";
$json->Load($resp->BodyStr);
$json->EmitCompact(0);
print $json->Emit(), "\n";
# 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"
# }
#
my $spreadsheetId = $json->StringOf('spreadsheetId');
my $propertiesTitle = $json->StringOf('properties.title');
my $propertiesLocale = $json->StringOf('properties.locale');
my $propertiesAutoRecalc = $json->StringOf('properties.autoRecalc');
my $propertiesTimeZone = $json->StringOf('properties.timeZone');
my $propertiesDefaultFormatBackgroundColorRed = $json->IntOf('properties.defaultFormat.backgroundColor.red');
my $propertiesDefaultFormatBackgroundColorGreen = $json->IntOf('properties.defaultFormat.backgroundColor.green');
my $propertiesDefaultFormatBackgroundColorBlue = $json->IntOf('properties.defaultFormat.backgroundColor.blue');
my $propertiesDefaultFormatPaddingTop = $json->IntOf('properties.defaultFormat.padding.top');
my $propertiesDefaultFormatPaddingRight = $json->IntOf('properties.defaultFormat.padding.right');
my $propertiesDefaultFormatPaddingBottom = $json->IntOf('properties.defaultFormat.padding.bottom');
my $propertiesDefaultFormatPaddingLeft = $json->IntOf('properties.defaultFormat.padding.left');
my $propertiesDefaultFormatVerticalAlignment = $json->StringOf('properties.defaultFormat.verticalAlignment');
my $propertiesDefaultFormatWrapStrategy = $json->StringOf('properties.defaultFormat.wrapStrategy');
my $propertiesDefaultFormatTextFormatFontFamily = $json->StringOf('properties.defaultFormat.textFormat.fontFamily');
my $propertiesDefaultFormatTextFormatFontSize = $json->IntOf('properties.defaultFormat.textFormat.fontSize');
my $propertiesDefaultFormatTextFormatBold = $json->BoolOf('properties.defaultFormat.textFormat.bold');
my $propertiesDefaultFormatTextFormatItalic = $json->BoolOf('properties.defaultFormat.textFormat.italic');
my $propertiesDefaultFormatTextFormatStrikethrough = $json->BoolOf('properties.defaultFormat.textFormat.strikethrough');
my $propertiesDefaultFormatTextFormatUnderline = $json->BoolOf('properties.defaultFormat.textFormat.underline');
my $spreadsheetUrl = $json->StringOf('spreadsheetUrl');
my $i = 0;
my $count_i = $json->SizeOfArray('sheets');
while ($i < $count_i) {
$json->I($i);
my $propertiesSheetId = $json->IntOf('sheets[i].properties.sheetId');
$propertiesTitle = $json->StringOf('sheets[i].properties.title');
my $propertiesIndex = $json->IntOf('sheets[i].properties.index');
my $propertiesSheetType = $json->StringOf('sheets[i].properties.sheetType');
my $propertiesGridPropertiesRowCount = $json->IntOf('sheets[i].properties.gridProperties.rowCount');
my $propertiesGridPropertiesColumnCount = $json->IntOf('sheets[i].properties.gridProperties.columnCount');
$i = $i + 1;
}