对于每个请求,我正在使用带有此逻辑的Google Sheet API:
Laravel的创建代码如下:
class GoogleSheetsApiClientManager {
private $client;//authorized API client.
private $service = null;
public function __construct($apiOptions) {
$this->client = new Google_Client();
$this->client->setApplicationName(array_get($apiOptions, 'application_name', 'Google Sheets API PHP'));
$this->client->setScopes(array_get($apiOptions, 'scope', Google_Service_Sheets::SPREADSHEETS));
$this->client->setAuthConfig(array_get($apiOptions, 'credential_path'));
$this->client->setAccessType('offline');
$this->service = new Google_Service_Sheets($this->client);
}
public function addNewSheet($spreadsheetId, $hidden = false, $sheetName = false){
try {
$properties = [
'hidden' => $hidden
];
if($sheetName){
$properties['title'] = $sheetName;
}
$body = new Google_Service_Sheets_BatchUpdateSpreadsheetRequest([
'requests' => [
'addSheet' => [
'properties' => $properties
]
]
]);
$result = $this->service->spreadsheets->batchUpdate($spreadsheetId, $body);
return $result;
}
catch (\Exception $exception) {
Log::error('Add new sheet using Google Sheet API : '.$exception->getMessage());
return false;
}
}
}
呼叫类似于:
$apiOptions = [
'application_name' => 'My app name',
'credential_path' => __DIR__.'/service_account-secret.json'
];
$googleSheetManager = new GoogleSheetsApiClientManager($apiOptions);
$spreadsheetId = '--theSpreasheetId--';
$mySheet = $googleSheetManager->addNewSheet($spreadsheetId);
问题:每次创建新工作表时,其名称都基于上次创建的工作表。例如:最后一个工作表名称为sheet5
,即使sheet5
被删除,我也得到了一个新的工作表,名称为sheet6
。
我想要的是尽可能少的名称,这意味着如果sheet5
和sheet4
不存在(但是存在sheet3
),我想要一个新名称{{ 1}},而不是无限的递增名称