💬
doGet & doPost のイベントオブジェクト | GAS
main.gs
function doGet(e) {}
// ^^^ これの中身のこと
doGet のイベントオブジェクト
curl 'https://script.google.com/macros/s/…/exec?foo=var&hoge=123'
{
"contextPath": "",
"parameter": {"hoge": "123", "foo": "var"},
"contentLength": -1,
"queryString": "foo=var&hoge=123",
"parameters": {"hoge": ["123"], "foo": ["var"]}
}
配列を渡した場合
curl 'https://script.google.com/macros/s/…/exec?foo[]=var&foo[]=123'
{
"parameter": {"foo[]": "var"},
"contentLength": -1,
"queryString": "foo%5B%5D=var&foo%5B%5D=123",
"contextPath": "",
"parameters": {"foo[]": ["var","123"]}
}
doPost のイベントオブジェクト
curl https://script.google.com/macros/s/…/exec -d '{\"foo\": \"var\", \"hoge\": 123}' -H 'Content-Type: appliication/json'
{
"parameter": {},
"parameters": {},
"contentLength": 21,
"contextPath": "",
"postData": {
"contents": "{\"foo\": \"var\", \"hoge\": 123}",
"length": 21,
"name": "postData",
"type": "appliication/json"
},
"queryString": ""
}
配列を渡した場合
curl https://script.google.com/macros/s/…/exec -d '{\"foo\": \"var\", \"hoge\": [123, 456]}' -H 'Content-Type: appliication/json'
{
"parameters": {},
"parameter": {},
"contentLength": 28,
"contextPath": "",
"postData": {
"contents": "{\"foo\": \"var\", \"hoge\": [123, 456]}",
"length": 28,
"name": "postData",
"type": "appliication/json"
},
"queryString": ""
}
フォームデータを渡した場合
JavaScript で送信する
sample-formdata.js
// フォームデータを生成する
const formData = new FormData();
formData.append('username', 'John');
formData.append('email', 'john@example.com');
formData.append('age', 30);
// フォームデータを送信する
fetch('https://script.google.com/macros/s/…/exec', {
method: 'POST',
body: formData
})
.then((response) => {
if (!response.ok) throw new Error('リクエストが失敗しました');
return response.text();
})
.then((data) => console.log('成功:', data))
.catch((error) => console.error('エラー:', error));
{
"queryString": "",
"contextPath": "",
"parameter": {
"email": "john@example.com",
"age": "30",
"username": "John"
},
"contentLength": 317,
"parameters": {
"username": ["John"],
"age": ["30"],
"email": ["john@example.com"]
}
}
Discussion