やること
スプレットシートの情報をGASでChat Work通知する
コード
function myFunction() {
//Chat Work API
const token = 'xxxxxxxxxxxxxxxxxxxxxxxxx';
const base_url = 'https://api.chatwork.com/v2';
const room_id = 'xxxxxxxxxxxxxxxxxxxxxxxxxx'
const url = base_url + '/rooms/' + room_id + '/messages';
//スプレットシート
const sheet_id = 'xxxxxxxxxxxxxxxxxxxxx';
const ss = SpreadsheetApp.openById(sheet_id);
const sheet = ss.getSheetByName("シート 1");
const cell = sheet.getDataRange().getValues();
const value = cell.toString();
//確認のため
console.log(value);
const options = {
'method': 'post',
'headers': {'X-ChatWorkToken' : token},
'payload': {'body' : value}
};
const result = UrlFetchApp.fetch(url, options);
const json = JSON.parse(result.getContentText());
//確認のため
console.log(json);
}
※room_id
はCWのURLに入っているrid
以降
※sheet_id
はスプレットシートのURLに入っているspreadsheets/d/
以降
気をつけること
setValue:1 つのデータだけをセットする
setValues:複数のデータをセットする
s
がついているかついていないかで大きく変わる!!
最後に
今回は全てのセルを取得してますが自動でスプレットシートにデータが入ってきたら、範囲を指定してCWで通知するとか使えそうですね。
参考文献