React Native
React Native
Google Sheets - Create a New Spreadsheet
See more Google Sheets Examples
Demonstrates how to create a new and empty spreadsheet.Chilkat React Native Downloads
import { Http, HttpResponse, JsonObject } from '@chilkat/react-native'
async function chilkatExample() {
// 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..
const jsonToken = new JsonObject();
jsonToken.loadFile('qa_data/tokens/googleSheets.json');
if (!jsonToken.hasMember('access_token')) {
console.log('No access token found.');
return;
}
const http = new Http();
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:
const json = new JsonObject();
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.
const resp = new HttpResponse();
try {
await http.httpJsonAsync('POST', 'https://sheets.googleapis.com/v4/spreadsheets', json, 'application/json', resp);
} catch {
console.log(http.lastErrorText);
return;
}
console.log(`response status code = ${resp.statusCode}`);
console.log('response JSON:');
json.load(resp.bodyStr);
json.emitCompact = false;
console.log(json.emit());
// 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"
// }
//
let i = 0;
const spreadsheetId = json.stringOf('spreadsheetId');
let propertiesTitle = json.stringOf('properties.title');
const propertiesLocale = json.stringOf('properties.locale');
const propertiesAutoRecalc = json.stringOf('properties.autoRecalc');
const propertiesTimeZone = json.stringOf('properties.timeZone');
const propertiesDefaultFormatBackgroundColorRed = json.intOf('properties.defaultFormat.backgroundColor.red');
const propertiesDefaultFormatBackgroundColorGreen = json.intOf('properties.defaultFormat.backgroundColor.green');
const propertiesDefaultFormatBackgroundColorBlue = json.intOf('properties.defaultFormat.backgroundColor.blue');
const propertiesDefaultFormatPaddingTop = json.intOf('properties.defaultFormat.padding.top');
const propertiesDefaultFormatPaddingRight = json.intOf('properties.defaultFormat.padding.right');
const propertiesDefaultFormatPaddingBottom = json.intOf('properties.defaultFormat.padding.bottom');
const propertiesDefaultFormatPaddingLeft = json.intOf('properties.defaultFormat.padding.left');
const propertiesDefaultFormatVerticalAlignment = json.stringOf('properties.defaultFormat.verticalAlignment');
const propertiesDefaultFormatWrapStrategy = json.stringOf('properties.defaultFormat.wrapStrategy');
const propertiesDefaultFormatTextFormatFontFamily = json.stringOf('properties.defaultFormat.textFormat.fontFamily');
const propertiesDefaultFormatTextFormatFontSize = json.intOf('properties.defaultFormat.textFormat.fontSize');
const spreadsheetUrl = json.stringOf('spreadsheetUrl');
i = 0;
const countI = json.sizeOfArray('sheets');
while (i < countI) {
json.i = i;
const propertiesSheetId = json.intOf('sheets[i].properties.sheetId');
propertiesTitle = json.stringOf('sheets[i].properties.title');
const propertiesIndex = json.intOf('sheets[i].properties.index');
const propertiesSheetType = json.stringOf('sheets[i].properties.sheetType');
const propertiesGridPropertiesRowCount = json.intOf('sheets[i].properties.gridProperties.rowCount');
const propertiesGridPropertiesColumnCount = json.intOf('sheets[i].properties.gridProperties.columnCount');
i++;
}
}