英文:
Update gs files from script using API
问题
我可以告诉你,从 Google Apps Script 是否可以更改同一项目的 gs 文件的内容。这是为了接收代码更新所必需的。
我正在寻找类似于 Google Clasp 的方式来使用 API 的机会。
这样做有多现实?
[更新]
尝试这段代码:
var scriptID = ScriptApp.getScriptId();
var url = 'https://script.googleapis.com/v1/projects/' + scriptID + '/content';
var token = ScriptApp.getOAuthToken();
var options = {
'method' : 'get',
'headers' : {'Authorization':'Bearer '+ token},
'muteHttpExceptions' : true
};
var response = UrlFetchApp.fetch(url, options);
var json = JSON.parse(response.getContentText());
Logger.log(json);
在 Logger 中:
{error={details=[{domain=googleapis.com, @type=type.googleapis.com/google.rpc.ErrorInfo, reason=ACCESS_TOKEN_SCOPE_INSUFFICIENT, metadata={service=script.googleapis.com, method=google.apps.script.management.v1.ProjectsService.GetContent}}], message=Request had insufficient authentication scopes., code=403.0, status=PERMISSION_DENIED}}
英文:
Can you please tell me if it is possible to change the contents of the gs files of the same project from the google app script. This is required to receive code updates.
I am looking for opportunities to use the API, similar to how google clasp does it.
How realistic is it to do this?
[UPDATE]
Try this code:
var scriptID = ScriptApp.getScriptId();
var url = 'https://script.googleapis.com/v1/projects/' + scriptID + '/content';
var token = ScriptApp.getOAuthToken();
var options = {
'method' : 'get',
'headers' : {'Authorization':'Bearer '+ token},
'muteHttpExceptions' : true
};
var response = UrlFetchApp.fetch(url, options);
var json = JSON.parse(response.getContentText());
Logger.log(json);
In Logger:
{error={details=[{domain=googleapis.com, @type=type.googleapis.com/google.rpc.ErrorInfo, reason=ACCESS_TOKEN_SCOPE_INSUFFICIENT, metadata={service=script.googleapis.com, method=google.apps.script.management.v1.ProjectsService.GetContent}}], message=Request had insufficient authentication scopes., code=403.0, status=PERMISSION_DENIED}}
答案1
得分: 1
如Tanaike所提到的,您实际上可以使用Google Apps Script API编辑Google Apps Script中的.gs文件。
实际上,Google Clasp确实使用Google Apps Script API来执行相同类型的更改。
您可以以以下请求作为示例,使用projects.updateContent
方法对source
参数进行简单更改。此外,您需要确保从Apps Script设置启用了Google Apps Script API,不仅需要在请求中添加要发送的.gs文件,还需要manifest中的appsscript.json文件。
{
"files": [
{
"name": "appsscript",
"type": "JSON",
"source": "{
\"timeZone\": \"America/El_Salvador\",
\"dependencies\": {},
\"exceptionLogging\": \"STACKDRIVER\",
\"runtimeVersion\": \"V8\"
}"
},
{
"name": "Code",
"type": "SERVER_JS",
"source": "function myFunction() {
Logger.log(\"This is a test\");
}",
"functionSet": {
"values": [
{
"name": "myFunction"
}
]
}
}
]
}
参考资料:
英文:
As Tanaike mentioned, you can in fact edit gs files from Google Apps Script using the Google Apps Script API.
Actually Google Clasp does use the Google Apps Script API to make the exact same type of changes.
You can take the following request as an example using the method projects.updateContent
to make a simple change in the source
parameter. In addition to that, you need to make sure that the Google Apps Script API is enabled from your Apps Script Settings, and not only add the gs file you want to send in the request, but also the appsscript.json file from the manifest is required in the request.
{
"files": [
{
"name": "appsscript",
"type": "JSON",
"source": "{\n \"timeZone\": \"America/El_Salvador\",\n \"dependencies\": {},\n \"exceptionLogging\": \"STACKDRIVER\",\n \"runtimeVersion\": \"V8\"\n}"
},
{
"name": "Code",
"type": "SERVER_JS",
"source": "function myFunction() {\n Logger.log(\"This is a test\");\n}\n",
"functionSet": {
"values": [
{
"name": "myFunction"
}
]
}
}
]
}
References:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论