Google Sheet API插入多行Android

时间:2017-09-20 05:54:40

标签: android google-sheets google-sheets-api google-apis-explorer

我正在尝试在我的Google Sheet Through API中插入多个值,但它给了我错误,错误列在下面

{
  "code" : 400,
  "errors" : [ {
    "domain" : "global",
    "message" : "Invalid values[1][0]: list_value {\n  values {\n    string_value: \"1041\"\n  }\n  values {\n    string_value: \"28.4121001\"\n  }\n  values {\n    string_value: \"77.0438644\"\n  }\n  values {\n    string_value: \"2156-5091\"\n  }\n  values {\n    string_value: \"ABCD\"\n  }\n  values {\n    string_value: \"Sep 20 2017, 01:27 PM\"\n  }\n  values {\n    string_value: \"NA\"\n  }\n  values {\n    string_value: \"A1E85C43-B8C8-42F0-96EC-D1586FCBB2CF\"\n  }\n}\n",
    "reason" : "badRequest"
  } ],
  "message" : "Invalid values[1][0]: list_value {\n  values {\n    string_value: \"1041\"\n  }\n  values {\n    string_value: \"28.4121001\"\n  }\n  values {\n    string_value: \"77.0438644\"\n  }\n  values {\n    string_value: \"2156-5091\"\n  }\n  values {\n    string_value: \"ABCD\"\n  }\n  values {\n    string_value: \"Sep 20 2017, 01:27 PM\"\n  }\n  values {\n    string_value: \"NA\"\n  }\n  values {\n    string_value: \"A1E85C43-B8C8-42F0-96EC-D1586FCBB2CF\"\n  }\n}\n",
  "status" : "INVALID_ARGUMENT"
}

以下是我用于将数据发送到表单的代码

      private class MakeRequestTask extends AsyncTask<Void, Void, AppendValuesResponse> {
        private com.google.api.services.sheets.v4.Sheets mService = null;
        private Exception mLastError = null;
        List<CustomerGeoData> mList;

        MakeRequestTask(GoogleAccountCredential credential, List<CustomerGeoData> mList) {
            HttpTransport transport = AndroidHttp.newCompatibleTransport();
            JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
            mService = new com.google.api.services.sheets.v4.Sheets.Builder(
                    transport, jsonFactory, credential)
                    .setApplicationName("Geo Tagger")
                    .build();

            this.mList = mList;
        }

        /**
         * Background task to call Google Sheets API.
         *
         * @param params no parameters needed for this task.
         */
        @Override
        protected AppendValuesResponse doInBackground(Void... params) {
            try {
                return getDataFromApi();
            } catch (Exception e) {
                e.printStackTrace();
                mLastError = e;
                cancel(true);
                return null;
            }
        }

        /**
         * Sending the data to the Sheet
         * https://docs.google.com/spreadsheets/d/1qLwONBo8SbjvVwEi_RCjzw8D8teHcdke-rkCEowq2bU/edit
         *
         * @return List of names and majors
         * @throws IOException
         */
        private AppendValuesResponse getDataFromApi() throws IOException {
            String spreadsheetId = GeoTaggerConstants.CUSTOMER_GEO_DATA;
            String range = GeoTaggerConstants.CUSTOMER_GEO_DATA_SHEET_NAME + "!A1:H";

            Object[] additionalRows = new Object[mList.size()];
            Object[] cellValue;

            List<List<Object>> values = new ArrayList<>();

            for (int i = 0; i < mList.size(); i++) {
                //Cell Values
                cellValue = new String[]{mList.get(i).getCustomer_Barcode(), mList.get(i).getLatitude(), mList.get(i).getLongitude(),
                        mList.get(i).getCustomer_No(), mList.get(i).getCustomer_Name(), mList.get(i).getLogged_At(),
                        mList.get(i).getLogged_By(), mList.get(i).getRow_ID()};

                //Additional Rows added here till the size of the list
                additionalRows[i] = Arrays.asList(cellValue);

                lastCustomerName = mList.get(i).getCustomer_Name();
            }
            values.add(Arrays.asList(additionalRows));    
            ValueRange body = new ValueRange().setValues(values);

            AppendValuesResponse result =
                    mService.spreadsheets().values().append(spreadsheetId, range, body)
                            .setValueInputOption("RAW")
                            .execute();


            return result;
        }


        @Override
        protected void onPreExecute() {
            //Start Loading
        }

        @Override
        protected void onPostExecute(AppendValuesResponse output) {
            if (output == null || output.size() == 0) {
                System.out.println("No results returned.");
            } else {

                System.out.println("output  >>>  " + output);
            }
        }

        @Override
        protected void onCancelled() {
            if (mLastError != null) {
                if (mLastError instanceof GooglePlayServicesAvailabilityIOException) {
                    showGooglePlayServicesAvailabilityErrorDialog(
                            ((GooglePlayServicesAvailabilityIOException) mLastError)
                                    .getConnectionStatusCode());
                } else if (mLastError instanceof UserRecoverableAuthIOException) {
                    startActivityForResult(
                            ((UserRecoverableAuthIOException) mLastError).getIntent(), REQUEST_AUTHORIZATION);
                } else {
                    System.out.println("The following error occurred:\n"
                            + mLastError.getCause());
                }
            } else {
                System.out.println("Request cancelled.");
            }
        }
    }

这就是我的mList看起来像我发送给Sheet API的数据

Values sending looks like this

任何帮助都很明显,在此先感谢

0 个答案:

没有答案