Google Sheet API v4会插入新行

时间:2017-02-08 11:36:13

标签: android google-sheets

我使用Google Sheet API v4,我想在我的工作表中插入新行。 我试过https://developers.google.com/sheets/api/guides/values#appending_values 但这样只需插入数据,我想将数据添加到特定列,如下所示:Is there a way to add a row on a Google Spreadsheet without OAuth using java? 这是一个如何使用api v3

执行此操作的示例
    row.getCustomElements().setValueLocal("id", "21");
    row.getCustomElements().setValueLocal("type", si.getType().toString());
    row.getCustomElements().setValueLocal("longitude", String.valueOf(si.getLongitude()));
    row.getCustomElements().setValueLocal("latitude", String.valueOf(si.getLatitude()));
    row.getCustomElements().setValueLocal("last_maint", String.valueOf(si.getLast()));
    row.getCustomElements().setValueLocal("inst_date", String.valueOf(si.getInst()));

    row = service.insert(listFeedUrl, row);

如何使用api v4进行相同的操作。

2 个答案:

答案 0 :(得分:1)

按照此快速入门设置您的Android项目以使用Google Spreadsheet API v4 https://developers.google.com/sheets/api/quickstart/android

设置完成后,您阅读使用API​​。

插入新行的代码

MainActivity中的private class MakeRequestTask extends AsyncTask<Void, Void, List<String>> 添加insertDataToApi()功能

        private boolean insertDataToApi() throws IOException{
        String spreadsheetId = "your_spreadsheet_id";
        String range = "MySheet!A1:A"; // A1:A refers to the 1st column of "MySheet", similarlly B1:B 2nd column. For more understanding https://developers.google.com/sheets/api/guides/concepts

        List<Object> row1 = new ArrayList<>();
        row1.add("Name");
        row1.add("Rollno");
        row1.add("Class");
        // similarly create more rows with data

        List<List<Object>> vaules = new ArrayList<>();
        vaules.add(row1);

        ValueRange valueRange = new ValueRange();
        valueRange.setMajorDimension("ROWS");
        valueRange.setValues(vaules);

        UpdateValuesResponse response = this.mService.spreadsheets().values()
                .update(spreadsheetId, range, valueRange)
                .setValueInputOption("RAW")
                .execute();

        Log.e("Update_response", response.toString());

        return true;
    }

现在只需在insertDataToApi()

中拨打protected List<String> doInBackground(Void... params){}即可

答案 1 :(得分:0)

将行添加到Google表格中,在C#,Google.Api.Sheet v4中工作

public void SaveAccount(String userName, String password) {

        GetCredential();

        // Create Google Sheets API service.
        var service = new SheetsService(new BaseClientService.Initializer()
        {
            HttpClientInitializer = credential,
            ApplicationName = ApplicationName,
        });

        // Define one row
        List<IList<Object>> values = new List<IList<object>>();

        IList<Object> obj = new List<Object>();
        obj.Add(userName);
        obj.Add(password);

        values.Add(obj);

        // Add Batch 
        SpreadsheetsResource.ValuesResource.AppendRequest request =
           service.Spreadsheets.Values.Append(new ValueRange() { Values = values }, idHBusAccount, rangeHBusAccount);
        request.InsertDataOption = SpreadsheetsResource.ValuesResource.AppendRequest.InsertDataOptionEnum.INSERTROWS;
        request.ValueInputOption = SpreadsheetsResource.ValuesResource.AppendRequest.ValueInputOptionEnum.USERENTERED;
        var response = request.Execute();

        // Console.WriteLine(response.ToString());

    }