Google Apps Script を外部から更新する際に必要なAPI
curl \
'https://script.googleapis.com/v1/projects/18I8JqUgKM2Y07rO4wRAXG6I5QKXJoNAF2FmbFTxMrnookR1fsCiS-OMZ/content?key=[YOUR_API_KEY]' \
--header 'Authorization: Bearer [YOUR_ACCESS_TOKEN]' \
--header 'Accept: application/json' \
--compressed
{
"scriptId": "18I8JqUgKM2Y07rO4wRAXG6I5QKXJoNAF2FmbFTxMrnookR1fsCiS-OMZ",
"files": [
{
"name": "appsscript",
"type": "JSON",
"source": "{\n \"timeZone\": \"Asia/Tokyo\",\n \"dependencies\": {\n },\n \"exceptionLogging\": \"STACKDRIVER\",\n \"runtimeVersion\": \"V8\"\n}",
"lastModifyUser": {
"domain": "gmail.com",
"email": "test@gmail.com",
"name": "test",
"photoUrl": "https://lh3.googleusercontent.com/a/ACg8ocKIzqhRl622IvBlUUfDnYt0q1-LCpcXF9EfOEqEPLKHQmNQvBI=h128"
},
"createTime": "2024-05-19T00:22:49.412Z",
"updateTime": "2024-05-19T00:23:01.235Z",
"functionSet": {}
},
{
"name": "Code",
"type": "SERVER_JS",
"source": "function myFunction() {\n console.log(\"test\");\n}\n",
"lastModifyUser": {
"domain": "gmail.com",
"email": "test@gmail.com",
"name": "test",
"photoUrl": "https://lh3.googleusercontent.com/a/ACg8ocKIzqhRl622IvBlUUfDnYt0q1-LCpcXF9EfOEqEPLKHQmNQvBI=h128"
},
"createTime": "2024-05-19T00:22:49.412Z",
"updateTime": "2024-05-19T00:23:01.235Z",
"functionSet": {
"values": [
{
"name": "myFunction"
}
]
}
}
]
}
curl --request PUT \
'https://script.googleapis.com/v1/projects/18I8JqUgKM2Y07rO4wRAXG6I5QKXJoNAF2FmbFTxMrnookR1fsCiS-OMZ/content?key=[YOUR_API_KEY]' \
--header 'Authorization: Bearer [YOUR_ACCESS_TOKEN]' \
--header 'Accept: application/json' \
--header 'Content-Type: application/json' \
--data '{"files":[{"name":"appsscript","type":"JSON","source":"{\n \"timeZone\": \"Asia/Tokyo\",\n \"dependencies\": {\n },\n \"exceptionLogging\": \"STACKDRIVER\",\n \"runtimeVersion\": \"V8\"\n}","functionSet":{}},{"name":"Code","type":"SERVER_JS","source":"function myFunction() {\n console.log(\"tester\");\n}\n","functionSet":{}}]}' \
--compressed
{
"name": string,
"type": enum (FileType),
"source": string,
"lastModifyUser": {
object (User)
},
"createTime": string,
"updateTime": string,
"functionSet": {
object (FunctionSet)
}
}
https://developers.google.com/apps-script/api/reference/rest/v1/scripts/run?hl=ja
これを実行する前に事前に deployments を作成して、実行可能API化をする必要がある。
https://developers.google.com/apps-script/api/reference/rest/v1/projects.deployments/create?hl=ja
function myFunction() {
const token = ScriptApp.getOAuthToken();
const id = '';
const url = `https://script.googleapis.com/v1/scripts/${id}:run`;
const res = UrlFetchApp.fetch(url, {
method: 'POST',
headers: { Authorization: `Bearer ${token}` },
payload: {
"function": "myFunction",
"devMode": true
},
muteHttpExceptions: true
});
console.log(res.getResponseCode())
console.log(res.getContentText())
}