【GAS】スクリプト内の変数をHTMLで表示する
スクリプト側
code:script.js
function doGet() {
let notificationSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('通知');
let notificaiton = notificationSheet.getRange(2, 1, notificationSheet.getLastRow() - 1, notificationSheet.getLastColumn()).getValues();
console.log(notificaiton);
//Date型の日付をStirng型に
for(let i =0; i < notificaiton.length; i++){
let y = dt.getFullYear();
let m = ("00" + (dt.getMonth()+1)).slice(-2);
let d = ("00" + dt.getDate()).slice(-2);
let result = y + "/" + m + "/" + d;
}
//最新のお知らせを上にしたいので配列を逆に
notificaiton = notificaiton.reverse();
let html = HtmlService.createTemplateFromFile("index");
//htmlに通知データを与える
html.notificationData = notificaiton;
Logger.log(html.notificationData);
let resurt = html.evaluate()
resurt.setXFrameOptionsMode(HtmlService.XFrameOptionsMode.ALLOWALL);
return resurt;
}
18行目でnotificaitonという変数をHTMLに与えている
HTML側
code:index.html
<head>
<meta charset="UTF-8">
<title>お知らせ</title>
</head>
<body style="margin:0px!important">
<? notificationData.forEach(function(option) { ?>
<div style="background:<?= option2 ?>"><strong><?= option0 ?></strong> <br/>
<? }); ?>
</body>
</html>
<?= hoge ?> // gsファイルでhogeにテキストを代入することができる
<?!= hoge ?> // gsファイルでhogeに タグ付きの要素を代入することができる
参考欄の 1. より引用
上のコードでは仮引数の名前をoptionにし、さらに配列を与えているため、<?!= option[1] ?>などで値を与えている
私は、タグ付きの要素の代入の仕方がわからなかったため、以下のサイトは本当に役に立った。
参考