Skip to content

Instantly share code, notes, and snippets.

@ymuichiro
Last active February 9, 2025 15:46
Show Gist options
  • Save ymuichiro/99c39b5ed3374ab1e2e62e10583a112f to your computer and use it in GitHub Desktop.
Save ymuichiro/99c39b5ed3374ab1e2e62e10583a112f to your computer and use it in GitHub Desktop.
gas remote api

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

Files オブジェクトの構造体

{
  "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())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment