gpt4 book ai didi

google-drive-api - 谷歌表格 API : Create or move Spreadsheet into Folder

转载 作者:行者123 更新时间:2023-12-04 00:54:01 25 4
gpt4 key购买 nike

是否可以在指定的文件夹中创建电子表格,或者我必须使用 Drive API 来移动它?

最佳答案

使用驱动 API 创建一个空工作表并使用工作表 API 打开它:

function getClient()
{
$client = new \Google_Client();

putenv(
'GOOGLE_APPLICATION_CREDENTIALS='.__DIR__."/config/google-creds.json"
);
$client = new \Google_Client();
$client->setScopes(
[
\Google_Service_Drive::DRIVE,
\Google_Service_Storage::CLOUD_PLATFORM,
'https://www.googleapis.com/auth/spreadsheets',
]
);
$client->useApplicationDefaultCredentials();

return $client;
}

// Get the API client and construct the service object.
$client = getClient();
$service = new Google_Service_Drive($client);

$ROOT_FOLDER_ID='you-must-set-this-to-your-folder-id';
// create the empty sheet:

$googleServiceDriveDriveFile = new \Google_Service_Drive_DriveFile();
$googleServiceDriveDriveFile->setMimeType(
'application/vnd.google-apps.spreadsheet')
;
$googleServiceDriveDriveFile->setName('Test');
$googleServiceDriveDriveFile->setParents([$ROOT_FOLDER_ID]);
$res = $service->files->create($googleServiceDriveDriveFile);

// print the id of the file we just made
print 'Created file with id : ' . $res->getId() . "\n";

// Print the names and IDs for up to 10 files.
$optParams = array(
'pageSize' => 10,
'fields' => 'nextPageToken, files(id, name)',
'q' => "'$ROOT_FOLDER_ID' in parents"
);

$results = $service->files->listFiles($optParams);

if (count($results->getFiles()) == 0) {
print "No files found.\n";
} else {
print "Files:\n";
foreach ($results->getFiles() as $file) {
printf("%s (%s)\n", $file->getName(), $file->getId());
}
}
// fetch the sheet you created and edit it.
$service = new Google_Service_Sheets($client);
$sheet = $service->spreadsheets->get($res->getId());

print "Fetched sheet with name: " . $sheet->getSpreadsheetUrl() . "\n";

关于google-drive-api - 谷歌表格 API : Create or move Spreadsheet into Folder,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42938990/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com