API上设置自动换行策略

API上设置自动换行策略

进行了一段时间的工作后,看到了一些在堆栈溢出时关闭的东西,但是没有一个关于上下文的很好的例子。我的大问题是获取工作表的gridId。

final Request wrap = new Request()
                .setRepeatCell(new RepeatCellRequest()
                        .setCell(new CellData()
                                // test to see what field gets set
                                // .setUserEnteredValue( new ExtendedValue().setStringValue(mContext.getString(R.string.lazy_dog)))
                                .setUserEnteredFormat(new CellFormat().setWrapStrategy("WRAP")))
                        .setRange(new GridRange()
                                .setSheetId(response.getSheets().get(0).getProperties().getSheetId())
                                .setStartRowIndex(2)
                                .setStartColumnIndex(1)
                                .setEndColumnIndex(2)
                        ).setFields("*")
                );

最佳答案

这是为我工作的整个代码块。我相信还有更多简洁的方法可以做到。欢迎任何输入,但这对我有用。该代码在任务中,仅包括创建新图纸,添加列和格式的部分。

 SpreadsheetProperties properties = new SpreadsheetProperties();
    properties.setTitle(new StringBuilder(mContext.getString(R.string.app_initials))
            .append(" " + mUnit)
            .append(" ")
            .append(calendar.getTime().toString())
            .toString());

    Spreadsheet requestBody = new Spreadsheet();

    GridProperties gridProperties = new GridProperties();
    gridProperties.setFrozenRowCount(2).setColumnCount(7);

    SheetProperties sheetProperties = new SheetProperties();
    sheetProperties.setTitle("my new sheet");
    sheetProperties
            .setGridProperties(gridProperties);

    Sheet sheet = new Sheet();
    sheet.setProperties(sheetProperties);

    List<Sheet> sheets = new ArrayList<>();
    sheets.add(sheet);

    requestBody.setSheets(sheets);
    requestBody.setProperties(properties);

    String rangeH = "A1:D1";

    List<List<Object>> valuesH = new ArrayList<>();
    Object o1 = "date", o2 = "description", o3 = "distance", o4 = "total " + mUnit, o5 = "", o6 = "total time";
    valuesH.add(Arrays.asList(o1, o2, o3, o4, o5, o6));

    ValueRange valueRangeH = new ValueRange();
    valueRangeH.setMajorDimension("ROWS");
    valueRangeH.setRange(rangeH);
    valueRangeH.setValues(valuesH);

    //Object formula = "=SUM(C1:C)";
    String range = "D2:F2";

    Object o7 = "=SUM(C1:C)", o8 = mUnit, o9 = "=SUM(F3:F)";

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

    values.add(Arrays.asList(o7, o8,o9));

    ValueRange valueRange = new ValueRange();
    valueRange.setMajorDimension("ROWS");
    valueRange.setRange(range);
    valueRange.setValues(values);

    Spreadsheet response = null;
    AppendValuesResponse avResponse = null;

    try {

        Sheets.Spreadsheets.Create request = mService.spreadsheets().create(requestBody);
        response = request.execute();

        avResponse = this.mService.spreadsheets().values()
                .append(response.getSpreadsheetId(), rangeH, valueRangeH)
                .setValueInputOption("USER_ENTERED")
                .execute();

        //adds the formula
        avResponse = this.mService.spreadsheets().values()
                .append(response.getSpreadsheetId(), range, valueRange)
                .setValueInputOption("USER_ENTERED")
                .execute();

        final Request wrap = new Request()
                .setRepeatCell(new RepeatCellRequest()
                        .setCell(new CellData()
                                // test to see what field gets set
                                // .setUserEnteredValue( new ExtendedValue().setStringValue(mContext.getString(R.string.lazy_dog)))
                                .setUserEnteredFormat(new CellFormat().setWrapStrategy("WRAP")))
                        .setRange(new GridRange()
                                .setSheetId(response.getSheets().get(0).getProperties().getSheetId())
                                .setStartRowIndex(2)
                                .setStartColumnIndex(1)
                                .setEndColumnIndex(2)
                        ).setFields("*")
                );


        List<Request> requests = new ArrayList<>();
        requests.add(wrap);




      this.mService.spreadsheets().batchUpdate(response.getSpreadsheetId(),
                new BatchUpdateSpreadsheetRequest().setRequests(requests))
                .execute();

关于java - 如何使用Java库在Google Sheets API上设置自动换行策略,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58207756/

10-09 00:46