我使用下面的代码来做到这一点。
BatchUpdateSpreadsheetRequest batchUpdateSpreadsheetRequest = new BatchUpdateSpreadsheetRequest();
DuplicateSheetRequest requestBody = new DuplicateSheetRequest();
requestBody.setNewSheetName("test");
requestBody.setSourceSheetId(sheetId);
Sheets sheetsService = createSheetsService();
batchUpdateSpreadsheetRequest.set("duplicateSheet", requestBody);
Sheets.Spreadsheets.BatchUpdate request = sheetsService.spreadsheets().
batchUpdate(spreadsheetId,batchUpdateSpreadsheetRequest);
BatchUpdateSpreadsheetResponse response = request.execute();
当我执行此操作时,我收到以下错误。
Exception in thread "main" com.google.api.client.googleapis.json.GoogleJsonResponseException: 400 Bad Request
{
"code" : 400,
"errors" : [ {
"domain" : "global",
"message" : "Invalid JSON payload received. Unknown name \"duplicate_sheet\": Cannot find field.",
"reason" : "badRequest"
} ],
"message" : "Invalid JSON payload received. Unknown name \"duplicate_sheet\": Cannot find field.",
"status" : "INVALID_ARGUMENT"
}
我在这里做错了什么?创建工作表副本的正确方法是什么?
答案 0 :(得分:1)
我设法通过跟随google documentation类似的事情来解决这个问题。以下是我使用的代码。
BatchUpdateSpreadsheetRequest batchUpdateSpreadsheetRequest = new
BatchUpdateSpreadsheetRequest();
List<Request> requests = new ArrayList<>();
DuplicateSheetRequest requestBody = new DuplicateSheetRequest();
requestBody.setNewSheetName("test");
requestBody.setSourceSheetId(sheetId);
requests.add(new Request().setDuplicateSheet(requestBody));
Sheets sheetsService = createSheetsService();
batchUpdateSpreadsheetRequest.setRequests(requests);
Sheets.Spreadsheets.BatchUpdate request =
sheetsService.spreadsheets().batchUpdate(spreadsheetId, batchUpdateSpreadsheetRequest);
BatchUpdateSpreadsheetResponse response = request.execute();
答案 1 :(得分:0)
这里是PHP
版本,可在需要时创建工作表副本:
//Assuming you already have a $spreadsheet
$spreadsheetId = $spreadsheet->spreadsheetId;
$body = new Google_Service_Sheets_BatchUpdateSpreadsheetRequest(array(
'requests' => array(
'duplicateSheet' => array(
'sourceSheetId' => 0, //Source sheet id goes here as an integer
'insertSheetIndex' => 1, //Position where the new sheet should be inserted
'newSheetName' => 'test' //Set new name if you want
)
)
));
$spreadsheet = $service->spreadsheets->batchUpdate($spreadsheetId, $body);