copyWorksheet method

Future<Worksheet?> copyWorksheet(
  1. Worksheet ws,
  2. String title, {
  3. int? index,
})

Copies ws with specified title and index.

Returns Future of created Worksheet.

Throws GSheetsException if sheet with title already exists, or index is invalid.

Implementation

Future<Worksheet?> copyWorksheet(
  Worksheet ws,
  String title, {
  int? index,
}) async {
  except((index ?? 0) < 0, 'invalid index ($index)');
  final response = await GSheets.batchUpdate(_client, id, [
    {
      'duplicateSheet': {
        'sourceSheetId': ws.id,
        'insertSheetIndex': index,
        'newSheetId': null,
        'newSheetName': title
      }
    }
  ]);
  final duplicateSheetJson =
      (jsonDecode(response.body)['replies'] as List?)?.first;
  if (duplicateSheetJson == null) return null;
  final duplicate = Worksheet._fromJson(
    duplicateSheetJson['duplicateSheet'],
    _client,
    id,
    renderOption,
    inputOption,
  );
  sheets.forEach((sheet) => sheet._incrementIndex(duplicate.index - 1));
  sheets.add(duplicate);
  return duplicate;
}