![見出し画像](https://assets.st-note.com/production/uploads/images/93080909/rectangle_large_type_2_0b7d6a180c6f99b8a4500f510c72497e.png?width=1200)
Photo by
kawabata
宿題:7日目
宿題の感想
setValueの使い方がまだうまく理解できていない事に気が付きました!
動画の振り返り+TAさんの回答例を参考にもう一度トライしてみたいと思います。
![](https://assets.st-note.com/img/1670732770773-yVHaESriZM.png?width=1200)
ヒント
セットバリューでできそう。
//自分で書いたコード:エラーになった
function myFunction7_04() {
const url = 'https://tonari-it.com/not-exists';
try {
const response =UrlFetchApp.fetch(url);
console.log(response.getContentText().slice(0, 300));
}catch (e) {
errorLog();
}
}
function errorLog() {
const sheet = SpreadsheetApp.getActiveSheet();
sheet.getRange('A1').setValue('例外が発生しました:' + e.name);
sheet.getRange('A2').setValue('エラーメッセージ');
}
![](https://assets.st-note.com/img/1670733044806-0VwSD9OS9j.png?width=1200)
ヒント
セットバリューでいけそう
//自分で書いたコード:エラーになった
function myFunction7_08() {
const url = 'https://httpbin.org/post';
const params = {
method: 'post',
payload: { custname: 'Bob', custtel: '090000', comments: 'よろしく!'}
};
const response = UrlFetchApp.fetch(url, params);
const text = response.getContentText();
const obj = JSON.parse(text);
const formData = obj.form;
const sheet = SpreadsheetApp.getActiveSheet();
sheet.getRange('B1:C3').setValue(formData.comments);
}
![](https://assets.st-note.com/img/1670746567389-7V9yRf8rp0.png?width=1200)
ヒント
カスタム関数を使う。
https://tonari-it.com/gas-spreadsheet-user-function/
function ZIPCODE(postcode){
const response = UrlFetchApp.fetch('http://zipcloud.ibsnet.co.jp/api/search?zipcode=' +
postcode);
const results = JSON.parse(response.getContentText()).results;
return results[0].address1 + results[0].address2 + results[0].address3;
}