2021.09 기준
flutter version: 1.22.6
gsheets: ^0.2.7
pubspec.yaml
추가0.3.2
까지 나와있으나, flutter SDK version이 낮아서 0.2.7
로 진행# google sheet
gsheets: ^0.2.7
CandyGSheetsAPIConfig
클래스 생성class CandyGSheetsAPIConfig {
static const _credentials = r'''
{
"type": "service_account",
"project_id": "",
...
"client_x509_cert_url": ""
}
''';
}
다음과 같은 형태로 작성
static const _credentials = r''' ''';
🙋🏻♀️ 참고사항
r
= raw, 원시 문자열 (예시로 \n, \t 안먹히고 문자열 그대로 읽힘)'''
= 멀티 라인 문자열 ('''안으로 감싸져있다면 문자열로 다 인식, 한줄이 아닌 여러 줄로 입력 가능)
GSheets
객체 완성 static final candyGSheets = GSheets(_credentials);
전체 코드
import 'package:gsheets/gsheets.dart';
class CandyGSheetsAPIConfig {
static const _credentials = r'''
{
"type": "service_account",
"project_id": "",
...
"client_x509_cert_url": ""
}
''';
static final candyGSheets = GSheets(_credentials);
}
드라이브 > 우클릭 > google 스프레드시트 추가
주소창 클릭
주소가 다음과 같이 있다면
https://docs.google.com/spreadsheets/d/1kDTrR5pqT8QxeElnSytyji-xxu1UQHpjiBBVLF0GqpE/edit#gid=0
/d/1kDTrR5pqT8QxeElnSytyji-xxu1UQHpjiBBVLF0GqpE/edit
이부분이 스프레드 시트 식별자(id)가 된다.
class HeavyUserEventService {
final String _spreadsheetId = '1kDTrR5pqT8QxeElnSytyji-xxu1UQHpjiBBVLF0GqpE';
}
Future<void> initalWorkSheet() async {
final gseets = CandyGSheetsAPIConfig.candyGSheets;
}
class HeavyUserEventService {
final String _spreadsheetId = '1kDTrR5pqT8QxeElnSytyji-xxu1UQHpjiBBVLF0GqpE';
Spreadsheet spreadsheet;
Future<void> initalWorkSheet() async {
final gseets = CandyGSheetsAPIConfig.candyGSheets;
spreadsheet ??= await gseets.spreadsheet(_spreadsheetId);
}
class HeavyUserEventService {
final String _workSheetTitle = 'FCM_heavyUser';
...
Worksheet workSheet;
Future<void> initalWorkSheet() async {
...
if (workSheet == null) {
workSheet = await spreadsheet.worksheetByTitle(_workSheetTitle);
workSheet ??= await spreadsheet.addWorksheet(_workSheetTitle);
}
}
}
Spreadsheet spreadsheet;
Worksheet workSheet;
class HeavyUserEventService {
final String _spreadsheetId = '1kDTrR5pqT8QxeElnSytyji-xxu1UQHpjiBBVLF0GqpE';
final String _workSheetTitle = 'FCM_heavyUser';
Spreadsheet spreadsheet;
Worksheet workSheet;
Future<void> initalWorkSheet() async {
final gseets = CandyGSheetsAPIConfig.candyGSheets;
spreadsheet ??= await gseets.spreadsheet(_spreadsheetId);
if (workSheet == null) {
workSheet = await spreadsheet.worksheetByTitle(_workSheetTitle);
workSheet ??= await spreadsheet.addWorksheet(_workSheetTitle);
}
}
}
workSheet객체를 이용해 insertRow 메소드 생성
line 수와 row 입력
bool
, string
, and double
전체 코드
class HeavyUserEventService {
Spreadsheet spreadsheet;
Worksheet workSheet;
...
void insertRow({
List<dynamic> row,
}) async {
if (workSheet == null) {
return;
}
final result = await workSheet.values.insertRow(
2,
[
4030.22,
'Bora Test2',
20,
{'dd': 'ss'}.toString()
],
);
print('$_workSheetTitle insertRow completed $result');
}
}
cf.