google-apps-script - 以编程方式为容器绑定表启用特定的 appscript
问题描述
我想以编程方式启用容器绑定的 appscript。我可以通过 api 创建应用程序脚本并附加到工作表。问题是如果工作表已经启用了另一个应用程序脚本,那么除非我指定它,否则该脚本正在工作而不是我的脚本tools->script editor->select project->enable
。我已经通过 api 创建了脚本。我可以通过 api 启用它吗?
到目前为止,我在下面有这段代码。但没有如何以编程方式启用脚本的代码。作为旁注,如果没有其他应用程序脚本附加到电子表格,则以下脚本将按预期工作。否则它是不可预测的
<?php
require __DIR__ . '/vendor/autoload.php';
if (php_sapi_name() != 'cli') {
throw new Exception('This application must be run on the command line.');
}
/**
* Returns an authorized API client.
* @return Google_Client the authorized client object
*/
function getClient()
{
#https://script.google.com/home/usersettings
$client = new Google_Client();
$client->setApplicationName('Google Apps Script API PHP Quickstart');
$client->setScopes(['https://www.googleapis.com/auth/script.projects','https://www.googleapis.com/auth/script.scriptapp','https://www.googleapis.com/auth/spreadsheets','https://www.googleapis.com/auth/spreadsheets.readonly','https://www.googleapis.com/auth/script.external_request','https://www.googleapis.com/auth/script.deployments']);
$client->setAuthConfig('credentials.json');
$client->setAccessType('offline');
$client->setPrompt('select_account consent');
// Load previously authorized token from a file, if it exists.
// The file token.json stores the user's access and refresh tokens, and is
// created automatically when the authorization flow completes for the first
// time.
$tokenPath = 'token.json';
if (file_exists($tokenPath)) {
$accessToken = json_decode(file_get_contents($tokenPath), true);
$client->setAccessToken($accessToken);
}
// If there is no previous token or it's expired.
if ($client->isAccessTokenExpired()) {
// Refresh the token if possible, else fetch a new one.
if ($client->getRefreshToken()) {
$client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
} else {
// Request authorization from the user.
$authUrl = $client->createAuthUrl();
printf("Open the following link in your browser:\n%s\n", $authUrl);
print 'Enter verification code: ';
$authCode = trim(fgets(STDIN));
// Exchange authorization code for an access token.
$accessToken = $client->fetchAccessTokenWithAuthCode($authCode);
$client->setAccessToken($accessToken);
// Check to see if there was an error.
if (array_key_exists('error', $accessToken)) {
throw new Exception(join(', ', $accessToken));
}
}
// Save the token to a file.
if (!file_exists(dirname($tokenPath))) {
mkdir(dirname($tokenPath), 0700, true);
}
file_put_contents($tokenPath, json_encode($client->getAccessToken()));
}
return $client;
}
/**
* Shows basic usage of the Apps Script API.
*
* Call the Apps Script API to create a new script project, upload files to the
* project, and log the script's URL to the user.
*/
$client = getClient();
$service = new Google_Service_Script($client);
// Create a management request object.
$request = new Google_Service_Script_CreateProjectRequest();
$request->setTitle('spreadsheet_script 40');
$request->setParentId('11111');
$response = $service->projects->create($request);
$scriptId = $response->getScriptId();
$code = <<<EOT
function onEdit(e) {
var ui = SpreadsheetApp.getUi();
ui.alert('alert 30091');
}
EOT;
$file1 = new Google_Service_Script_ScriptFile();
$file1->setName('spreadsheet script 56');
$file1->setType('SERVER_JS');
$file1->setSource($code);
$manifest = <<<EOT
{
"timeZone": "America/New_York",
"exceptionLogging": "CLOUD",
"oauthScopes": [
"https://www.googleapis.com/auth/script.projects",
"https://www.googleapis.com/auth/script.scriptapp",
"https://www.googleapis.com/auth/spreadsheets",
"https://www.googleapis.com/auth/spreadsheets.readonly",
"https://www.googleapis.com/auth/script.external_request",
"https://www.googleapis.com/auth/script.deployments"
]
}
EOT;
$file2 = new Google_Service_Script_ScriptFile();
$file2->setName('appsscript');
$file2->setType('JSON');
$file2->setSource($manifest);
$request = new Google_Service_Script_Content();
$request->setScriptId($scriptId);
$request->setFiles([$file1, $file2]);
$request1=new Google_Service_Script_Version();
$request1->setScriptId($scriptId);
$request1->setVersionNumber(1);
$service->projects_versions->create($scriptId,$request1);
$request2=new Google_Service_Script_DeploymentConfig();
$request2->setScriptId($scriptId);
$request2->setVersionNumber(1);
$service->projects_deployments->create($scriptId,$request2);
$response = $service->projects->updateContent($scriptId, $request);
echo "https://script.google.com/d/" . $response->getScriptId() . "/edit\n";
解决方案
推荐阅读
- java - OkHttp3 无法在 Android 上开始握手
- google-cloud-platform - 如何比较流集中 AVRO 和 parquet 文件格式的性能?
- javascript - Javascript:仅在所有成员函数之一上的类型“void”.ts(2339) 上不存在属性“xxxx”
- python - Python:文本提取和列表理解
- python - ec2.Instance' 对象不可迭代
- python - 神经网络的最后一层应该有多少个神经元?
- r - 为什么通过返回解决了意外的数字常数错误?
- javascript - 如何修复:React-Context - TypeError: Object is not iterable (cannot read property Symbol(Symbol.iterator))
- angular - 我的表单无效,但我的 (ngSubmit) 正在调用该函数
- javascript - 有没有办法设置 networkd3 Sankey 的默认垂直对齐方式?