插入行而不覆盖数据

时间:2016-09-22 02:22:57

标签: java android google-sheets google-spreadsheet-api

我最终在谷歌这里结束了很多,因为我开始尝试为自己创建一个Android应用程序,因为v4 API并不是很有帮助,所以我很难过。我在这里搜索,但我没有看到答案回答我的问题。

我有预算电子表格。我的应用程序将从工作表中获取数据并将其过滤到我当前的付费周。所以我可以查看我目前所有的交易和未来的交易,这样我就可以知道我有多少额外花费。然后我想能够从应用程序中插入一个新的牵引力,如果我得到气体,我希望能够在几次点击中添加,而不是尝试花几分钟在工作表应用程序中编辑工作表。后来我计划为我放入的单元格定制公式,并将其复制到它的北面和南面的单元格中,这样数学运算就可以了。

我使用the Android Quickstart能够轻松读取数据。我已经调整过来过滤工作表数据以过滤我的数据。我甚至走得太远,无法将数据写入工作表。问题是我无法在java / android中找到与快速入门相同的样式中的任何示例,以了解如何在工作表的中间插入行而不是覆盖单元格。我假设我必须在某个地方使用INSERTDATAOPTION = INSERT_ROWS,但我找不到任何地方可以添加它。 documentation建议我使用spreadsheets.values.append,但.append永远不会显示为选项。我只获得BatchGet,BatchUpdate,Get,Update。

我是一名初学者,我确信我的代码很笨拙,经过数小时的谷歌搜索后拼凑在一起,但我确定我只是错过了一些简单的事情。任何帮助,将不胜感激。



private List<String> PostDataForApi() throws IOException {
            String spreadsheetID = getResources().getString(R.string.my_google_spreadsheet_id);
            Integer sheetID = getResources().getInteger(R.integer.my_google_sheet_id);
            
            List<RowData> rowData = new ArrayList<RowData>();
            List<CellData> cellData = new ArrayList<CellData>();

            String value = "test";
            String formula = "=IF(COUNTBLANK(C510) = 2,\"\",Sum(B511+(SUM(C510))))";
            String value2 = "999";

            CellData cell = new CellData();
            cell.setUserEnteredValue(new ExtendedValue().setStringValue(value));
            CellData cell2 = new CellData();
            cell2.setUserEnteredValue(new ExtendedValue().setFormulaValue(formula));
            CellData cell3 = new CellData();
            cell3.setUserEnteredValue(new ExtendedValue().setStringValue(value2));

            cellData.add(cell);
            cellData.add(cell2);
            cellData.add(cell3);
            rowData.add(new RowData().setValues(cellData));

            BatchUpdateSpreadsheetRequest batchRequests = new BatchUpdateSpreadsheetRequest();
            BatchUpdateSpreadsheetResponse response;
            List<Request> requests = new ArrayList<Request>();
            AppendCellsRequest appendCellReq = new AppendCellsRequest();

            appendCellReq.setSheetId(sheetID);
            appendCellReq.setRows( rowData );
            appendCellReq.setFields("userEnteredValue");

            requests = new ArrayList<Request>();
            requests.add( new Request().setAppendCells(appendCellReq));
            batchRequests = new BatchUpdateSpreadsheetRequest();
            batchRequests.setRequests( requests );


            response =  this.mService.spreadsheets().batchUpdate(spreadsheetID, batchRequests).execute();
            System.out.println(response.toPrettyString());

            return null;
        }
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:2)

我想到了一些心灵麻木后把东西扔在墙上,看看有什么东西。我必须分两步完成。第一步将在第32行的工作表中插入一行,这样你就会得到一个空白行33.然后第二个区域会将值插入该空白行。我希望这可以帮助将来的某个人。

String spreadsheetID = getResources().getString(R.string.my_google_spreadsheet_id);
            Integer sheetID = getResources().getInteger(R.integer.my_google_sheet_id);

            BatchUpdateSpreadsheetResponse response;
            BatchUpdateSpreadsheetRequest batchRequests = new BatchUpdateSpreadsheetRequest();
            List<Request> requests = new ArrayList<Request>();

            InsertDimensionRequest insertDimensionRequest = new InsertDimensionRequest();
            DimensionRange dimRange = new DimensionRange();
            dimRange.setStartIndex(32);
            dimRange.setEndIndex(33);
            dimRange.setSheetId(sheetID);
            dimRange.setDimension("ROWS");

            insertDimensionRequest.setRange(dimRange);
            insertDimensionRequest.setInheritFromBefore(false);
            requests.add( new Request().setInsertDimension(insertDimensionRequest));
            batchRequests = new BatchUpdateSpreadsheetRequest();
            batchRequests.setRequests( requests );
            response =  this.mService.spreadsheets().batchUpdate(spreadsheetID, batchRequests).execute();
            System.out.println(response.toPrettyString());

            List<List<Object>> argData = getData(entryTitle, entryValue);
            ValueRange vRange = new ValueRange();
            vRange.setRange("2016!A33");
            vRange.setValues(argData);
            List<ValueRange> vList = new ArrayList<>();
            vList.add(vRange);
            BatchUpdateValuesRequest batchRequest = new BatchUpdateValuesRequest();
            batchRequest.setValueInputOption("USER-ENTERED");
            batchRequest.setData(vList);
            this.mService.spreadsheets().values().batchUpdate(spreadsheetID, batchRequest).execute();

答案 1 :(得分:0)

我一直在用v4 google sheet api进行太多不必要的喧嚣,这太荒谬了。因此,我回到了gdata(v3)api,更容易理解,也更好地记录了。

以下是设置它的链接以及一些示例https://developers.google.com/google-apps/spreadsheets/

唯一缺少的是那些注释中的授权过程,这是一种喧嚣,但经过一些挖掘后,我能够获得授权代码库,如下所示。

public class YourClass {
    // Application name
    private static final String APPLICATION_NAME = "Your-Application-Name";

// account info and p12
private static final String ACCOUNT_P12_ID = "Get-the-details-developer-console-google";
private static final File P12FILE = new File("D:/path/Drive API Test-bf290e0ee314.p12");

// scopes
private static final List<String> SCOPES = Arrays.asList(
        "https://docs.google.com/feeds",
        "https://spreadsheets.google.com/feeds");

// Spreadsheet API URL
private static final String SPREADSHEET_URL = "https://spreadsheets.google.com/feeds/spreadsheets/private/full";

private static final URL SPREADSHEET_FEED_URL;

static {
    try {
        SPREADSHEET_FEED_URL = new URL(SPREADSHEET_URL);
    } catch (MalformedURLException e) {
        throw new RuntimeException(e);
    }
}
// Authorize
private static Credential authorize() throws Exception {
    System.out.println("authorize in");

    HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
    JsonFactory jsonFactory = new JacksonFactory();

    GoogleCredential credential = new GoogleCredential.Builder()
            .setTransport(httpTransport)
            .setJsonFactory(jsonFactory)
            .setServiceAccountId(ACCOUNT_P12_ID)
            .setServiceAccountPrivateKeyFromP12File(P12FILE)
            .setServiceAccountScopes(SCOPES)
            .build();

    boolean ret = credential.refreshToken();
    // debug dump
    System.out.println("refreshToken:" + ret);

    // debug dump
    if (credential != null) {
        System.out.println("AccessToken:" + credential.getAccessToken());
    }

    System.out.println("authorize out");

    return credential;
}

//    Get service
private static SpreadsheetService getService() throws Exception {
    System.out.println("service in");

    SpreadsheetService service = new SpreadsheetService(APPLICATION_NAME);
    service.setProtocolVersion(SpreadsheetService.Versions.V3);

    Credential credential = authorize();
    service.setOAuth2Credentials(credential);

    // debug dump
    System.out.println("Schema: " + service.getSchema().toString());
    System.out.println("Protocol: " + service.getProtocolVersion().getVersionString());
    System.out.println("ServiceVersion: " + service.getServiceVersion());

    System.out.println("service out");

    return service;
}

从那以后,我能够执行一些插入并附加到工作表上,没有任何重大的喧嚣。