在Google表格文档中创建新工作表

时间:2016-12-27 09:22:46

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

我想使用Java API v4在Google工作表文档中创建新工作表。我测试了这段代码:

        Sheets service = getSheetsService();

        Spreadsheet sheet = new Spreadsheet();
        SpreadsheetProperties properties = new SpreadsheetProperties();
        properties.setTitle("new SpreadSheetTitle");
        sheet.setProperties(properties);

        Spreadsheet response = service.spreadsheets().create(sheet)
            .execute();

但是当我打开文档时,我看不到新页面。实现这个的正确方法是什么?

P.S

我设法创造了这个:

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

        List<Object> dataRow = new ArrayList<>();
        dataRow.add("data 1 " + timestamp);
        dataRow.add("data 2 " + timestamp);
        dataRow.add("data 3 " + timestamp);
        dataRow.add("data 4 " + timestamp);
        dataRow.add("data 5 " + timestamp);
        writeData.add(dataRow);

        requests.add(new Request().setAddSheet(new AddSheetRequest()
            .setProperties(new SheetProperties().setTitle("scstc"))));


        BatchUpdateSpreadsheetRequest body
            = new BatchUpdateSpreadsheetRequest().setRequests(requests);
        BatchUpdateSpreadsheetResponse response = service.spreadsheets().batchUpdate(spreadsheetId, body).execute();

我可以创建空白表但是如何插入测试数据?

1 个答案:

答案 0 :(得分:1)

您在文档中创建了一个新标签 如果你想在创建的时候添加一些列,你可以这样做:

const buffer = ArrayBuffer(4 * 100 * 100)
const ui8ca = new Uint8ClampedArray(buffer);
const imageData = new ImageData(buffer, 100, 100);

const canvas = document.getElementById('myCanvas');
const ctx = canvas.getContext('2d');
ctx.putImageData(imageData, 0, 0);

此代码只是将数据插入名为“TITLE_OF_YOUR_NEW_TAB”的新标签

ValueRange appendBody = new ValueRange()
                .setValues(Collections.singletonList(
                        Arrays.asList("ColumnName1", "ColumnName2")));

getSheetsService().spreadsheets().values()
                .append(SPREADSHEET_ID, "TITLE_OF_YOUR_NEW_TAB", appendBody)
                .setValueInputOption("USER_ENTERED")
                .setInsertDataOption("INSERT_ROWS")
                .setIncludeValuesInResponse(true)
                .execute();