createSpreadsheet method

Future<Spreadsheet> createSpreadsheet(
  1. String title, {
  2. List<String> worksheetTitles = const <String>['Sheet1'],
  3. ValueRenderOption render = ValueRenderOption.unformattedValue,
  4. ValueInputOption input = ValueInputOption.userEntered,
})

Creates a new Spreadsheet, and returns it.

Requires SheetsApi.SpreadsheetsScope.

It's recommended to save Spreadsheet id once its created, it also can be shared with the user by email via method share of Spreadsheet.

worksheetTitles - optional (defaults to ['Sheet1']), titles of the worksheets that will be created along with the spreadsheet

render - determines how values should be rendered in the output. https://developers.google.com/sheets/api/reference/rest/v4/ValueRenderOption

input - determines how input data should be interpreted. https://developers.google.com/sheets/api/reference/rest/v4/ValueInputOption

Throws Exception if GSheets's scopes does not include SpreadsheetsScope. Throws GSheetsException if does not have permission.

Implementation

Future<Spreadsheet> createSpreadsheet(
  String title, {
  List<String> worksheetTitles = const <String>['Sheet1'],
  ValueRenderOption render = ValueRenderOption.unformattedValue,
  ValueInputOption input = ValueInputOption.userEntered,
}) async {
  final client = await this.client.catchError((_) {
    // retry once on error
    _client = null;
    return this.client;
  });
  final worksheets = worksheetTitles
      .map((title) => {
            'properties': {
              'title': title,
              'sheetType': 'GRID',
              'gridProperties': {
                'rowCount': defaultRowsCount,
                'columnCount': defaultColumnCount,
              }
            },
          })
      .toList();
  final response = await client.post(
    _sheetsEndpoint.toUri(),
    body: jsonEncode(
      {
        'properties': {
          'title': title,
        },
        'sheets': worksheets,
      },
    ),
  );
  checkResponse(response);
  return Spreadsheet._fromJson(
    json: jsonDecode(response.body),
    client: client,
    renderOption: _parseRenderOption(render),
    inputOption: _parseInputOption(input),
  );
}