如何在 google spreadsheet api 中复制 sheet
How to duplicate a sheet in google spreadsheet api
我使用下面的代码来做到这一点。
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"
}
我在这里做错了什么?创建 sheet 副本的正确方法是什么?
我已经按照 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();
这是创建 sheet 副本的 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);
我使用下面的代码来做到这一点。
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"
}
我在这里做错了什么?创建 sheet 副本的正确方法是什么?
我已经按照 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();
这是创建 sheet 副本的 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);