如何从flutter应用程序中更新和删除google表单中的值?

b09cbbtk  于 2022-11-17  发布在  Flutter
关注(0)|答案(1)|浏览(123)

我正在使用flutter作为在google工作表中进行crud操作的接口,我正在使用gsheets包。到目前为止,我成功地创建了数据,但我不知道如何从flutter应用程序中更新和删除任何特定的行或列。因此,我提到了我用来从应用程序中创建数据的代码。

class User{
  static const credentials = r'''
{
  "type": "service_account",
  "project_id": "******",
  "private_key_id": "*********",
  "private_key": "-----BEGIN PRIVATE KEY-----*********-----END PRIVATE KEY-----\n",
  "client_email": "*******",
  "client_id": "***********",
  "auth_uri": "*********************",
  "token_uri": "*************************************"
}

''';

static final id = '13caD5_ljQlwU3-VbbUeKSo-HnzrmEdfRXkhAWNAHitM';
static final gsheets = GSheets(credentials);
static Worksheet? us;

static Future init() async{
  try{
  final spreadsheet = await gsheets.spreadsheet(id);
  us = await getWorkSheet(spreadsheet, title:"staff" );
  final firstRow = Uf.getF();
  us!.values.insertRow(1, firstRow);
} catch (e){
  print(e.toString());
}
}
static Future <Worksheet> getWorkSheet(
  Spreadsheet ss, {
    required String title,
  }
) async {
  try{
    return await ss.addWorksheet(title);
  }catch (e){
    return ss.worksheetByTitle(title)!;
  }
}

static Future insert(List<Map<String,dynamic>> rowList)async{
  if(us == null) return;
  us!.values.map.appendRows(rowList);
}

}



class Uf{
  static final String id = "Id";
  static final String date = "Date";
  static final String company = "Company";
  static final String reason = "Reason";
  static final String  received_calls= "Received Calls";
  static final String handled = "Handled";
  static final String status = "Status";
  static final String task = "Task";

  static List getF()=>[id,date,company,reason,received_calls,handled,status,task];
}

class U{
  final String id;
  final String date;
  final String company;
  final String reason;
  final String calls;
  final String handled;
  final String status;
  final String task;

  U({ required this.id, required this.date,required this.company,required this.reason,required this.calls,required this.handled,required this.status,required this.task});



Map <String , dynamic> toJson()=>{
  Uf.id : id,
  Uf.date : date,
  Uf.company : company,
  Uf.reason : reason,
  Uf.received_calls : calls,
  Uf.handled : handled,
  Uf.status : status,
  Uf.task : task,

};
}
8ulbf1ek

8ulbf1ek1#

若要update储存格:

static Future<bool> updateCell(
      {required dynamic value, required String key, required String id}) async {

    // value -> New value to be updated
    // key   -> Key of that column eg.'name'
    // id    -> Key of that cell 'current value of the cell' eg.'John'

    if (_nameOfSheet == null) return false;

    return _nameOfSheet!.values
        .insertValueByKeys(value, columnKey: key, rowKey: id);
  }

delete一个单行:(String id是检测整行的列中的指示符值)

static Future deleteRow(String id) async {
    if (_nameOfSheet == null) return false;

    final index = await _nameOfSheet!.values.rowIndexOf(id);
    if (index == -1) return false;

    return _nameOfSheet!.deleteRow(index);
  }

相关问题